2

Say I have some code like below:

public class Person
{
    public string Name, Job;
    public int Age;
    public Person(string name, int age, string job)
    {
        Name = name;
        Age = age;
        Job = job;
    }
}

Is there a shorter way to do this instead of doing "[Variable] = [Argument]" for every argument?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 7
    3 lines of simplest code possible are too much? You should use properties instead of public fields though – Tim Schmelter Sep 15 '17 at 11:29
  • 2
    @TimSchmelter this could also be a simplified example of a class with 100 properties – fubo Sep 15 '17 at 11:30
  • 4
    Change the variable names to N, A & J & the parameter names to n, a & j. – PaulF Sep 15 '17 at 11:30
  • 4
    @fubo: i have never seen a (well designed) class that requires 100 parameters in the constructor – Tim Schmelter Sep 15 '17 at 11:31
  • 1
    If they would just add a feature in VS that would automatically generate the constructor for you, that would be a big step forward. – Yair Halberstadt Sep 15 '17 at 11:38
  • @YairHalberstadt they have. Type Ctrl+Ins and select "Create Constructor". Or is it Ctrl+. ? Whatever the shortcut for the suggestions is – Panagiotis Kanavos Sep 15 '17 at 11:38
  • @PanagiotisKanavos [Sarcasm](https://en.wikipedia.org/wiki/Sarcasm) - Not everybody recognizes it when they see it. – Fildor Sep 15 '17 at 11:39
  • @elseytd **why** are you looking for this? What is the *actual* problem you want to solve? Maybe it's already solved, maybe you are doing something wrong like having too many fields. Maybe you should be looking for an array – Panagiotis Kanavos Sep 15 '17 at 11:40
  • 1
    https://stackoverflow.com/a/41974829/34092 - `Whether you should do this is a whole different question though` – mjwills Sep 15 '17 at 11:40
  • @elseytd you *can* shorten it to a single line right now, but only if you have a single field in C# 7.1 : `Person(string name) => Name=name;`, Not a very good idea. This will change in C# 8 with primary constructors. Do you *really* need it though? – Panagiotis Kanavos Sep 15 '17 at 11:42
  • @Fildor unjustified - the feature wasn't there before Roslyn. It was a *ReSharper-only* feature. Right now I'm not quite certain if the generator is built-in or part of the Productivity Powertools – Panagiotis Kanavos Sep 15 '17 at 11:47
  • @PanagiotisKanavos That may be true, but unjustified or not - I am *sure* Yair *was* being sarcastic ... :) – Fildor Sep 15 '17 at 11:49
  • 1
    @mjwills you are *evil*. I can't unsee this! – Panagiotis Kanavos Sep 15 '17 at 11:53

2 Answers2

3

Use an object initializer

Since you properties are public, you can set them right after the instantiation:

var person = new Person
{
    Name = value1,
    Age = value2,
    Job = value3
}

The class itself will be:

public string Name, Job {get; set;}
public int Age {get; set;}
public Person()
{
}

P.s. don't use public fields

enkryptor
  • 1,574
  • 1
  • 17
  • 27
  • (basically : use an 'object initializer') https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers – Timothy Groote Sep 15 '17 at 11:33
1

Since there are no checks, why don't you just omit parameters in the constructor, transfigurate fields into the properties, and leave their init to the calling code?

Person person = new Person()
{
    Name = "elseytd",
    Age = 24,
    Job = Jobs.Programmer
};
AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • 2
    Doesn't that end up with more code - every instantiation you need to specify the field names as well as the values - essentially duplicating the contents of the constructor each time.. It also leads to the possibility of not correctly instantiating if all parameters are required. – PaulF Sep 15 '17 at 11:35
  • 1
    @PaulF, it ends up being the **same** amount of code, but much less in the constuctor of the class, which, presumably, is just a DTO. It also adds some explicitness to the initialization, meaning you 100% won't swap some two strings by a mistake. – AgentFire Sep 15 '17 at 11:36
  • @mjwills there is another neat thing for doing that for a programmer, [AutoMapper](http://automapper.org/). – AgentFire Sep 15 '17 at 11:44
  • Great idea @AgentFire ! – mjwills Sep 15 '17 at 11:45