Thursday, August 29, 2013

C# inner object’s touch notation.

The notation will not create or change the internal object.

That's it

var obj = new Obj
       {
              Inner = {CompanyName = "Any name"}
       };

Required that the inner object was created before of use.

The full example:

       class Program
       {

             static void Main()
             {
                    var obj = new Obj
                           {
                                  Inner = {CompanyName = "Any name"}
                           };
             }


       }

       class Obj
       {
             private InnerObj _inner = new InnerObj();
             public InnerObj Inner
             {
                    get
                    {
                           return _inner;
                    }
                    set
                    {
                           _inner = value;
                    }
             }
       }

       class InnerObj
       {
             private string _companyName;
             public String CompanyName
             {
                    get
                    {
                           return _companyName;
                    }
                    set
                    {
                           _companyName = value;
                    }
             }
       }

No comments:

Post a Comment