Structs and Constructors.

Did you know that the Value Types ( Structs) cannot have parameterless Constructors?

For example the code below generates a compilation error:
"Structs cannot contain explicit parameterless constructors"

internal struct StructExample
{
public StructExample()
{
}
}

However, you can define parameterless constructors in structs by specifying "static" keyword.

internal struct StructExample
{
static StructExample()
{
}
}

No Access modifier is specified, because we "Cannot specify any access modifier for a static Constructor". By Default, they have private access modifier.

Comments

Popular posts from this blog

Knowing too much is bad(in the world of OOP)

Reverse a LinkedList Algorithm

DateTime.Now in C#