2
Answers

C# structure

Neo

Neo

16y
3.7k
1

Hi,

I just gone through following link

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=120

read following code lines. However following code is fine I guess because function MyMethod not initialise any variable. Although you declare  MyInterface obj= new MyStruct();  still it will work. Please comment.

 

interface MyInterface
    {
         void MyMethod();
    }
    struct MyStruct : MyInterface
    {
        public  void MyMethod()
        {
            Console.WriteLine("Struct Method from MyStruct");
            Console.ReadLine();

        } 

    } // end of structure
    class Class1
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main(string[] args)
        {
             MyInterface obj= new MyStruct();
                 
            obj.MyMethod(); // this is error
    
        }
    } // end of main class
Answers (2)