Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 315469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:17:40+00:00 2026-05-12T08:17:40+00:00

So I started reading Jon Skeet’s 2nd edition of C# in depth and kind

  • 0

So I started reading Jon Skeet’s 2nd edition of C# in depth and kind of confused about the following code in terms what it does and what is wrong with it (ch 13, section: Immutability and object initialization)

Message message = new Message(
"skeet@pobox.com",
"csharp-in-depth-readers@everywhere.com",
"I hope you like the second edition")
{
       Subject = "A quick message"  // <=  {Subject = "A quick message"   }; what is it?
};

Elaboration on this topic, would help tremendously!

Would someone explain that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:17:40+00:00Added an answer on May 12, 2026 at 8:17 am

    This code:

    Message message = new Message(
    "skeet@pobox.com",
    "csharp-in-depth-readers@everywhere.com",
    "I hope you like the second edition")
    {
           Subject = "A quick message"
    };
    

    is equivalent to this:

    Message tmp = new Message("skeet@pobox.com", 
        "csharp-in-depth-readers@everywhere.com",
        "I hope you like the second edition");
    tmp.Subject = "A quick message";
    Message message = tmp;
    

    That’s just object initializer syntax: see chapter 8 for more details.

    The problems with this are:

    • It’s not clear what all the string arguments mean – what’s “from”, what’s “to”, what’s the body etc
    • It requires the type to be mutable (unfortunate for all kinds of reasons)
    • If you want to also specify the subject in the constructor, you end up with a bunch
      of different overloads
    • If you have cross-validation between (say) subject and body, you need to be careful about when you do it

    With optional parameters and named arguments, all of this is solved:

    // The argument names may be wrong here; I don't have the book with me.
    // Intellisense will prompt you though :)
    Message message = new Message(
        from: "skeet@pobox.com",
        to: "csharp-in-depth-readers@everywhere.com",
        body: "I hope you like the second edition",
        subject: "A quick message);
    

    It’s clear what means what, it’s all in a constructor call so the type can be immutable, you can have a single constructor with optional parameters (e.g. here we could have a byte[] attachment = null parameter specified in the constructor), and you can do all the validation in one place. Lovely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started reading Jon Skeet's early access version of his book, which contains sections
I was reading Jon Skeet's refcard on C#. He stated: Events are closely related
I started reading about underscore.js today, it is a library for javascript that adds
I have started reading the book Essential Linux Device Drivers. I am following the
When I first started reading about and learning ruby, I read something about the
I recently started reading about ASP.net MVC and after getting excited about the concept,
When I first started reading about Python, all of the tutorials have you use
I have just started reading about ORMLite since I am interested in using it
I started reading more about ThreadPoolExecutor from Java Doc as I am using it
I started reading about struts2 yesterday and came across the below slide. Site link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.