Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record type with multiple constructors

Tags:

c#

.net

c#-9.0

How do I create multiple constructors for a record type in C#?

I created a record type like this:

public record Person(int Id, string FirstName, string LastName)

Now I want to introduce another constructor overload with no parameters, how can I do that? In a normal class I would do something like this:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Person()
    {
        
    }

    public Person(int id, string firstName, string lastName)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
    }
}
like image 222
Nadar Avatar asked May 15 '21 13:05

Nadar


People also ask

Can records have constructors?

If you want your record's constructor to do more than initialize its private fields, you can define a custom constructor for the record. However, unlike a class constructor, a record constructor doesn't have a formal parameter list; this is called a compact constructor.

Can one class have multiple constructors?

There can be multiple constructors in a class. However, the parameter list of the constructors should not be same. This is known as constructor overloading.

What is it called when you have multiple constructors?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

Can we have 2 constructors in C#?

Prerequisite: Constructors in C# A user can implement constructor overloading by defining two or more constructors in a class sharing the same name. C# can distinguish the constructors with different signatures. i.e. the constructor must have the same name but with different parameters list.


4 Answers

The selected answer works for this simple case where all the members are simple types. With reference types you usually want to call their constructors etc.

The right solution is to simply add the constructor you want like this:

record Rank(int level, string description);

record Manager(string FirstName, Rank rank) {
  public Manager() : this("", new(0, "Entry")) { }

  // public Manager(string FirstName, Rank rank) auto generated by compiler
}
                          
like image 158
kofifus Avatar answered Oct 17 '22 15:10

kofifus


Use optional arguments.

public record Person(int Id = default, string FirstName = null, string LastName = null);
like image 21
Mustafa Arslan Avatar answered Oct 17 '22 16:10

Mustafa Arslan


You can write your code like below:

public record Person
{
    public int Id { get; init; }
    public string FirstName { get; init; }
    public string LastName { get; init; }
    //constructor
    public Person()
    {
        //init or do something
    }
    //overload constructor
    public Person(int id, string firstName, string lastName)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
    }
}
like image 44
MauNguyenVan Avatar answered Oct 17 '22 15:10

MauNguyenVan


you can write it like this:

public record Person(int Id,string FirstName,string LastName){
   public Person(YourDto item):this(item.Id,item.FirstName,item.LastName){} 
}

so, in the constructor you can pass your Dto item.

like image 35
Pouria Montakhabi Avatar answered Oct 17 '22 16:10

Pouria Montakhabi