ASP.NET MVC: Code Nuggets

<% %>, <%= %>, and <%: %> have special meaning:

Code enclosed within <% %> will be executed
Code enclosed within <%= %> will be executed, and the result will be outputted to the page
Code enclosed within <%: %> will be executed, and the result will be outputted to the page, as well as being HTML encoded

If you’re on ASP.NET 4 or later, you’ll want to use the <%: %> syntax vs. the <%= %> syntax, as the result gets HTML encoded.

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#: access modifiers

Members default to private when no access modifier is present.

public: accessible from an object variable as well as derived classes
private: accessible only from the class that has defined the method. In C#, all members are private by default
protected: accessible by the defining class and derived classes. Protected methods are not accessible from an object variable.
internal: accessible by any type in the assembly, but not outside the assembly
protected internal: access is limited to the current assembly or types derived from the defining class in the current assembly

internal or internal protected members are really only useful when creating code libraries.

Types (classes, interfaces, structures, enumerations, delegates) are limited to public or internal; private may be used when the type is nested. internal is the default accessibility for types in C#.

.NET: String Format Characters

C or c: currency based on local cultural symbol, $1234.56
D or d: decimal, optional number (i.e. {0:D8}) used to pad value, 012345678
E or e: exponential notation, 1.234E+05
F or f: fixed point, 0:F3 1234.567
G or g: general, used to format to fixed or exponential
N or n: numerical format (with commas), 1,234.00
X or x: hexadecimal, 0:X 123F, 0:x 123f

You can use this with string.Format, like below:

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.