Interviews1 answer
What is a primary constructor C# 6.0?
1 Answer
Primary constructor is introduced in C# 6.0.
It allows you to assign a value to an auto-property at declaration time only, just as with a field.
Without primary constructor:
public class Student
{
public int Id{ get; private set; }
public Name { get; private set; }
public Student(int id, string name)
{
Id = id;
Name = name;
}
}
With primary constructor:
public class Student
{
public Student(int id, string name)
{
public int Id{ get; } = id;
public Name { get;} = name;
}
}
Santosh Kumar AdidawarpuPosted May 8, 2016, 9:21 AM EDT
