c#-7.0

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

爱⌒轻易说出口 提交于 2019-11-28 03:31:40
问题 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. 回答1: You should have the TupleElementNames attribute on the method

Using a C# 7 tuple in an ASP.NET Core Web API Controller

旧城冷巷雨未停 提交于 2019-11-28 01:00:31
Do you know why this works: public struct UserNameAndPassword { public string username; public string password; } [HttpPost] public IActionResult Create([FromBody]UserNameAndPassword usernameAndPassword) { Console.WriteLine(usernameAndPassword); if (this.AuthenticationService.IsValidUserAndPasswordCombination(usernameAndPassword.username, usernameAndPassword.password)) return new ObjectResult(GenerateToken(usernameAndPassword.username)); return BadRequest(); } But when I replace it with a tuple, this doesn’t work? [HttpPost] public IActionResult Create([FromBody](string username, string

Non-shortcircuting boolean operators and C# 7 Pattern matching

[亡魂溺海] 提交于 2019-11-27 23:58:10
问题 Im currently writing an C# application, targeting .NET 4.7 (C# 7). I am confused after I tried using the new way of declaring a variable utilizing the "is" keyword: if (variable is MyClass classInstance) This way it works, but when doing: if (true & variable is MyClass classInstance) { var a = classInstance; } Visual Studio (I'm using 2017) shows me the the Error Use of unassigned local variable 'classInstance' . Using the short-circuting version of & ( && ) it works fine. Am I missing

Equality and polymorphism

烂漫一生 提交于 2019-11-27 15:39:55
问题 With two immutable classes Base and Derived (which derives from Base) I want to define Equality so that equality is always polymorphic - that is ((Base)derived1).Equals((Base)derived2) will call Derived.Equals operators == and != will call Equals rather than ReferenceEquals (value equality) What I did: class Base: IEquatable<Base> { public readonly ImmutableType1 X; readonly ImmutableType2 Y; public Base(ImmutableType1 X, ImmutableType2 Y) { this.X = X; this.Y = Y; } public override bool

C# 7 Expression Bodied Constructors

半腔热情 提交于 2019-11-27 13:56:38
In C# 7, how do I write an Expression Bodied Constructor like this using 2 parameters. public Person(string name, int age) { Name = name; Age = age; } A way to do this is to use a tuple and a deconstruction to allow multiple assignments in one expression: public class Person { public string Name { get; } public int Age { get; } public Person(string name, int age) => (Name, Age) = (name, age); } As of C# 7.1 (introduced with Visual Studio 2017 Update 3), the compiler code will now optimise away the actual construction and deconstruction of the tuple. So this approach has no performance overhead

Using C# 7 features inside of a View in an ASP.NET MVC Core project

烈酒焚心 提交于 2019-11-27 13:54:42
问题 I've looked for other questions related to this, but none seem to be quite what I'm looking for. I have a website running on ASP.NET Core with the new project structure in VS2017. Code files using C#7 features compile fine. But attempting to use those features in a View results in a series of errors about syntax. I tried installing Roslyn to get it to be used when compiling views since from what I can tell the C#7 features are available in the Roslyn nuget package 2.x and higher. But now I'm

C# 7 .NET / CLR / Visual Studio version requirements

試著忘記壹切 提交于 2019-11-27 11:33:09
What are the minimum .NET framework and CLR version requirements for running C# 7? Also, do I need VS 2017 to compile C# 7? dmeglio You do NOT need to target .NET 4.6 and above, that is incorrect. To use Tuples, you need the System.ValueTuple NuGet package. Right on https://www.nuget.org/packages/System.ValueTuple/ you can see it says it supports 4.5 and above, and actually, it supports 4.0 and above. And if you want to get crazy, if you create your own System.ValueTuple class that does exactly what that package does, it will work back on .NET 3.5 and probably older too. For "Task-like" types,

Odd return syntax statement

百般思念 提交于 2019-11-27 05:17:40
问题 I know this may sound strange but I don't know even how to search this syntax in internet and also I am not sure what exactly means. So I've watched over some MoreLINQ code and then I noticed this method public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException

C# 7 ValueTuple compile error

夙愿已清 提交于 2019-11-27 03:44:51
问题 I'm using VS2017 RC and my application targets net framework 4.6.1. I have two assemblies referencing System.ValueTuple 4.3 MyProject.Services MyProject.WebApi In MyProject.Services I have a class with a method like this public async Task<(int fCount, int cCount, int aCount)> GetAllStatsAsync() { // Some code... return (fCount, cCount, aCount); } In MyProject.WebApi I have a controller that use this method like that: public async Task<HttpResponseMessage> GetInfoAsync() { // Some code... var

In C# 7 is it possible to deconstruct tuples as method arguments

浪尽此生 提交于 2019-11-27 02:37:53
问题 For example I have private void test(Action<ValueTuple<string, int>> fn) { fn(("hello", 10)); } test(t => { var (s, i) = t; Console.WriteLine(s); Console.WriteLine(i); }); I would like to write something like this private void test(Action<ValueTuple<string, int>> fn) { fn(("hello", 10)); } test((s,i) => { Console.WriteLine(s); Console.WriteLine(i); }); Is this possible with some proper notation? 回答1: You can shorten it to: void test( Action<ValueTuple<string, int>> fn) { fn(("hello", 10)); }