AleksP AleksP - 2 months ago 5x
C# Question

How to name tuple properties?

How and "could be" organized return from method which returns tuple type with name of paramiters,
as an exapmle

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
not like

Is this possible or not?

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


You need to declare a helper class to do so.

public class MyResult
    public string Nick { 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.