Object initialization expressions

            1. What are the ways to initialize objects?

            2. What are the ways to initialize nested members?

            3. What are the ways to initialize lists and arrays?

Example 1:

    public class Test

    {

        public Test() { }

 

        public Test(bool param1, int param2)

        {

            Property1 = param1;

            Property2 = param2;

        }

 

        public bool Property1 { set; get; }

        public int Property2 { set; get; }

        public string Property3 { set; get; }

 

        public override string ToString()

        {

            return Property1.ToString() + Property2.ToString() + Property3;

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            var test = new Test { Property1 = true, Property2 = 3 };

 

            var test2 = new Test(false, 1) { Property3 = "Test" };

        }

    }

In first case the default constructor is called and properties are initilized by specified values. In second case the specific construcor is called and property is initilized by a value.

Example 2:

    public class Point

    {

        public int X { set; get; }

        public int Y { set; get; }

    }

 

    public class Rectangle

    {

        public Point TopLeft { set; get; }

        public Point BottomRight { set; get; }

    }

var rect = new Rectangle { TopLeft = new Point { X = 0, Y = 0 }, BottomRight = new Point() { X = 10, Y = 10 } };

It is possible to initialize nested members using object initialization expressions.

Example 3:

    public class Rectangle

    {

        Point topLeft = new Point { X = 0, Y = 0 };

        Point bottomRight = new Point { X = 10, Y = 10 };

        public Point TopLeft { set { topLeft = value; } get { return topLeft; } }

        public Point BottomRight { set { bottomRight = value; } get { return bottomRight; } }

    }

var rect = new Rectangle { TopLeft = { X = 0, Y = 0 }, BottomRight = { X = 10, Y = 10 } };

In case of nested members are already initialized we can skip the new operator.

Example 4:

        static void Main(string[] args)

        {

 

            List<int> integers = new List<int> { 1, 3, 9, 18 };

 

            List<Test> list = new List<Test>{

                new Test(){ Property2 = 0 },

                new Test(){ Property2 = 1 },

                new Test(){ Property2 = 2 }

            };

 

            Trace.WriteLine("———————————–");

 

            ArrayList arrayListOfInts = new ArrayList() { 1, 3, 9, 18 };

 

            ArrayList arrayListOfTests = new ArrayList()

            {

                new Test(){ Property2 = 0 },

                new Test(){ Property2 = 1 },

                new Test(){ Property2 = 2 }

            };

 

 

            list.ForEach(el => Trace.WriteLine(el));

 

            foreach (var el in arrayListOfTests)

            {

                Trace.WriteLine(el);

            }

 

        }

As we can add constants to lists and arrays, we can add initialized objects there by using object initialization expressions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s