C#: static constructors, static classes

If you want to dynamically set a static member (i.e. read it from a database or file), you can make use of a static constructor. Here are some interesting notes about static constructors:

  • The static constructor runs before any instance constructors run.
  • Static constructors don’t take an access modifier and can’t take parameters.
  • Static constructors execute only one time, no matter how many instances are created.
  • Static constructors get called when an instance of the class is created or before the first static member of the class is accessed.
  • A class or structure can define only 1 static constructor.

C#: static keyword

Use the static keyword for members of a type that are so common that there isn’t a need to create an instance of the type. A few notes on on static data:

  • Static members can only operate on other static members.
  • Static data is allocated once and shared among all instances of the same type.
  • A non-static method can make use of non-static and static data. Static data is available to all instances of the type.
  • If you assign a static member in a constructor, every time a new instance is created, the static member gets reset.

C#: Read-only fields

The C# keyword readonly is used when you don’t know a value at compile time, but you want that value to never change once it’s been assigned. Read-only fields aren’t static like constants are; if you’d like to make them static you must include the static keyword.

An interesting fact about readonly fields is that their value can be assigned from within a constructor. An example of this is when you need to read a value from the database.

C#: Constants

A few notes about constants in C#:

  1. Once a constant has been assigned a value, you can’t change it’s value or you’ll receive a compile error.
  2. Constants may not be used as return values.
  3. The value assigned to a constant must be known at compile time.
  4. Constant fields are static and thus need to be prefixed with the type name. However if referencing a constant within the current type or member, you don’t need to prefix it with the type name.

C#: Default Values for member variables

5 simple rules to remember for the default values that class member variables get:

  1. Numeric data is set to 0 or 0.0
  2. char types are set to ‘\0’
  3. bool types are set to false
  4. string types are set to null
  5. Reference types are set to null

This is at the class level. When within method scope, you must assign an initial value before using a variable as they don’t receive a default assignment. The only exception is when using a variable as an output parameter (out keyword).

If you’d like to override the default values, you can assign a type’s member variable at the time of declaration:

Be aware that members are assigned before the constructor gets called. That means that if you give a member a default value but then assign it in the constructor, it cancels out the initial assignment.

C#: default constructor

Default constructor ensures that all member data is set to a default value (all constructors do this).

Once you define a custom constructor, the free default constructor is removed. You must redefine it if you want to be able to create objects using it.

C#: creating objects, new keyword

You can’t allocate classes on the stack in C#, unlike C++.

The new keyword calculates the number of bytes needed for the specified object and allocates enough memory from the managed heap.

C# object variables are a reference to the object in memory, not the object itself.

C#: System.Environment

Here are a few handy properties and methods that are found on the System.Environment class:

Current OS: System.Environment.OSVersion

Current working directory: System.Environment.CurrentDirectory

string[] of drives on the machine: System.Environment.GetLogicalDrives()

Version of .NET app is running under: System.Environment.Version

Current machine name: System.Environment.MachineName

Newline symbol for the current environment: System.Environment.NewLine

Number of processors on the current machine: System.Environment.ProcessorCount

Full path to system directory: System.Environment.SystemDirectory

User name application is running under: System.Environment.UserName

C#: Variations on the Main() method, System.Environment.GetCommandLineArgs()

Below are a few different variations that can be done on the Main method.

Return type is void, string array as argument:

Return type is void, no arguments:

Return type is int, no arguments:

Return type is int, string array as argument:

Besides accessing them via the ‘string[] args’ parameter, you can access command-line arguments using

When you do this, you are not required to define the Main() method as taking a string array.