How to declare a C# Record Type?

前端 未结 6 1547
日久生厌
日久生厌 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;
        }
    }
    

提交回复
热议问题