.net - C# Syntax explanation -


i saw few days ago syntax , wondered if tell me how called, how work , useful.

when ask how work mean setters property readonly(get), , second braces mean: "setters = {".

http://msdn.microsoft.com/en-us/library/ms601374.aspx

thanks

datagrid.cellstyle = new style(typeof(datagridcell))                 {                     // cancel black border appears when user presses on cell                     setters = { new setter(control.borderthicknessproperty, new thickness(0)) } // end of setters                 } // end of style 

it call object initializer , collection initializer , allows set properties in { .. } block when calling constructor. inside block, you're using setters = { ... } collection initializer - allows specify elements of collection (here, don't have create new instance of collection - adds elements in curly braces). more information see msdn page.

in general, syntax of object initializers has few options:

// without explicitly mentioning parameter-less constructor: new { prop1 = ..., prop2 = ... } // specifying constructor arguments: new a(...) { prop1 = ..., prop2 = ... } 

the syntax collection initializers looks this:

// creating new instance new list<int> { 1, 2, 3 } // adding existing instance inside object initializer: somelist = { 1, 2, 3 } 

it worth mentioning closely related anonymous types (where don't give type name - compiler generates hidden type , can work using var):

// create anonymous type properties new { prop1 = ..., prop2 = ... } 

all of these features new in c# 3.0. see so post explains tricky aspect of collection initializers (in style you're using them).


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -