www.webdeveloper.com
Results 1 to 3 of 3

Thread: What is the advantage of using Object Initializer

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Posts
    60

    What is the advantage of using Object Initializer

    Hello everyone,

    I want to know that is there any advantage of using Object Initializer?

    For ex. i have code like this:

    Code:
                    SqlParameter nodeparameter = new SqlParameter();
                    nodeparameter.ParameterName = "@nodevalue";
                    nodeparameter.SqlDbType = SqlDbType.VarChar;
                    nodeparameter.Size = parentnodeid.Length;
                    nodeparameter.Direction = ParameterDirection.Input;
                    nodeparameter.Value = strSplit[1];
                    myCommand.Parameters.Add(nodeparameter);
    It is working fine, but i have write this code in an other way, like this:

    Code:
                    SqlParameter nodeparameter = new SqlParameter
                   {
                    ParameterName = "@nodevalue",
                    SqlDbType = SqlDbType.VarChar,
                    Size = parentnodeid.Length,
                    Direction = ParameterDirection.Input,
                    Value = strSplit[1]
                    };
                    myCommand.Parameters.Add(nodeparameter);

    This code is also working fine,
    but i want to know that which way of writing code is better.
    if you have any idea, please tell me.

    Thanks

  2. #2
    Join Date
    Apr 2006
    Posts
    60
    Hello Again,

    I have found a link which is related to this thread.
    http://stackoverflow.com/questions/4...any-difference

  3. #3
    Join Date
    Oct 2012
    Posts
    47
    While this is a cool feature, it has some practical limitations once you need to go beyond a simple get / set logic. Another new feature is out the idea of ​​object initializers. Object initializersare an important aspect of LINQ to extensions. NET, but not limited to use in only LINQ expressions.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles