C# 在元组类中命名比“;第1项&引用;项目2“;
有没有一种方法可以使用元组类,但提供其中项目的名称 例如:C# 在元组类中命名比“;第1项&引用;项目2“;,c#,c#-4.0,tuples,C#,C# 4.0,Tuples,有没有一种方法可以使用元组类,但提供其中项目的名称 例如: public Tuple<int, int, int int> GetOrderRelatedIds() public元组getOrderRelatedId() 返回OrderGroupId、OrderTypeId、OrderSubjectId和OrderRequirementId的ID 最好让我的方法的用户知道哪个是哪个。(调用该方法时,结果为result.Item1、result.Item2、result.Item3
public Tuple<int, int, int int> GetOrderRelatedIds()
public元组getOrderRelatedId()
返回OrderGroupId、OrderTypeId、OrderSubjectId和OrderRequirementId的ID
最好让我的方法的用户知道哪个是哪个。(调用该方法时,结果为result.Item1、result.Item2、result.Item3、result.Item4。不清楚哪个是哪个。)
(我知道我可以创建一个类来保存所有这些ID,但如果这些ID已经有了自己的类,那么为这个方法的返回值创建一个类似乎很愚蠢。)直到C#7.0,除了定义自己的类型之外,没有其他方法可以做到这一点。使用.net 4,您可能会看到,不要在这个简单的情况下使用它,因为编译时错误会变成运行时错误
class Program
{
static void Main(string[] args)
{
dynamic employee, manager;
employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
manager = new ExpandoObject();
manager.Name = "Allison Brown";
manager.Age = 42;
manager.TeamSize = 10;
WritePerson(manager);
WritePerson(employee);
}
private static void WritePerson(dynamic person)
{
Console.WriteLine("{0} is {1} years old.",
person.Name, person.Age);
// The following statement causes an exception
// if you pass the employee object.
// Console.WriteLine("Manages {0} people", person.TeamSize);
}
}
// This code example produces the following output:
// John Smith is 33 years old.
// Allison Brown is 42 years old.
另外值得一提的是方法中的,但是如果要返回它,需要创建一个类
var MyStuff = new
{
PropertyName1 = 10,
PropertyName2 = "string data",
PropertyName3 = new ComplexType()
};
不,您不能命名元组成员
介于两者之间的是使用元组而不是元组。以下是一个过于复杂的问题:
class MyTuple : Tuple<int, int>
{
public MyTuple(int one, int two)
:base(one, two)
{
}
public int OrderGroupId { get{ return this.Item1; } }
public int OrderTypeId { get{ return this.Item2; } }
}
class MyTuple:Tuple
{
公共MyTuple(int-one,int-two)
:底座(一个,两个)
{
}
public int-OrderGroupId{get{返回this.Item1;}}
public int OrderTypeId{get{返回this.Item2;}}
}
为什么不创建一个类呢?我想我应该创建一个类,但另一个选择是输出参数
public void GetOrderRelatedIds(out int OrderGroupId, out int OrderTypeId, out int OrderSubTypeId, out int OrderRequirementId)
因为元组只包含整数,所以可以用字典
var orderIds=新字典{
{“OrderGroupId”,1},
{“OrderTypeId”,2},
{“ordersubiteid”,3},
{“OrderRequirementId”,4}。
};
但是我也不建议这样做。如果您的物品类型都不同,那么我制作了一个类来更直观地获取它们
此类的用法:
var t = TypedTuple.Create("hello", 1, new MyClass());
var s = t.Get<string>();
var i = t.Get<int>();
var c = t.Get<MyClass>();
var t=TypedTuple.Create(“hello”,1,new MyClass());
var s=t.Get();
var i=t.Get();
var c=t.Get();
源代码:
public static class TypedTuple
{
public static TypedTuple<T1> Create<T1>(T1 t1)
{
return new TypedTuple<T1>(t1);
}
public static TypedTuple<T1, T2> Create<T1, T2>(T1 t1, T2 t2)
{
return new TypedTuple<T1, T2>(t1, t2);
}
public static TypedTuple<T1, T2, T3> Create<T1, T2, T3>(T1 t1, T2 t2, T3 t3)
{
return new TypedTuple<T1, T2, T3>(t1, t2, t3);
}
public static TypedTuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 t1, T2 t2, T3 t3, T4 t4)
{
return new TypedTuple<T1, T2, T3, T4>(t1, t2, t3, t4);
}
public static TypedTuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
{
return new TypedTuple<T1, T2, T3, T4, T5>(t1, t2, t3, t4, t5);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6>(t1, t2, t3, t4, t5, t6);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6, T7>(t1, t2, t3, t4, t5, t6, t7);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8>(t1, t2, t3, t4, t5, t6, t7, t8);
}
}
public class TypedTuple<T>
{
protected Dictionary<Type, object> items = new Dictionary<Type, object>();
public TypedTuple(T item1)
{
Item1 = item1;
}
public TSource Get<TSource>()
{
object value;
if (this.items.TryGetValue(typeof(TSource), out value))
{
return (TSource)value;
}
else
return default(TSource);
}
private T item1;
public T Item1 { get { return this.item1; } set { this.item1 = value; this.items[typeof(T)] = value; } }
}
public class TypedTuple<T1, T2> : TypedTuple<T1>
{
public TypedTuple(T1 item1, T2 item2)
: base(item1)
{
Item2 = item2;
}
private T2 item2;
public T2 Item2 { get { return this.item2; } set { this.item2 = value; this.items[typeof(T2)] = value; } }
}
public class TypedTuple<T1, T2, T3> : TypedTuple<T1, T2>
{
public TypedTuple(T1 item1, T2 item2, T3 item3)
: base(item1, item2)
{
Item3 = item3;
}
private T3 item3;
public T3 Item3 { get { return this.item3; } set { this.item3 = value; this.items[typeof(T3)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4> : TypedTuple<T1, T2, T3>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4)
: base(item1, item2, item3)
{
Item4 = item4;
}
private T4 item4;
public T4 Item4 { get { return this.item4; } set { this.item4 = value; this.items[typeof(T4)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5> : TypedTuple<T1, T2, T3, T4>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
: base(item1, item2, item3, item4)
{
Item5 = item5;
}
private T5 item5;
public T5 Item5 { get { return this.item5; } set { this.item5 = value; this.items[typeof(T5)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6> : TypedTuple<T1, T2, T3, T4, T5>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
: base(item1, item2, item3, item4, item5)
{
Item6 = item6;
}
private T6 item6;
public T6 Item6 { get { return this.item6; } set { this.item6 = value; this.items[typeof(T6)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6, T7> : TypedTuple<T1, T2, T3, T4, T5, T6>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
: base(item1, item2, item3, item4, item5, item6)
{
Item7 = item7;
}
private T7 item7;
public T7 Item7 { get { return this.item7; } set { this.item7 = value; this.items[typeof(T7)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8> : TypedTuple<T1, T2, T3, T4, T5, T6, T7>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8)
: base(item1, item2, item3, item4, item5, item6, item7)
{
Item8 = item8;
}
private T8 item8;
public T8 Item8 { get { return this.item8; } set { this.item8 = value; this.items[typeof(T8)] = value; } }
}
公共静态类TypedTuple
{
公共静态类型双向创建(T1)
{
返回新的TypedTuple(t1);
}
公共静态类型双向创建(T1 T1、T2 T2)
{
返回新的TypedTuple(t1,t2);
}
公共静态类型双向创建(T1 T1、T2 T2、T3)
{
返回新的TypedTuple(t1、t2、t3);
}
公共静态类型复制创建(T1 T1、T2 T2、T3 T3、T4 T4)
{
返回新的TypedTuple(t1、t2、t3、t4);
}
公共静态类型复制创建(T1 T1、T2 T2、T3 T3、T4 T4、T5 T5)
{
返回新的TypedTuple(t1、t2、t3、t4、t5);
}
公共静态类型复制创建(T1 T1、T2 T2、T3 T3、T4 T4、T5 T5、T6 T6)
{
返回新的TypedTuple(t1、t2、t3、t4、t5、t6);
}
公共静态类型复制创建(T1 T1、T2 T2、T3 T3、T4 T4、T5 T5、T6 T6、T7 T7)
{
返回新的TypedTuple(t1、t2、t3、t4、t5、t6、t7);
}
公共静态类型复制创建(T1 T1、T2 T2、T3 T3、T4 T4、T5 T5、T6 T6、T7 T7、T8 T8)
{
返回新的TypedTuple(t1、t2、t3、t4、t5、t6、t7、t8);
}
}
公共类TypedTuple
{
受保护的字典项=新建字典();
公共类型双(T项1)
{
第1项=第1项;
}
公共TSource Get()
{
目标价值;
if(this.items.TryGetValue(typeof(TSource),out值))
{
返回(TSource)值;
}
其他的
返回默认值(TSource);
}
私人T项目1;
public T Item1{get{return this.Item1;}set{this.Item1=value;this.items[typeof(T)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项1、T2项2)
:基本(项目1)
{
第2项=第2项;
}
私人T2项目2;
public T2 Item2{get{返回this.Item2;}set{this.Item2=value;this.items[typeof(T2)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项1、T2项2、T3项3)
:基本(项目1、项目2)
{
第3项=第3项;
}
私人T3项目3;
public T3 Item3{get{返回this.Item3;}set{this.Item3=value;this.items[typeof(T3)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项1、T2项2、T3项3、T4项4)
:基本(项目1、项目2、项目3)
{
第4项=第4项;
}
私人T4项目4;
public T4 Item4{get{返回this.Item4;}set{this.Item4=value;this.items[typeof(T4)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项1、T2项2、T3项3、T4项4、T5项5)
:基本(项目1、项目2、项目3、项目4)
{
第5项=第5项;
}
私人T5项目5;
public T5 Item5{get{返回this.Item5;}set{this.Item5=value;this.items[typeof(T5)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项目1、T2项目2、T3项目3、T4项目4、T5项目5、T6项目6)
:基本(项目1、项目2、项目3、项目4、项目5)
{
第6项=第6项;
}
私人T6项目6;
public T6 Item6{get{返回this.Item6;}set{this.Item6=value;this.items[typeof(T6)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项目1、T2项目2、T3项目3、T4项目4、T5项目5、T6项目6、T7项目7)
:基本(项目1、项目2、项目3、项目4、项目5、项目6)
{
第7项=第7项;
}
私人T7项目7;
public T7 Item7{get{返回this.Item7;}set{this.Item7=value;this.items[typeof(T7)]=value;}
}
公共类TypedTuple:TypedTuple
{
公共类型双(T1项1、T2项2、T3项3、T4项4、T5项5、T6项6、T7项7、T8项8)
:基本(项目1、项目2、项目3、项目4、项目5、项目6、项目7)
{
第8项=第8项;
}
私人T8项目8;
公共T8 Item8{get{返回this.Item8;}set{this.ite
public static class TypedTuple
{
public static TypedTuple<T1> Create<T1>(T1 t1)
{
return new TypedTuple<T1>(t1);
}
public static TypedTuple<T1, T2> Create<T1, T2>(T1 t1, T2 t2)
{
return new TypedTuple<T1, T2>(t1, t2);
}
public static TypedTuple<T1, T2, T3> Create<T1, T2, T3>(T1 t1, T2 t2, T3 t3)
{
return new TypedTuple<T1, T2, T3>(t1, t2, t3);
}
public static TypedTuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 t1, T2 t2, T3 t3, T4 t4)
{
return new TypedTuple<T1, T2, T3, T4>(t1, t2, t3, t4);
}
public static TypedTuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
{
return new TypedTuple<T1, T2, T3, T4, T5>(t1, t2, t3, t4, t5);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6>(t1, t2, t3, t4, t5, t6);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6, T7>(t1, t2, t3, t4, t5, t6, t7);
}
public static TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
{
return new TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8>(t1, t2, t3, t4, t5, t6, t7, t8);
}
}
public class TypedTuple<T>
{
protected Dictionary<Type, object> items = new Dictionary<Type, object>();
public TypedTuple(T item1)
{
Item1 = item1;
}
public TSource Get<TSource>()
{
object value;
if (this.items.TryGetValue(typeof(TSource), out value))
{
return (TSource)value;
}
else
return default(TSource);
}
private T item1;
public T Item1 { get { return this.item1; } set { this.item1 = value; this.items[typeof(T)] = value; } }
}
public class TypedTuple<T1, T2> : TypedTuple<T1>
{
public TypedTuple(T1 item1, T2 item2)
: base(item1)
{
Item2 = item2;
}
private T2 item2;
public T2 Item2 { get { return this.item2; } set { this.item2 = value; this.items[typeof(T2)] = value; } }
}
public class TypedTuple<T1, T2, T3> : TypedTuple<T1, T2>
{
public TypedTuple(T1 item1, T2 item2, T3 item3)
: base(item1, item2)
{
Item3 = item3;
}
private T3 item3;
public T3 Item3 { get { return this.item3; } set { this.item3 = value; this.items[typeof(T3)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4> : TypedTuple<T1, T2, T3>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4)
: base(item1, item2, item3)
{
Item4 = item4;
}
private T4 item4;
public T4 Item4 { get { return this.item4; } set { this.item4 = value; this.items[typeof(T4)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5> : TypedTuple<T1, T2, T3, T4>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
: base(item1, item2, item3, item4)
{
Item5 = item5;
}
private T5 item5;
public T5 Item5 { get { return this.item5; } set { this.item5 = value; this.items[typeof(T5)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6> : TypedTuple<T1, T2, T3, T4, T5>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
: base(item1, item2, item3, item4, item5)
{
Item6 = item6;
}
private T6 item6;
public T6 Item6 { get { return this.item6; } set { this.item6 = value; this.items[typeof(T6)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6, T7> : TypedTuple<T1, T2, T3, T4, T5, T6>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
: base(item1, item2, item3, item4, item5, item6)
{
Item7 = item7;
}
private T7 item7;
public T7 Item7 { get { return this.item7; } set { this.item7 = value; this.items[typeof(T7)] = value; } }
}
public class TypedTuple<T1, T2, T3, T4, T5, T6, T7, T8> : TypedTuple<T1, T2, T3, T4, T5, T6, T7>
{
public TypedTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8)
: base(item1, item2, item3, item4, item5, item6, item7)
{
Item8 = item8;
}
private T8 item8;
public T8 Item8 { get { return this.item8; } set { this.item8 = value; this.items[typeof(T8)] = value; } }
}
class Program
{
public class MyTuple
{
private Tuple<int, int> t;
public MyTuple(int a, int b)
{
t = new Tuple<int, int>(a, b);
}
public int A
{
get
{
return t.Item1;
}
}
public int B
{
get
{
return t.Item2;
}
}
public override bool Equals(object obj)
{
return t.Equals(((MyTuple)obj).t);
}
public override int GetHashCode()
{
return t.GetHashCode();
}
public static bool operator ==(MyTuple m1, MyTuple m2)
{
return m1.Equals(m2);
}
public static bool operator !=(MyTuple m1, MyTuple m2)
{
return !m1.Equals(m2);
}
}
static void Main(string[] args)
{
var v1 = new MyTuple(1, 2);
var v2 = new MyTuple(1, 2);
Console.WriteLine(v1 == v2);
Dictionary<MyTuple, int> d = new Dictionary<MyTuple, int>();
d.Add(v1, 1);
Console.WriteLine(d.ContainsKey(v2));
}
}
public class OrderRelatedIds
{
public int OrderGroupId { get; set; }
public int OrderTypeId { get; set; }
public int OrderSubTypeId { get; set; }
public int OrderRequirementId { get; set; }
public OrderRelatedIds()
{
}
public OrderRelatedIds(int orderGroupId)
: this()
{
OrderGroupId = orderGroupId;
}
public OrderRelatedIds(int orderGroupId, int orderTypeId)
: this(orderGroupId)
{
OrderTypeId = orderTypeId;
}
public OrderRelatedIds(int orderGroupId, int orderTypeId, int orderSubTypeId)
: this(orderGroupId, orderTypeId)
{
OrderSubTypeId = orderSubTypeId;
}
public OrderRelatedIds(int orderGroupId, int orderTypeId, int orderSubTypeId, int orderRequirementId)
: this(orderGroupId, orderTypeId, orderSubTypeId)
{
OrderRequirementId = orderRequirementId;
}
}
OrderRelatedIds orders = new OrderRelatedIds
{
OrderGroupId = 1,
OrderTypeId = 2,
OrderSubTypeId = 3,
OrderRequirementId = 4
};
public class OrderRelatedIds
{
public int OrderGroupId;
public int OrderTypeId;
public int OrderSubTypeId;
public int OrderRequirementId;
}
helloworld("Hi1,Hi2");
/// <summary>
/// Return hello = Item1 and world Item2
/// </summary>
/// <param name="input">string to split</param>
/// <returns></returns>
private static Tuple<bool, bool> helloworld(string input)
{
bool hello = false;
bool world = false;
foreach (var hw in input.Split(','))
{
switch (hw)
{
case "Hi1":
hello= true;
break;
case "Hi2":
world= true;
break;
}
}
return new Tuple<bool, bool>(hello, world);
}
(string first, string middle, string last) LookupName(long id)
Tuple<ApiResource, JSendResponseStatus> result = await SendApiRequest();
ApiResource apiResource = result.Item1;
JSendResponseStatus jSendStatus = result.Item2;
var myDetails = (MyName: "RBT_Yoga", MyAge: 22, MyFavoriteFood: "Dosa");
Console.WriteLine($"Name - {myDetails.MyName}, Age - {myDetails.MyAge}, Passion - {myDetails.MyFavoriteFood}");
static void Main(string[] args)
{
var empInfo = GetEmpInfo();
Console.WriteLine($"Employee Details: {empInfo.firstName}, {empInfo.lastName}, {empInfo.computerName}, {empInfo.Salary}");
}
static (string firstName, string lastName, string computerName, int Salary) GetEmpInfo()
{
//This is hardcoded just for the demonstration. Ideally this data might be coming from some DB or web service call
return ("Rasik", "Bihari", "Rasik-PC", 1000);
}
var tupleList = new List<(int Index, string Name)>
{
(1, "cow"),
(5, "chickens"),
(1, "airplane")
};
foreach (var tuple in tupleList)
Console.WriteLine($"{tuple.Index} - {tuple.Name}");
(string first, string middle, string last) LookupName(long id)
public static (string extension, string fileName) GetFile()
{
return ("png", "test");
}
var (extension, fileName) = GetFile();
Console.WriteLine(extension);
Console.WriteLine(fileName);
var tuple = TupleExample(key, value);
private (string key1, long value1) ValidateAPIKeyOwnerId(string key, string value)
{
return (key, value);
}
if (!string.IsNullOrEmpty(tuple.key1) && tuple.value1 > 0)
{
//your code
}
public static IQueryable<(string name, string surname)> GetPersonName(this PersonContext ctx, int id)
{
return ctx.Persons
.Where(person => person.Id == id)
.Select(person => ValueTuple.Create(person.Name, person.Surname));
}
// This won't work
ctx.GetPersonName(id).Select((name, surname) => { return name + surname; })
// But this will
ctx.GetPersonName(id).Select(t => { return t.name + t.surname; })
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main() {
var me = (age: 21, favoriteFood: "Custard");
string json = JsonConvert.SerializeObject(me);
// Will output {"Item1":21,"Item2":"Custard"}
Console.WriteLine(json);
}
}
(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.
(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.
From Docs https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples