1

How do the data members of the type get their default values when the default constructor is omitted and if they're given default values anyways what is the use of the default constructor in the first place ?

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person("SomeName");
        Console.WriteLine(person.Age);
        Console.WriteLine(person.FamileName == null);
    }
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string FamileName { get; set; }

    public Person(string name)
    {
        Name = name;
    }
}

Edit: This is from a book I've been reading and it's what got me thinking that the default constructor is responsible for giving the data members their default values but it seems that I got it wrong and it's only a way for me to modify these values if I needed to, please correct me if I'm wrong.

Every C# class is provided with a “freebie” default constructor that you can redefine if need be. By definition, a default constructor never takes arguments. After allocating the new object into memory, the default constructor ensures that all field data of the class is set to an appropriate default value

3 Answers3

0

Object fields are always initialized with default values if you won't specify differently using constructor or field initialization (same goes for properties).

Default value usualy corresponds to all bits set to zero (null for reference types, 0 for numeric types).

You can set other value using constructor:

public Person()
{
    Name = "Sebastian";
}

or initializer:

public string Name { get; set; } = "Sebastian";
bottaio
  • 4,963
  • 3
  • 19
  • 43
0

In C# you can assign default value.

class Person
{
    public string Name { get; set; } ="Someone";
    public int Age { get; set; }
    public string FamileName { get; set; }
}

If you want to assign the values in the initialization of object you can do this:

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person { Name= "SomeOne"; Age=16; };
        Console.WriteLine(person.Age);
        Console.WriteLine(person.FamileName == null);
    }
}

If you only need to assign values or default value, you do not need the constructor. You can do it in the above mention methods. I normally use constructors when I need to do some additional tasks e.g. database connection, setting up other object and etc.

Link: https://msdn.microsoft.com/en-us/library/bb397680.aspx

Amir
  • 1,722
  • 22
  • 20
0

The members in your class will be initialized with their default values. For reference types, this is null and for value types, it's the default value. There's nothing special about being members in an object - it's the same values they would have if they were in the middle of a function:

public void Foo()
{
    string name; // null
    int age; // 0
    bool isChild; // false
}

As for the default constructor, it's a nice place to make sure your data types are safe. For example, you might want to initialize any reference typed properites to a non-null value:

class Widget
{
    public string Name { get; set; }
    public Widget() 
    {
        Name = string.Empty; // this way someone can call Widget.Name safely
    }
}

On a related note, you should also see: How do you give a C# Auto-Property a default value?

Community
  • 1
  • 1
Jimmy
  • 27,142
  • 5
  • 87
  • 100