Using new syntax styles in .Net 3.5


.NET 3.x came with lot of new definition styles, which will reduce the number of lines a developer needs to be write compared to conventional OOPS type definitions. But unfortunately, many people still sticking on old methodologies. I recently noticed some of my members practicing this unacceptable way of coding. To motivate them what I done is prepared some logics in both old 2.0 version style and 3.x version style. Here I am going to present them. I am sharing some important aspects of these examples, which will clearly show you how we will be benefitted by using new syntaxes defined in .NET 3.x.

Automatic Properties

Properties are there from 1.0 version of .NET. So people have a tendency to follow the same syntax defined in early version. But in 3.x property definition has been refined by removing the declaration of private variables, commonly known as Automatic Properties. To make this understand to members, I declared 2 new classes i.e. one with old style and another with new styles. But both classes contain same entities. The difference is shown below.

Lines used in old 2.0 style - 69 lines
Lines used in new 3.x style- 14 lines

Not believing, just check both classes definition below

2.0 definition style of a class

namespace WebApplication1.OldStyle
{
    public class EmployeeOldStyle
    {
        private string _id;
        private string _name;
        private Address _address;
        public string ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public Address EmpAddress
        {
            get
            {
                return _address;
            }
            set
            {
                _address = value;
            }
        }
        public class Address
        {
            private string _address1;
            private string _address2;
            public string Address1
            {
                get
                {
                    return _address1;
                }
                set
                {
                    _address1 = value;
                }
            }
            public string Address2
            {
                get
                {
                    return _address2;
                }
                set
                {
                    _address2 = value;
                }
            }
        }
    }
}

3.x definition style of a class

namespace WebApplication1.NewStyle
{
    public class EmployeeNewStyle
    {
        public string ID{get;set;}
        public string Name{get;set;}
        public Address EmpAddress{get;set;}
        public class Address
        {
            public string Address1{get; set;}
            public string Address2{get;set;}
        }
    }
}

Difference is visible in 1st look itself. All those lines we reduced by declaring properties as "Automatic Properties" i.e. without any private variables, just get and set. More over we can make a property definition in a single line like public string ID{get;set;}. Now every one may convinced with "Automatic Properties" declarations whenever using .net 3.x version. Please don't blindly follow old syntax. If you are coding with a specific version, then utilise its full features.

Object Initializers

Now you have 2 classes like above. How you will initialise an object of a class during it's creation. One conventional way is to define an overloaded contructor and pass the values to that constructor during object creation. But here we are losing optional feature. Some value should be passed through constructor. Other conventional method on which we have full control of setting our objects initial values is, after object creation manually setting it's public members. These all are part of .Net since 1.0. But 3.x provided owsome methods for object initialize. I compared both methods over the classes mentioned above. Result is like below

Lines used in old 2.0 style - 7 lines
Lines used in new 3.x style- 1 line

Have a look on code used

2.0 style of object initialization

EmployeeOldStyle oldEmp = new EmployeeOldStyle();
EmployeeOldStyle.Address oldAdd = new EmployeeOldStyle.Address();
oldAdd.Address1 = "Address1";
oldAdd.Address2 = "Address2";
oldEmp.ID = "1";
oldEmp.Name = "Jaish";
oldEmp.EmpAddress = oldAdd;

3.x style of object initialization

EmployeeNewStyle newEmp = new EmployeeNewStyle { ID = "1", Name = "Jaish", EmpAddress = new EmployeeNewStyle.Address { Address1 = "Address1", Address2 = "Address2" } };

You can identify that if more complex types included, like more properties of type same as "Address", the difference will increase again. .net 3.x simply using "{}" and passing the properties name and assigning the initial values to it. It's more readble that we can see which property values got initialized. Another aspect is that passing properties is optional. If you have 10 properties, you can decide which ever proerties need to be initialized.

Collection Initializers

We are handling various custom collections, which may contain number of class objects inside it. I stored 2 objects of each of the classes mentioned above. The code shown below difference

Lines used in old 2.0 style - 17 lines
Lines used in new 3.x style- 1 line!!!

Have a look on code too

2.0 style of collection initialization

    List<EmployeeOldStyle> list = new List<EmployeeOldStyle>();
            EmployeeOldStyle oldEmp = new EmployeeOldStyle();
            EmployeeOldStyle.Address oldAdd = new EmployeeOldStyle.Address();
            oldAdd.Address1 = "Address1";
            oldAdd.Address2 = "Address2";
            oldEmp.ID = "1";
            oldEmp.Name = "Jaish";
            oldEmp.EmpAddress = oldAdd;
            list.Add(oldEmp);
            oldEmp = new EmployeeOldStyle();
            oldAdd = new EmployeeOldStyle.Address();
            oldAdd.Address1 = "Address1A";
            oldAdd.Address2 = "Address2A";
            oldEmp.ID = "2";
            oldEmp.Name = "Yadhu";
            oldEmp.EmpAddress = oldAdd;
            list.Add(oldEmp);

3.x  style of collection initialization

List<EmployeeNewStyle> list = new List<EmployeeNewStyle> { new EmployeeNewStyle { ID = "1", Name = "Jaish", EmpAddress = new EmployeeNewStyle.Address { Address1 = "Address1", Address2 = "Address2" } }, new EmployeeNewStyle { ID = "2", Name = "Yadhu", EmpAddress = new EmployeeNewStyle.Address { Address1 = "Address1A", Address2 = "Address2A" } } };

Here also we used the "{}". But this time for both collection and type. 

Summary: 

At runtime, the semantics will be exactly the same as with today's longer syntax (so you don't need to worry about behavior changes).  But now you don't need to type as much, and your code can be more crisp and concise. Download the code attached and clarify for any doubts.

erver'>

Similar Articles