11

A few months ago I read about a technique so that if there parameters you passed in matched the local variables then you could use some short hand syntax to set them. To avoid this:

public string Method(p1, p2, p3)
{
    this.p1 = p1;
    this.p2 = p2;
    this.p3 = p3;
}

Any ideas?

ono2012
  • 4,967
  • 2
  • 33
  • 42
Schotime
  • 15,707
  • 10
  • 46
  • 75
  • Are you thinking of this shorthand syntax from C++? https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Frucifer Apr 23 '18 at 20:51

3 Answers3

30

You may be thinking about the new object initializer syntax in C# 3.0. It looks like this:

var foo = new Foo { Bar = 1, Fizz = "hello" };

So that's giving us a new instance of Foo, with the "Bar" property initialized to 1 and the "Fizz" property to "hello".

The trick with this syntax is that if you leave out the "=" and supply an identifier, it will assume that you're assigning to a property of the same name. So, for example, if I already had a Foo instance, I could do this:

var foo2 = new Foo { foo1.Bar, foo1.Fizz };

This, then, is getting pretty close to your example. If your class has p1, p2 and p3 properties, and you have variables with the same name, you could write:

var foo = new Foo { p1, p2, p3 };

Note that this is for constructing instances only - not for passing parameters into methods as your example shows - so it may not be what you're thinking of.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • You could be right Matt although i'm not certain. haha. really need to save these things when i find them next time. cheers. – Schotime Feb 03 '09 at 10:38
  • Are you sure this syntax is valid? `var foo2 = new Foo { foo1.Bar, foo1.Fizz };` I know this works in JavaScript, but I'm not finding anything in the C# reference that says this is valid syntax. I've tried several code examples and have not been able to produce anything that compiles. – Kevin Bowersox Jan 26 '22 at 10:38
  • Here is a small example of the code not compiling: https://gist.github.com/kmb385/ed076b32e5666f8fb5b8a563d9cee42a – Kevin Bowersox Jan 26 '22 at 10:45
  • Indeed, projection initializers aren't valid for named types. – Jon Skeet Jan 26 '22 at 11:21
14

There's an even easier method of doing this in C# 7 - Expression bodied constructors.

Using your example above - your constructor can be simplified to one line of code. I've included the class fields for completeness, I presume they would be on your class anyway.

private string _p1;
private int _p2;
private bool _p3;  

public Method(string p1, int p2, bool p3) => (_p1, _p2, _p3) = (p1, p2, p3);

See the following link for more info :-

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

grimdog_john
  • 675
  • 1
  • 7
  • 15
3

You might be thinking of the "object initializer" in C#, where you can construct an object by setting the properties of the class, rather than using a parameterized constructor.

I'm not sure it can be used in the example you have since your "this" has already been constructed.

Andy White
  • 86,444
  • 48
  • 176
  • 211