Skip to content

.NET Glossary#

Terminology, Lingo, and Clarification for .NET.


Var Type / Implicitly Typed Local Variables#

Simple Definition:

  • Sets the variable type to whatever is first assigned.

Simple Why:

  • var x = new MyLongClassName<int>() reads better than
    MyLongClassName<int> x = new MyLongClassName<int>();.
  • Helps decouple design and write code faster at expense of some legibility if not careful.
  • Read more here.
How

Input:

var x = 1; // These lines are the same
int x = 1; // These lines are the same

var x;     // Error: Need to assign type

var x = 1;
x = "hi";  // Error: "hi" not an int

Value Type#

Simple Definition:

Simple Why:

image-20230930075842.png

How

Follow this code and examine i:

int i = 0;
Console.WriteLine(i);     // i = 0
ChangeValue(i);
Console.WriteLine(i);     // i = 0

static void ChangeValue(int i)
{
    i =  2;
    Console.WriteLine(i); // i = 2
}

For extra points, check it out in the debugger.

image-20230930084354.png

Notice how when we get out of scope, it switches back!

image-20230930084627.png

Reference Type#

Simple Definition:

Simple Why:

image-20230930083107.png

How

Objects (like this examples Student class) are references:

Student ed = new Student();
ed.studentName = "Ed";
Console.WriteLine(ed.studentName); // Ed
ChangeReferenceType(ed);
Console.WriteLine(ed.studentName); // Not Ed

static void ChangeReferenceType(Student student)
{
    student.studentName = "Not Ed";
}

class Student
{
    public string studentName;
}

Notice how trying to change just the string doesn't change the object!

Student ed = new Student();
ed.studentName = "Ed";
Console.WriteLine(ed.studentName); // Ed
ChangeStringReference(ed.studentName);
Console.WriteLine(ed.studentName); // Ed (!)
ChangeClassReference(ed);
Console.WriteLine(ed.studentName); // Not Ed

static void ChangeStringReference(string studentName)
{
    studentName = "Not Ed";
}

static void ChangeClassReference(Student student)
{
    student.studentName = "Not Ed";
}

class Student
{
    public string studentName;
}

So what's going on here?

C# Passes-By-Value (in this case, the memory address). Take a look on why object properties vs strings act different:

image-20230930101434.png

Read this for more context.


Here's a simplified version of our reference being Passed-By-Value:

string s = "hi!";
Console.WriteLine(s);     // s = "hi!"
ChangeValue(s);
Console.WriteLine(s);     // s = "hi!" ...wait, what?

static void ChangeValue(string s)
{
    s = "bye!";
    Console.WriteLine(s); // s = "bye!"
}

If Absolutely Necessary, we can resolve this by using ref which Passes-By-Reference.

string s = "hi!";
Console.WriteLine(s);     // s = "hi!"
ChangeValue(ref s);
Console.WriteLine(s);     // s = "bye!"

static void ChangeValue(ref string s)
{
    s = "bye!";
    Console.WriteLine(s); // s = "bye!"
}

Or we can defer to using a mutable object, like StringBuilder:

using System.Text;

StringBuilder s = new StringBuilder("hi!");
Console.WriteLine(s);     // s = "hi!"
ChangeValue(s);
Console.WriteLine(s);     // s = "bye!"

static void ChangeValue(StringBuilder s)
{
    s.Clear();
    s.Append("bye!");
    Console.WriteLine(s); // s = "bye!"
}

Apologies if I didn't do well explaining this, its fairly nuanced!

Boxing / Unboxing#

Simple Definition:

Simple Why:

How

When passing values to an object parameter:

using System.Collections;

ArrayList x = new ArrayList();

int myInt = 10;     // Int
x.Add(myInt);       // Boxing Int -> Object
int y = (int) x[0]; // Unboxing Object -> Int Explicitly

In our debugger, we can see this the value -> reference -> value nastiness:

image-20230930135754.png


Console.WriteLine#

Simple Definition:

  • Writes to the console & adds a newline.
  • Basically Console.Write with a newline at the end.

Simple Why:

  • Console.WriteLine() beats Console.Write() as you'll usually want to add a return.
  • Allows platform agnostic newlines, since each OS uses different control characters. (1)
  1. MacOS only uses the return or carriage return character \r, Linux only uses the newline or line feed character \n, and Windows uses BOTH \r\n.
How

Input:

System.Console.WriteLine("Hello");
System.Console.WriteLine("World");

Output:

Hello
World
Note: The World will have a newline at the end as well.

There are a few ways to slam variables into a string, you should know all of them!

All 3 ways deliver this output:

Output:

Ed
Round 1: 2
Round 2: 3
Total: 5

Concatenation:

string name = "Ed";
int scoreOne = 2;
int scoreTwo = 3;
Console.WriteLine(name + "\nRound 1: " + scoreOne + "\nRound 2: " + scoreTwo + "\nTotal: " + (scoreOne + scoreTwo));

Composite Formatting:

string name = "Ed";
int scoreOne = 2;
int scoreTwo = 3;
Console.WriteLine("{0}\nRound 1: {1}\nRound 2: {2}\nTotal: {3}", name, scoreOne, scoreTwo, (scoreOne + scoreTwo));

String Interpolation:

string name = "Ed";
int scoreOne = 2;
int scoreTwo = 3;
Console.WriteLine($"{name}\nRound 1: {scoreOne}\nRound 2: {scoreTwo}\nTotal: {scoreOne + scoreTwo}");

String interpolation is usually the most expressive & shortest:

image-20230930160754.png

It also works well with verbatim / raw strings!

Look, just click this link and pay attention to this picture:

image-20230930162336.png