Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Object Constructor - shorthand property syntax

Tags:

c#

object

class

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?

like image 288
Schotime Avatar asked Feb 03 '09 10:02

Schotime


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

like image 170
grimdog_john Avatar answered Sep 30 '22 13:09

grimdog_john


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.

like image 31
Andy White Avatar answered Sep 30 '22 12:09

Andy White


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.

like image 43
Matt Hamilton Avatar answered Sep 30 '22 12:09

Matt Hamilton