valuetuple

Value tuples expose wrong parameter name from WebAPI

纵饮孤独 提交于 2020-02-24 00:38:26
问题 I'm using web api. I've been a bit lazy and decided to return a value tuple from my controller. [HttpGet] [Route(AuthAPIRoutes.GET_MFA_DEVICES)] public (string Type, string Value)[] GetMultiFactoryMethods() { return GlobalFactory<IPaystreamMFASecurityService>.Instance.GetMultiFactorMethods(); } The JSON response doesn't seem to be using the appropriate naming is this being optimized away? { "item1": "Phone", "item2": "1-512-555-0550" } NOTE: I'm aware I can explicitly make a model to avoid

Are C# anonymous types redundant in C# 7

a 夏天 提交于 2020-01-12 13:59:27
问题 Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following line collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray(); redundant. What would be the use case where one is better used over the other (for either performance reasons or optimization)? Obviously, if there is a need for more than six

How to create a List of ValueTuple?

女生的网名这么多〃 提交于 2019-12-18 12:09:45
问题 Is it possible to create a list of ValueTuple in C# 7? like this: List<(int example, string descrpt)> Method() { return Something; } 回答1: You are looking for a syntax like this: List<(int, string)> list = new List<(int, string)>(); list.Add((3, "first")); list.Add((6, "second")); You can use like that in your case: List<(int, string)> Method() => new List<(int, string)> { (3, "first"), (6, "second") }; You can also name the values before returning: List<(int Foo, string Bar)> Method() => ...

C# ValueTuple properties naming

房东的猫 提交于 2019-12-10 04:08:21
问题 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

Convert ValueTuple to IEnumerable

三世轮回 提交于 2019-12-08 16:18:27
问题 Is there a saner way to do the following: public static class ValueTupleAdditions { public static IEnumerable<object> ToEnumerable<A, B>(this ValueTuple<A, B> tuple) { yield return tuple.Item1; yield return tuple.Item2; } public static IEnumerable<object> ToEnumerable<A, B, C>(this ValueTuple<A, B, C> tuple) { yield return tuple.Item1; yield return tuple.Item2; yield return tuple.Item3; } [etc] } EDIT: Since people are asking for a use case, here you go. using Xunit; namespace Whatever {

Are C# anonymous types redundant in C# 7

北城以北 提交于 2019-12-04 00:56:02
Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following line collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray(); redundant. What would be the use case where one is better used over the other (for either performance reasons or optimization)? Obviously, if there is a need for more than six fields, tuples cannot be used, but is there something a bit more nuanced to it? There are various

When to use: Tuple vs Class c# 7.0

血红的双手。 提交于 2019-11-30 11:15:45
问题 Before Tuples, I used to create a class and its variables then create object from this class and make that object the return type for some functions. Now with the tuples I can do the same and in c# 7.0 we can assign understandable names for tuples properties (before this it was item1 , item2 , etc..) So now I am wondering, when should I use tuples and when should I create a class in c# 7.0? 回答1: As this answer is causing some confusion amongst some folk here, I should clarify that - as per

How to create a List of ValueTuple?

谁都会走 提交于 2019-11-30 06:35:55
Is it possible to create a list of ValueTuple in C# 7? like this: List<(int example, string descrpt)> Method() { return Something; } You are looking for a syntax like this: List<(int, string)> list = new List<(int, string)>(); list.Add((3, "first")); list.Add((6, "second")); You can use like that in your case: List<(int, string)> Method() => new List<(int, string)> { (3, "first"), (6, "second") }; You can also name the values before returning: List<(int Foo, string Bar)> Method() => ... And you can receive the values while (re)naming them: List<(int MyInteger, string MyString)> result = Method

Is it possible to bind to a ValueTuple field in WPF with C#7

一世执手 提交于 2019-11-29 13:13:59
If I have a viewmodel property public (string Mdf, string MdfPath) MachineDefinition { get; set; } and I try to bind to it in XAML / WPF <Label Content="{Binding Path=MachineDefinition.Item2}" /> or <Label Content="{Binding Path=MachineDefinition.MdfPath}" /> I get the same error I see that ValueTuple fields are really fields not properties . Is this the problem? The confusion is that for old style Tuple ( pre C#7 ) all the Items were properties https://msdn.microsoft.com/en-us/library/dd386940(v=vs.110).aspx and thus bindable. For ValueTuple they are fields https://github.com/dotnet/corefx

I can't get parameter names from valuetuple via reflection in c# 7.0

时光总嘲笑我的痴心妄想 提交于 2019-11-29 10:11:55
I want to Map a ValueTuple to a class using reflection. Documentation says that there is a Attribute attached to ValueTuple with parameters names (others than Item1, Item2, etc...) but I can't see any Attribute. Disassembly shows nothing. What's happens? Example: public static T ToStruct<T, T1,T2>(this ValueTuple<T1,T2> tuple) where T : struct Via reflection can't get Item1, Item2 names to match with T fields via reflection. You should have the TupleElementNames attribute on the method created by the compiler. See this code : public class C { public (int a, int b) M() { return (1, 2); } }