none
What is the benefit of simplified object initialization? RRS feed

  • Question

  • I have code like this

    var form = new GetDate();
    form.Text = "Assigned";
    form.PreviousDate = txtEntered.Tag as DateTime?;
    form.txtDate.Text = txtAssigned.Text == string.Empty ? DateTime.Now.ToString("MM/dd hh:mm") : txtAssigned.Text;
    form.ShowDialog();
    

    Visual Studio would rather this code be like this

    var form = new GetDate
    {
    	Text = "Assigned",
    	PreviousDate = txtEntered.Tag as DateTime?
    };
    form.txtDate.Text = txtAssigned.Text == string.Empty ? DateTime.Now.ToString("MM/dd hh:mm") : txtAssigned.Text;
    form.ShowDialog();
    Ok, but why? The VS suggestion seems less readable to me. Is there a compiled benefit?

    Saturday, January 25, 2020 8:26 PM

Answers

  • In this example, the difference is rather slight.  The issue is that the first case has to create a complete object, using the default constructor, which will create values for all the fields.  You then replace some of those fields.

    In the second case, the constructor gets to fill in those fields with your values directly.  It doesn't have to create default fields and then override them.

    In your case, it's probably not important, but if the fields require non-trivial effort to construct, it can be lots better to pass values into the constructor rather than override them later.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Robert in SF Saturday, January 25, 2020 11:15 PM
    Saturday, January 25, 2020 10:21 PM

All replies

  • Hello,

    Its personal preference to use the second in the second code block, the same applies for when declaring variables e.g. var verses the actual type but unlike the suggestion you are asking for sometime var is bad if you can not look at a line of code and know what it is without hovering over the variable.

    Many times using the ? conditional operator is unwise while on the subject as it may make understanding the code days, months years down the road hard to figure out.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 25, 2020 8:42 PM
    Moderator
  • In this example, the difference is rather slight.  The issue is that the first case has to create a complete object, using the default constructor, which will create values for all the fields.  You then replace some of those fields.

    In the second case, the constructor gets to fill in those fields with your values directly.  It doesn't have to create default fields and then override them.

    In your case, it's probably not important, but if the fields require non-trivial effort to construct, it can be lots better to pass values into the constructor rather than override them later.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Robert in SF Saturday, January 25, 2020 11:15 PM
    Saturday, January 25, 2020 10:21 PM
  • There are many things that Microsoft wants to make us comply with that I refuse to comply with. In this situation, I doubt that there is any difference in the compiled code for both options.

    I do not however understand your assignment to PreviousDate. It appears incomplete.

    C# inherited the conditional operator from C and C++. VB programmers might be unfamiliar with it and therefore they might not like it. It is easy to understand if you are familiar with it, but as I say, I am not familiar with its use as used for PreviousDate.

    A mildly off-topic remark is that async/await is an example of something that can (not always) make a program unnecessarily complicated yet Microsoft thinks that async/await is wonderful.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, January 25, 2020 10:41 PM
  • And in tutorials the option that Microsoft really prefers is to create constructors that create objects.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, January 25, 2020 10:45 PM
  • There are many things that Microsoft wants to make us comply with that I refuse to comply with.

    Ha, ha, me too! :)

    Saturday, January 25, 2020 11:14 PM
  • I do not however understand your assignment to PreviousDate. It appears incomplete.

    C# inherited the conditional operator from C and C++. VB programmers might be unfamiliar with it and therefore they might not like it. It is easy to understand if you are familiar with it, but as I say, I am not familiar with its use as used for PreviousDate.


    Sam … I believe that Robert was using the .Tag property of the txtDate TextBox (which is simply an object) to store the date that had been previously used.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, January 25, 2020 11:16 PM
    Moderator
  • In your case, it's probably not important, but if the fields require non-trivial effort to construct, it can be lots better to pass values into the constructor rather than override them later.
    I get it. This is the takeaway. Thanks.
    Saturday, January 25, 2020 11:17 PM
  • Sam … I believe that Robert was using the .Tag property of the txtDate TextBox (which is simply an object) to store the date that had been previously used.

    That part is not part of the confusion. My confusion is the result of not seeing the as.

    And the trailing question mark is probably some other operator, it probably makes the field nullable. Is that it? The designers of C# are using the "?" in more than one way and that makes the code difficult to read. One of the fundamental design criteria is for C# to be readable and they break that rule often.



    Sam Hobbs
    SimpleSamples.Info


    Sunday, January 26, 2020 12:28 AM