Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name tuple properties?

Tags:

c#

tuples

How and "could be" organized return from the method which returns tuple type with the name of parameters, as an example

private static Tuple<string, string> methodTuple() {     return new {Name = "Nick", Age = "Twenty"}; /*exception because need to new Tuple<string, string>(){Item1 = "Nick", Item2 = "Twenty"}o*/ } 

and call parameters like methodTuple.Name not like methodTuple.Item1....N

Is this possible or not?

UPD: I want to create object with named parameters without new named type.

like image 282
AleksP Avatar asked Jan 12 '15 18:01

AleksP


People also ask

How do you name a tuple?

Creating a Named Tuple. To create a named tuple, import the namedtuple class from the collections module. The constructor takes the name of the named tuple (which is what type() will report), and a string containing the fields names, separated by whitespace. It returns a new namedtuple class for the specified fields.

Can you name a tuple in C#?

Starting C# v7. 0, it is now possible to name the tuple properties which earlier used to default to names like Item1 , Item2 and so on.

Are tuples reference types?

Tuple types are reference types. System. ValueTuple types are mutable.

Is tuple with 9 elements?

If you want to include more than eight elements in a tuple, you can do that by nesting another tuple object as the eighth element. The last nested tuple can be accessed using the Rest property. To access the nested tuple's element, use the Rest.


2 Answers

In C# 7.0 (Visual Studio 2017) there is a new option to do that:

(string first, string middle, string last) LookupName(long id) 
like image 52
MichaelMocko Avatar answered Sep 22 '22 05:09

MichaelMocko


You need to declare a helper class to do so.

public class MyResult {     public string Name { get; set; }     public string Age { get; set; } } 

What you're trying to return is an anonymous type. As the name suggests you don't know what its name is, so you can't declare your method to return it.

Anonymous Types (C# Programming Guide)

You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Update

C#7 introduces Tuple support built into the language and it comes with named tuples

(string name, int age) methodTuple() {     (...) } 

Read more on docs.microsoft.com: https://docs.microsoft.com/en-us/dotnet/articles/csharp/csharp-7#tuples

like image 21
MarcinJuraszek Avatar answered Sep 22 '22 05:09

MarcinJuraszek