Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
C#
class Program: abc
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello interface");

            xyz();

            Console.ReadLine();
            return;
        }
        public void xyz()
        {
            Console.WriteLine("interface XYZ");
        }
    }
    interface abc
    {
        void xyz();
    }
Posted
Comments
Herman<T>.Instance 22-Jun-14 13:29pm    
public static void xyz()
or Program prg = new Program(); prg.xyz();
larkspurklose3 22-Jun-14 13:35pm    
instead of
Program prg = new Program(); prg.xyz();

if i would write
abc prg = new Program(); prg.xyz();

is it fine to do so..??
Raul Iloc 22-Jun-14 15:20pm    
Yes is fine also, because your "Program" class implements the interface.

You are trying to access an instance member directly into your Main() without to use an object, and this in incorrect. You must first create an object then use its method like in the next example:
C#
abc testObject = new Program(); //The object is declared as interface "abc" but could be also declared of type "Program"!
testObject.xyz();
 
Share this answer
 
Your piece of code in your comment is now correct. You need to learn what are instance members vs public ones, the role of "this" parameter and understand general object-oriented syntax and semantic behind it (no, this is not yet OOP, just one of the prerequisites). Please see my past answers:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900