c#-6.0

Null-propagation replacement for null check prior conditional statement

旧巷老猫 提交于 2019-12-02 01:27:50
After seeing a similar question , I was wondering if the following expression ... if (attribute != null && attribute.Description == input) ... would behave (almost) identical, to following null-propagation variant? if (attribute?.Description == input) So far, I could determine only following (somehow minor) differences: not possible in case input is of non-nullable type in case input would be itself null , behavior would be altered Am I missing something? or are there other differences in behavior ? EDIT: in the end, the only fail-safe alternative I've found for the first snippet, would be: if

Writing a nested join with LINQ to Entities

試著忘記壹切 提交于 2019-12-01 23:23:20
I'm pretty sure there's an example of this somewhere on here, I just can't find it as I don't know the exact terms. So please, be kind. The problem should be a simple one: I have an Azure SQL database, somewhat badly designed (ie missing foreign keys and such). As I cannot redesign the database at this point, I have to handle relations manually via queries. As the database is also the bottleneck of our software, I must try and complete queries with as little database hits as possible. I have entities A, B, C and D. An entity A has several entities B (connected with an unregistered foreign key)

Why can't I use the conditional operator in an interpolated string without brackets? [duplicate]

喜你入骨 提交于 2019-12-01 22:52:51
This question already has an answer here: How to use the ternary operator inside an interpolated string? 1 answer Why can't I use the inline conditional-operator inside a c#-6 string interpolation, without encompassing it within brackets? and the errors: As you can see, it appears the parser is having a hard time. Is this a bug, or a feature of the string interpolation mechanism? From MSDN (emphasis mine): $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ? "" : "s")} old." You do not need to quote the quotation characters within the contained interpolation expressions because

How to specify the equivalent of /features:strict (of csc.exe) to msbuild.exe or in the .csproj file?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 17:51:49
Introduction Consider this simple (and bad) C# class: using System; namespace N { static class C { static void M(DateTime d) { if (d == null) Console.WriteLine("Yes"); else Console.WriteLine("No"); } static void L(object o) { if (o is Nullable) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } Both methods M and L have serious issues. In M , we ask if a value of the non-nullable struct DateTime is equal to null via the lifted == operator (which exists since DateTime overloads operator == ). This is always falls, and the compiler can tell at compile-time, so we have a branch ( "Yes"

Why is nameof(object) not allowed?

陌路散爱 提交于 2019-12-01 16:54:29
In C# 6.0 you can write this: var instance = default(object); var type = typeof(object); They have the same result of: var instance = default(System.Object); var type = typeof(System.Object); But you can't write this: var name = nameof(object); It generates the following error: Invalid expression term 'object'. But you can still write this: var name = nameof(System.Object); Why nameof(object) does not compile? The difference is that object is a synonym for the class Object and nameof() doesn't work on synonyms. Same applies to nameof(int) vs nameof(Int32) 来源: https://stackoverflow.com

Which “C# Experimental language feature” is this?

孤人 提交于 2019-12-01 15:50:37
In the example below, Resharper shows "C# Experimental language feature" tooltip on the first curly bracket. I've checked the new features of C# 6.0 but didn't come across a similar one. What is the referred experimental feature? class Class1 { { // <= C# Experimental language feature } } Note: It is an error for .Net Framework 4.5 compiler. "Invalid token '{' in class, struct, or interface member declaration" This was for Primary Constructors , a feature which has now been cut from C#6 . On November 12, 2014 Microsoft announced the release of Visual Studio 2015 on the day of Visual Studio

Breaking change in method overload resolution in C# 6 - explanation?

ぐ巨炮叔叔 提交于 2019-12-01 15:35:49
We've recently moved from VS2013 to VS2017 in our company. After the upgrade our codebase would no longer build. We would get the following error: The call is ambiguous between the following methods or properties: 'IRepository<T>.Get(object, params Expression<Func<T, object>>[])' and 'IRepository<T>.Get(object, params string[])' Here is the call itself: this.mainRepository.Get(newEntity.Id); ...and the interface definition: public interface IRepository<T> where T : class { T Get(object id, params Expression<Func<T, object>>[] includeExprs); T Get(object id, params string[] includeExprs); } I

nameof with generic types

此生再无相见时 提交于 2019-12-01 15:05:59
I am trying to get the name of a method on a generic interface. I would expect this to work as the type part would be a valid typeof: //This does not compile nameof(IGenericInterface<>.Method) //This would compile typeof(IGenericInterface<>) I think this should be valid C# 6 or am I missing something or is their a better way to do this. I don't want to use a string for the Method name as if method is renamed code would break without any build time errors. This is expected. According to the documentation , your expression is disallowed, because it refers to an unbound generic type: Because the

nameof with generic types

試著忘記壹切 提交于 2019-12-01 13:58:49
问题 I am trying to get the name of a method on a generic interface. I would expect this to work as the type part would be a valid typeof: //This does not compile nameof(IGenericInterface<>.Method) //This would compile typeof(IGenericInterface<>) I think this should be valid C# 6 or am I missing something or is their a better way to do this. I don't want to use a string for the Method name as if method is renamed code would break without any build time errors. 回答1: This is expected. According to

Breaking change in method overload resolution in C# 6 - explanation?

半腔热情 提交于 2019-12-01 13:41:47
问题 We've recently moved from VS2013 to VS2017 in our company. After the upgrade our codebase would no longer build. We would get the following error: The call is ambiguous between the following methods or properties: 'IRepository<T>.Get(object, params Expression<Func<T, object>>[])' and 'IRepository<T>.Get(object, params string[])' Here is the call itself: this.mainRepository.Get(newEntity.Id); ...and the interface definition: public interface IRepository<T> where T : class { T Get(object id,