Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you're coming from PHP, perhaps it would be more helpful to give you an overview of CSharp's syntax shortcuts!<br> They're really confusing at first glance because the syntax is SO similar. But after a quick intro, you'll learn to love them.</p> <h2>Collection Initializers</h2> <p>A Collection Initializer adds items to a collection. The syntax is <code>new SOMECOLLECTION { value, value, value }</code></p> <p>The traditional way to populate a collection:</p> <pre><code>var items = new List&lt;int&gt;(); items.Add(1); items.Add(2); items.Add(3); </code></pre> <p>The <strong>Collection Initializer</strong> way - this compiles to nearly identical code:</p> <pre><code>var items = new List&lt;int&gt;(){ 1, 2, 3 }; </code></pre> <p>Side note: <strong>Array vs List</strong> - I'd say <code>List</code> is more commonly used because its size can change. But it's mostly personal preference. The short-hand way to create an array is: <code>var items = new int[]{ 1, 2, 3 };</code>, but you can also omit the type and the compiler will figure it out (aka implicitly-typed array): <code>var items = new []{ 1, 2, 3 };</code></p> <h2>Object Initializers</h2> <p>An Object Initializer sets the properties of a new object. The syntax is <code>new SOMEOBJECT { Property = value, Property = value }</code></p> <p>The traditional way to populate the properties of an object:</p> <pre><code>var item = new Item(); item.First = "Chuck"; item.Last = "Norris"; </code></pre> <p>The <strong>Object Initializer</strong> way:</p> <pre><code>var item = new Item(){ First = "Chuck", Last = "Norris" }; </code></pre> <h2>Anonymous Objects</h2> <p>I'm only mentioning this because the syntax for an anonymous object is extremely similar to the others, but does something much, much different. They are extremely confusing if you aren't aware of them!</p> <p>The syntax looks like a combination of the Collection and Object Initializers: <code>new { AnyPropertyName = value, anyVariableNameBecomesAProperty, someOtherObject.SomeProperty }</code></p> <p>Let me explain! The compiler will create an "anonymous object" that has 3 properties, <code>AnyPropertyName</code>, <code>anyVariableNameBecomesAProperty</code>, and <code>SomeProperty</code>, and initializes the values the same way as the Object Initializer does. If you don't specify a property name, the name is inferred from the source. </p> <p>I hope this helps you understand why <code>new { h1, h2 }</code> (new anonymous object) is WAY different from <code>new []{ h1, h2 }</code> (new array).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload