Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has to do with the difference between <a href="http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=vs.80%29.aspx" rel="nofollow">Value Types and Reference Types</a>.</p> <p>"A data type is a value type if it holds the data within its own memory allocation." whereas, "A reference type contains a pointer to another memory location that holds the data."</p> <p>Common examples of value types: </p> <ul> <li>All numeric data types</li> <li>Boolean, Char, and Date</li> <li>All structures, even if their members are reference types</li> <li>Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, Short, UInteger, or ULong</li> </ul> <p>Common examples of reference types:</p> <ul> <li>String</li> <li>All arrays, even if their elements are value types</li> <li>Class types, such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form%28v=vs.80%29.aspx" rel="nofollow">Form</a></li> <li>Delegates</li> </ul> <p>All value types are initialized to a default value(zero for numeric types), and reference types point to <code>null</code>.</p> <p>A declaration of:</p> <pre><code>int num; </code></pre> <p>Does not initialize the variable <code>num</code>. Therefore <code>num</code> cannot be used. C# requires that each variable is set to some value(whether <code>null</code> or a number) before it is used.</p> <p>The same goes for reference types:</p> <pre><code>//un-initialized, therefore cannot be used until a value is given(A compiler error will occur) string str; </code></pre> <p>However, a declaration of:</p> <pre><code>int[] numbers = new int[5]; </code></pre> <p>Causes the array to be initialized(therefore calling the array's constructor) which then causes each of the elements to be initialized to the default value of int(i.e. zero).</p> <p>Using the same method on a reference type yields the same result:</p> <pre><code>string[] strings = new string[5]; </code></pre> <p>Causes the array to be initialized and therefore creating an array of <code>null</code> strings(which is the default value of all reference types).</p> <p><a href="http://msdn.microsoft.com/en-us/library/vstudio/ah19swz4.aspx" rel="nofollow">Structures</a> are the data type that you are looking for. Structures are value types and therefore cannot be set to <code>null</code>. Since they cannot be set to <code>null</code>, they must have some default value. The default value is determined by a <a href="http://en.wikipedia.org/wiki/Default_constructor" rel="nofollow">default constructor</a>.</p> <p>A structure can be defined like this:</p> <pre><code>struct MyStructName { //Members of the struct, like a class, default value is 0. int myNumber; //Structs can have members whose data type is a reference type //Default value is null. string myString; //Every struct has a default constructor, though you cannot define one. //The compiler generates one for you that initializes every member to it's //default value. } </code></pre> <p>This will define a data type that contains the members, myNumber and myString, that cannot be set to <code>null</code>.</p> <pre><code>//Array of MyStructName, the default constructor is called and each value is initialized MyStructName[] data = new MyStructName[5]; //Loop and print results for(int i = 0; i &lt; data.Length; i++) { //Prints the index, number and the string or "null" Console.WriteLine("{0}: myNumber: {1}, myString: \"{2}\"", i, data[i].myNumber, data[i].myString != null ? data[i].myString : "null"); } </code></pre>
    singulars
    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. 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