Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a C# Record Type?

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

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

However when I tried it, I get these errors

CS0116 A namespace cannot directly contain members such as fields or methods
CS1022 Type or namespace definition, or end-of-file expected
CS1514 { expected

How is this supposed to work?

Update: it's a feature of C# 9 https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/

like image 221
Jeremy Thompson Avatar asked Mar 29 '17 23:03

Jeremy Thompson


People also ask

What is declaration of variables in C?

A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable.

How do you declare variable variables?

In C, Java, ActionScript, etc, the type of a variable must be explicitly declared when the name is created. In Matlab, the type of the variable is inferred from the data put into the variable. A Value. A variable, by its very name, changes over time.


1 Answers

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

like image 191
DavidG Avatar answered Sep 23 '22 11:09

DavidG