Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ValueTuple properties naming

I'm trying ValueTuple Class in C#, and i have a doubt about properties naming, let's see:

If instantiate a ValueTuple declaring the object like this: var tuple1 = (Name: "Name1", Age: 25); We can name the properties,

but, like this: ValueTuple<string,int> tuple2 = (Name: "Name1", Age: 25);

we get a warning that says the names are ignored,so

We should type: ValueTuple<string,int> tuple2 = ("Name1",25);

Then the properties will be tuple2.Item1 and tuple2.Item2

Can someone explain this newbie the reason about this?

Than you in advantage

like image 898
Ferus7 Avatar asked Oct 06 '17 08:10

Ferus7


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 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?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

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.


2 Answers

There are two different "kinds" of tuples involved here: the .NET ValueTuple<...> types, and C# tuple types. C# tuple types can have element names; ValueTuple<...> types can't. (There's simply nowhere in the type system for that to live in a normal way.)

At execution time, a value is only aware of its .NET type. For example, if you use:

(string x, int y) tuple1 = ("hello", 10);
(string a, int b) tuple2 = ("hello", 10);
DoSomethingWithValue(tuple1);
DoSomethingWithValue(tuple2);
...
void DoSomethingWithValue(object value) { ... }

then DoSomethingWithValue wouldn't be able to distinguish between the two tuple values at all. The information is only available at compile-time.

Information about element names for things like parameters and return types is propagated via attributes that the C# compiler consumes. So for example, a method declared like this:

public (string name, int score) GetFoo()

is compiled in IL as if it had been declared like this:

[TupleElementNames(new string[] { "name", "score" }]
public ValueTuple<string, int> GetFoo()

The C# language defines appropriate conversions between the .NET types and the C# tuple types to make it as seamless as possible.

In terms of how to use tuples, I'd use the C# tuple types as far as you can. Naming the tuple elements makes a huge difference in usability.

like image 116
Jon Skeet Avatar answered Oct 09 '22 19:10

Jon Skeet


What you want to define is

(string Name,int Age) tuple1 = (Name: "Name1", Age: 25);

what you are defining is

ValueTuple<string,int> tuple2 = (Name: "Name1", Age: 25);

Look at the difference between in Type you are defining. Both are two different things if you take the name.

like image 31
Anup Sharma Avatar answered Oct 09 '22 20:10

Anup Sharma