Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Value vis-a-vis Reference Types</h3> <p><em>Variables</em> in C# are in 1 of 2 groups. <em>Value</em> types or <em>Reference</em> types. Types like <code>int</code> and <code>DateTime</code> are <em>value</em> types. In contrast, any class you create is a <em>reference</em> type. C# strings are also a <em>reference</em> type. Most things in the .NET framework are <em>reference</em> types.</p> <h3>Parts of a Variable</h3> <p>There is the variable <em>name</em> and it's <em>value</em>. <strong>Two parts</strong>.</p> <p>The variable's <em>name</em> is what you <strong><em>declare</em></strong> it to be. The <em>value</em> is what you <strong><em>assign</em></strong> to it.</p> <h3>Variables are Initialized</h3> <p>All variables are always given an <em>initial</em> value at the point the variable is <em>declared</em>. Thus all variables are <strong><em>initialized</em></strong>.</p> <p>For <em>value</em> types, like <code>int</code> the compiler will give them a valid value if you do not do so explicitly. <code>int</code>'s <strong><em>initialize</em></strong> to zero by default, <code>DateTime</code>'s <strong><em>initialize</em></strong> to <code>DateTime.MinValue</code> by default.</p> <p><em>Reference</em> type variables <strong><em>initialize</em></strong> to the object you give it. The compiler will not <strong><em>assign</em></strong> an object (i.e. a valid value) if you don't. In this case the value is <code>null</code> - nothing. So we say that the reference is <strong><em>initialized</em></strong> to null.</p> <h3>Objects are Instantiated</h3> <p>Humans are born. Objects are instantiated. A baby is an <em>instance</em> of a Human, an object is an <em>instance</em> of some Class.</p> <p>The act of creating an <em>instance</em> of a Class is called <strong><em>instantiation</em></strong> (Ta-Da!)</p> <h3>So <em>declare</em>, <em>initialize</em>, and <em>instantiate</em> come together like this</h3> <pre><code>MyClass myClassyReference= new MyClass(); </code></pre> <p>In the above, it is wrong to say "... creating an instance of an <em>object</em>..."</p> <h3>The Unbearable Lightness of Being</h3> <p>A <em>reference type</em> variable's name and value exists independently. And I do mean independent.</p> <p>An <em>instantiated</em> object may or may not have a reference to it.</p> <p>An <em>instantiated</em> object may have many references to it.</p> <p>A <em>declared</em> reference may or may not be <em>pointing to</em> an object.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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