vendredi 8 mai 2015

C# create object for same class in the class itself

I am just trying out some things in c# and i came across this where i have created an object for the same class in the class itself

class Class1
    {
        public int test1 { get; set; }
        Class1 x = new Class1();
    }

and then i have tried to create object from an other class which is throwing an error

An unhandled exception of type 'System.StackOverflowException' occurred in Test.exe

class Program
    {
        static void Main(string[] args)
        {
            Class1 x = new Class1();
        }
    }

i have googled and i havent found any related links to c# but i found it in c++ where they say a Class1 is an incomplete type, as it has not been defined yet, rather it's being defined. I would like to know whether its the same case for c# also

Aucun commentaire:

Enregistrer un commentaire