Understanding Object Initialization
Looking at the Object Initialization Syntax
To help streamline the process of getting an object up and running, C# offers object initializer syntax.
Syntactically, an object initializer consists of a comma-delimited list of specified values, enclosed by the { and } tokens.
class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point()
{
}
static void Main(string[] args)
{
Point p = new Point { X = 30, Y = 30 }; // Object Initialization Syntax.
}
}
Behind the scenes, the type's default constructor is invoked, followed by setting the values to the specified properties.
Note :It's important to remember that the object initialization process is using the property setter implicitly. If the property setter is marked private, this syntax cannot be used.
Using init-Only Setters (New 9.0)
These setters enable a property to have its value set during initialization but after construction is complete on the object the property becomes read-only. These types of properties are call immutable.
class PointReadOnlyAfterCreation
{
public int X { get; init; }
public int Y { get; init; }
static void Main(string[] args)
{
PointReadOnlyAfterCreation p = new PointReadOnlyAfterCreation() { X = 3,Y=4 };
// ERROR!
// p.X = 2;
// p.Y = 3;
}
}
Calling Custom Constructors with Initialization Syntax
// The default constructor is called implicitly
Point p = new Point { X = 30, Y = 30 };
// The default constructor is called explicitly
Point p2 = new Point() { X = 30, Y = 30 };
Working with Constant and Read-Only Field Data
Understanding Constant Field Data
C# offers the "const" keyword to define constant data, which can never change after the initial assignment.
class MyMathClass
{
// implicitly -> static
public const double PI = 3.14;
static void Main(string[] args)
{
MyMathClass.PI = 4; // ERROR!
}
}
Constant fields of a class are implicitly "static".
Regardless of where you define a constant piece of data, the one point to always remember is that the initial value asssigned to the constant must be specified at the time you define constant.
class MyMathClass
{
public const double PI;
public MyMathClass()
{
PI = 3.14; // ERROR
}
}Constant Interpolated Strings
"const string" values can use string interpolation in their assignment statements as long as all of the componenets that are used are also "const strings".
const string foo = "foo";
const string bar = "bar";
const string foobar = $"{foo}{bar}";
Understanding Read-Only Fields
Like a constant, a read-only field can not be changed after the intial assignment or you will receive a compile-time error. However, unlike a constant, the value assigned to a read-only field can be determined of runtime and therefore can legally be assigned within the scope of a constructor but nowhere else.
class MyMathClass
{
public readonly double PI;
public MyMathClass()
{
PI = 3.14;
}
}Understanding Static Read-Only Fields
Unlike a constant field, read-only fields are not implicitly static. Thus, if you want to expose from the class level, you must explicitly use the "static keyword".
class MyMathClass
{
public static readonly double PI = 3.14;
}
However, if the value of a static read-only field is not known until runtime, you must use a static constructor.
class MyMathClass
{
public static readonly double PI;
static MyMathClass()
{
PI = 3.14;
}
}