Using Object Initalizers yet?

Posted Thursday, February 14, 2008 9:03 AM by CoreyRoth

Object Initalizers aren't talked about very much as one of the new features in C# 3.0, but I find them pretty useful from time to time (esepcially when dealing with the SharePoint API).  They are great, when there isn't a constructor that has all of the arguments that you need to initialize the class with in it.  Here is an example of how I used it lately with the KeywordQuery class.

KeywordQuery myQuery = new KeywordQuery(siteCollection)

{

    QueryText = string.Format("Color:\"{0}\"", myColor),

    ResultTypes = ResultType.RelevantResults

};

In this case I wanted to initialize the QueryText and ResultType properties. This makes the syntax much cleaner.  Obviously this feature isn't life changing, but I find it a nice convenience.

Filed under: ,

Comments

No Comments

Leave a Comment

(required)
(required)
(optional)
(required)