c#-7.0

Could not load file or assembly 'System.ValueTuple'

北城以北 提交于 2019-11-29 02:50:58
I've got a VS2017 project that compiles to a DLL which is then called by an EXE written by someone else. Both projects target .Net Framework 4.6.2. I rewrote one of my DLL methods to return a tuple and also imported the associated NuGet package. When I compile the project it includes System.ValueTuple.dll in the output directory which is then deployed to other machines where my DLL is loaded and called by the EXE. But when the EXE attempts to call the method that returns a tuple it crashes: Unexpected Error Could not load file or assembly 'System.ValueTuple, Version=4.0.1.0, Culture=neutral,

Equality and polymorphism

半腔热情 提交于 2019-11-29 01:08:59
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 Equals(object obj) { if (object.ReferenceEquals(this, obj)) return true; if (obj is null || obj.GetType()!

Inline variable declaration not compiling

懵懂的女人 提交于 2019-11-28 22:23:06
I've been getting a message in Visual Studio 2017, specifically, IDE0018 Variable declaration can be inlined. So I try using an inline variable declaration the way it's mentioned in the visual studio 2017 release notes, but I can't get my project to compile. It show no error messages, but the output shows " Rebuild All failed..... error CS1525: Invalid expression term 'int' " The error only shows up in the output, not as an actual error in the error list. Here is an actual example of the code I'm using that is failing. if (int.TryParse(ExpYear, out int IExpYear)) { if (IExpYear < DateTime.Now

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

天涯浪子 提交于 2019-11-28 21:29:02
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 getting feedback that explicitly says error CS8059: Feature 'out variable declaration' is not available

TryParse with out var param

試著忘記壹切 提交于 2019-11-28 20:59:08
A new feature in C# 6.0 allows to declare variable inside TryParse method. I have some code: string s = "Hello"; if (int.TryParse(s, out var result)) { } But I receive compile errors: What I am doing wrong? P.S.: in project settings C# 6.0 and .NET framework 4.6 are set. A new feature in C# 6.0 allows to declare variable inside TryParse method. Declaration expressions was cut from C# 6.0 and wasn't shipped in the final release. You currently can't do that. There is a proposal for it on GitHub for C# 7 (also see this for future reference). Update (07/03/2017) With the official release of C#7,

Unable to return Tuple from a method using Visual Studio 2017 and C# 7.0

纵饮孤独 提交于 2019-11-28 19:59:20
I've installed Visual Studio 2017 Community that was released a week ago, and I started exploring the new features of C# 7. So I created a simple method that returns two values: public class Program { public static void Main(string[] args) { (int sum, int count) a = ReturnTwoValues(); } static (int sum, int count) ReturnTwoValues() => (1, 1); } Compiler is generating an error: Error CS8137 Cannot define a class or member that utilizes tuples because the compiler required type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' cannot be found. Are you missing a reference? I tried

C# 7 ValueTuple compile error

蹲街弑〆低调 提交于 2019-11-28 10:41:03
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 stats = await _myClass.GetAllStatsAsync(); var vm = new ViewModel { FCount = stats.fCount, CCount =

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

两盒软妹~` 提交于 2019-11-28 09:39:07
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? Paulo Morgado You can shorten it to: void test( Action<ValueTuple<string, int>> fn) { fn(("hello", 10)); } test(((string s, int i) t) => { Console.WriteLine(t.s); Console.WriteLine(t.i); }); Hopefully,

C# deconstruction and overloads

亡梦爱人 提交于 2019-11-28 07:42:30
问题 While investigating the new features in C# 7.x, I created the following class: using System; namespace ValueTuples { public class Person { public string Name { get; } public DateTime BirthDate { get; } public Person(string name, DateTime birthDate) { Name = name; BirthDate = birthDate; } public void Deconstruct(out string name, out int year, out int month, out int day) { name = Name; year = BirthDate.Year; month = BirthDate.Month; day = BirthDate.Day; } public void Deconstruct(out string name

Odd return syntax statement

纵饮孤独 提交于 2019-11-28 04:14:35
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(nameof(keySelector)); return _(); IEnumerable<TSource> _() { var knownKeys = new HashSet<TKey>(comparer);