How to declare a C# Record Type?

前端 未结 6 1544
日久生厌
日久生厌 2021-02-06 23:22

I read on a blog that C# 7 will feature record types

class studentInfo(string StudentFName, string StudentMName, string StudentLName);

However

相关标签:
6条回答
  • 2021-02-06 23:59

    Prior to C# 7.0 you declare classes like this:

    Although record types have not been implemented yet in C# 7.0 (as detailed by the other answers), you can shorten your code by using read-only auto-properties, introduced in C# 6.0:

    public class studentInfo
    {
        public string StudentFName { get; }
        public string StudentMName { get; }
        public string StudentLName { get; }
    
        public studentInfo(string strFN, string strMN, string strLN)
        {
            StudentFName = strFN;
            StudentMName = strMN;
            StudentLName = strLN;
        }
    }
    
    0 讨论(0)
  • 2021-02-07 00:01

    C#9 is now available and records with it.

    Here are some example:

    public record Person
    {
        public string LastName { get; init; }
        public string FirstName { get; init; }
    
        public Person(string first, string last) => (FirstName, LastName) = (first, last);
    }
    

    With the use of positional records (and inheritance):

    public record Teacher(string FirstName, string LastName,
        string Subject)
        : Person(FirstName, LastName);
    
    public sealed record Student(string FirstName,
        string LastName, int Level)
        : Person(FirstName, LastName);
    
    var person = new Person("Bill", "Wagner");
    
    var (first, last) = person;
    Console.WriteLine(first);
    Console.WriteLine(last);
    

    Finally, records support with-expressions. A with-expression instructs the compiler to create a copy of a record, but with specified properties modified:

    Person brother = person with { FirstName = "Paul" };
    

    Everything you need to know about every new features including records here

    0 讨论(0)
  • 2021-02-07 00:02

    Update:

    C# 9 now contains record types.

    public record Person
    {
        public string LastName { get; }
        public string FirstName { get; }
    
        public Person(string first, string last) => (FirstName, LastName) = (first, last);
    }
    

    Old answer:

    Record types are not (yet) implemented in C#. See the proposal in the official GitHub repository:

    https://github.com/dotnet/csharplang/blob/master/proposals/records.md

    Discuss or vote at https://github.com/dotnet/csharplang/issues/39

    0 讨论(0)
  • 2021-02-07 00:03

    [Rewritten to reflect the current state of things]

    To add to the other answers, you can easily track when C# features are pencilled in to appear in C# these days. For example, the Champion "Records" issue shows the state of thinking around records. Records is now scheduled for C# 9. But that feature was previously touted for C# 6, C# 7 and C# 8 too, so it remains only an aspiration.

    0 讨论(0)
  • 2021-02-07 00:04

    There is a way if you're looking for record-like, lazy way of implementing constructors using currently available C# 7 functionality:

    class Lazystructor
    {
        int a;
        float b;
        WebClient c;
    
        public Lazystructor(int a, float b, WebClient c)
            =>  (this.a, this.b, this.c) = (a, b, c);
    }
    

    A bit of consideration if you're using this on a performance-sensitive scenario, as it's a process of ValueTuple creation and extraction.

    0 讨论(0)
  • 2021-02-07 00:10

    Record types were on the roadmap for C# 7.0, but were ultimately delayed until a later version of the language.

    To quote Mads Torgersen in reply to this blog post,

    [Primary constructors] are still on the radar, along with the related concept of record types (which we considered for C# 7.0), and I am hopeful that we can land on a better design – maybe one that encompasses both.

    As of C# 7's release, the GitHub proposal for this language feature still indicates that the implementation is "In Progress."

    0 讨论(0)
提交回复
热议问题