c#-6.0

Does C# 6 Elvis operator (null propagation) short circuit

点点圈 提交于 2019-12-10 17:42:12
问题 Why this c# code throws a null exception? bool boolResult = SomeClass?.NullableProperty.ItsOkProperty ?? false; Isn´t elvis operator supposed to stop evaluation (short circuit) once the NullableProperty evaluates to null? In my understanding the line of code above is a shortcut for: bool boolResult if(SomeClass != null) if(SomeClass.NullableProperty != null) boolResult = SomeClass.NullableProperty.ItsOkProperty; else boolResult = false; else boolResult = false; Did I assume wrong? EDIT: Now I

Skip expression bodied property in debugger

只谈情不闲聊 提交于 2019-12-10 17:18:04
问题 Is there analog of [DebuggerStepThrough] attribute available for expression-bodied properties in C# ? For example I want to skip over the code public Byte ByteArray => Builder.CreateArray(); [DebuggerStepThrough] can not be applied to properties. Does C# team provide any other solution in C# 6.0 ? 回答1: DebuggerStepThrough isn't valid for expression bodied properties as this: [DebuggerStepThrough] public Byte ByteArray => Builder.CreateArray(); Doesn't compile. This however does: public Byte

Null propagation operator

心不动则不痛 提交于 2019-12-10 04:21:17
问题 I've looked around a bit but haven't been able to find an answer to how the new C# 6.0 compiler breaks down the new null propagation command for something such as the following: BaseType myObj = new DerivedType(); string myString = (myObj as DerivedType)?.DerivedSpecificProperty; What I want to know is how exactly it handles this. Does it cache the as cast into a new DerivedType variable (i.e., this is just syntactical sugar for an as cast followed by an null comparison). Or if it actually as

What does '$' sign do in C# 6.0?

怎甘沉沦 提交于 2019-12-09 15:15:47
问题 In MVC 6 source code I saw some code lines that has strings leading with $ signs. As I never saw it before, I think it is new in C# 6.0. I'm not sure. (I hope I'm right, otherwise I'd be shocked as I never crossed it before. It was like: var path = $"'{pathRelative}'"; 回答1: You're correct, this is a new C# 6 feature. The $ sign before a string enables string interpolation. The compiler will parse the string specially, and any expressions inside curly braces will be evaluated and inserted into

Omitted setter vs private setter?

橙三吉。 提交于 2019-12-09 07:26:08
问题 What is the difference between a property with a omitted setter and a property with a private setter? public string Foo { get; private set; } vs public string Foo { get; } 回答1: In C# 6, get; only properties are only settable from the constructor. From everywhere else, it is read-only. A property with a private set; can be set from everywhere inside that class. 回答2: From outside the class, it won't change anything if you use this syntax: public string Foo { get; } But you won't be able to

How to emulate C# 6 null-conditional in C# < 6

夙愿已清 提交于 2019-12-09 02:56:22
问题 With C# 6.0 I can do this var isEqual = x.Id == y.Id && x.UpdatedAt == y.UpdatedAt && x.Name == y.Name && x.RulesUrl == y.RulesUrl && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Is there any nice solution to do this with C# < 6.0? I mean this part && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Because in old projects we do not have possibility to use C# 6.0. How

Why can't I throw exceptions from an expression-bodied member?

血红的双手。 提交于 2019-12-09 02:07:18
问题 Using expression-bodied members allows you to define the body of a method or property as a single expression without a return keyword (should it return something). For example it turns these int Method1() { return 5; } void Method2() { Console.WriteLine(); } into these int Method1() => 5; void Method2() => Console.WriteLine(); A difference comes into play when you throw an exception from the body: void Method3() { throw new Exception(); } However, the following will not compile: void Method3(

Expression vs nameof

痞子三分冷 提交于 2019-12-08 19:35:58
问题 It is a good idea to use nameof over expressions for extracting property names? //method with expression protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, bool isValid, [param: Localizable(true)] string validationError) { string propertyName = PropertySupport.ExtractPropertyName(propertyExpression); RaisePropertyChanged(propertyName, isValid, validationError); } //the same logic without expression protected void RaisePropertyChanged(string propertyName, [param:

Does C# 6.0's String interpolation rely on Reflection?

隐身守侯 提交于 2019-12-08 17:02:10
问题 Short and simple. Does the new string interpolation in C# 6.0 rely on reflection? I.e. does string myStr = $"Hi {name}, how are you?"; use reflection at runtime to find the variable "name" and its value? 回答1: No. It doesn't. It is completely based on compile-time evaluation. You can see that with this TryRoslyn example that compiles and decompiles this: int name = 4; string myStr = $"Hi {name}, how are you?"; Into this: int num = 4; string.Format("Hi {0}, how are you?", num); string

Why Does .NET 4.6 Specific Code Compile When Targeting Older Versions of the Framework? [duplicate]

廉价感情. 提交于 2019-12-08 16:13:29
问题 This question already has answers here : Does C# 6.0 work for .NET 4.0? (5 answers) Closed 4 years ago . I have a project that targets older versions of the .NET framework (.NET 4.5.2). I installed Visual Studio 2015 (and therefore .NET 4.6 on my machine). I noticed that if I use C# language features released in .NET 4.6/C# 6, it still compiles. If my project's target framework is < .NET 4.6, shouldn't this not compile: public string MyExpressionBodyProperty => "1"; //auto properties are new