0

Note: I don't want to use records.

In the newer versions of C#, Is there any way to set properties in constructor definition like other languages such as Dart or Typescript?

Example :

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

In constructor instead of :

 public Person(string firstName, string lastName){
          FirstName = firstName;
          LastName  = lastName;
 }

Is there any feature like the following?

public Person(string this.FirstName, string this.LastName){
   
 }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nAviD
  • 2,784
  • 1
  • 33
  • 54
  • 4
    Have a look at [record types in C# 9](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#record-types) - it looks like you'd want `public record Person(string FirstName, string LastName);` (That's actually one of the specific examples...) – Jon Skeet Feb 21 '21 at 07:59
  • 1
    The documentation describes all the features in the language. Please feel free to post a question if you need help with a specific feature, but it's not very useful to ask the community to read the documentation for you. That said, as noted above, the new `record` type in C# will provide a much more concise way of declaring and using simple "data container" objects. See duplicates. – Peter Duniho Feb 21 '21 at 08:11
  • @JonSkeet yes I am familiar with records – nAviD Feb 21 '21 at 08:16
  • @PeterDuniho Sure, but didn't know what to search. I tried different search terms but did not get the answer. The article for constructors in C# in MS docs was dated back to 2017 – nAviD Feb 21 '21 at 08:18
  • _"The article for constructors in C# in MS docs was dated back to 2017"_ -- I'm not seeing how that's material. If there are no new changes to constructor, why would they update the article? You can find all the new features in recent versions here: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9. You can read about _all_ of the language features in the documentation. If it's not there, the language doesn't have that feature. The new `record` type is the closest thing to what you're asking about, so if that's not what you want, the short answer to your question is "no". – Peter Duniho Feb 21 '21 at 08:26
  • @nAviD: If you're familiar with records, which is the most obvious way of doing what you've asked for, you should say that in the question - along with what it *doesn't* do that you want. Note that the article I linked to also shows initialization with tuples, e.g. `public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName);`. If your question is really "does C# provide *exactly* the syntax I've asked for" then the answer is no, but that's not as interesting a question as "what facilities does C# provide for brevity in this area". – Jon Skeet Feb 21 '21 at 09:16
  • I found this question useful. The wording is not the same as any of the posted duplicates, and the proposed solution `go look at docs for something called records` is not obvious, especially for those of us coming from other languages. – Felipe Mar 12 '22 at 03:50

1 Answers1

0

You could use Object and Collection Initializers syntax without any constructor implementation to define a public filed at instantiation like the following

var p = new Person{
      FirstName="anyName",
      LastName ="lastName"
};

if the person class is immutable then you would use Record types from C# 9.0 as follwing

public record Person
{
    public string LastName { get; }
    public string FirstName { get; }

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

var p= new Person("firstName","LastName");
// or
var p2 = person with { FirstName = "firstName",LastName="lastName" };

You could only initialize a class through constructor or/and object initializer, and constructors does not support implicit initialization or declaration, so you have to write all things.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Ibram Reda
  • 1,882
  • 2
  • 7
  • 18