c#-6.0

How to implement INotifyPropertyChanged in C# 6.0?

拥有回忆 提交于 2019-11-27 08:52:19
The answer to this question has been edited to say that in C# 6.0, INotifyPropertyChanged can be implemented with the following OnPropertyChanged procedure: protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } However, it isn't clear from that answer what the corresponding property definition should be. What does a complete implementation of INotifyPropertyChanged look like in C# 6.0 when this construction is used? After incorporating the various changes, the code will look like this. I've

How to install the MS C# 6.0 compiler?

江枫思渺然 提交于 2019-11-27 07:42:02
问题 I am trying to compile a C# project that someone has created while using C# 6.0 features. In previous .NET releases, the current C# compiler was automatically installed and ready to run along with the .NET Framework. Apparently, this is no longer the case. I currently have .NET 4.6.1 on my machine, but invoking csc tells me: Microsoft (R) Visual C# Compiler version 4.6.1055.0 for C# 5 Copyright (C) Microsoft Corporation. All rights reserved. This compiler is provided as part of the Microsoft

C# 6.0 Null Propagation Operator & Property Assignment

别说谁变了你拦得住时间么 提交于 2019-11-27 06:34:24
问题 This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 in that you cannot call property setters against an object that has been null propagated (though you can call property getters against an object that has been null propagated). As you will see from the generated IL (which I have reflected into C#) , there is nothing that should limit the ability to

C# Null propagating operator / Conditional access expression & if blocks

梦想与她 提交于 2019-11-27 05:34:07
The Null propagating operator / Conditional access expression coming in c#-6.0 looks like quite a handy feature. But I'm curious if it will help solve the problem of checking if a child member is not null and then calling a Boolean method on said child member inside an if block: public class Container<int>{ IEnumerable<int> Objects {get;set;} } public Container BuildContainer() { var c = new Container(); if (/* Some Random Condition */) c.Objects = new List<int>{1,2,4}; } public void Test() { var c = BuildContainer(); //Old way if ( null != c && null != c.Objects && c.Objects.Any()) Console

String Interpolation with format variable

荒凉一梦 提交于 2019-11-27 05:14:32
I can do this: var log = string.Format("URL: {0}", url); or even like this var format = "URL: {0}"; ... var log = string.Format(format, url); I have a format defined somewhere else and use the format variable, not inline string. In C# 6, this is seems impossible: var format = $"URL: {url}"; // Error url does not exist ... var url = "http://google.com"; ... var log = $format; // The way to evaluate string interpolation here Is there anyway to use string interpolation with variable declared earlier? C# 6 seems interpolate the string inline during compile time. However consider using this feature

What benefits does dictionary initializers add over collection initializers?

末鹿安然 提交于 2019-11-27 04:40:28
问题 In a recent past there has been a lot of talk about whats new in C# 6.0 One of the most talked about feature is using Dictionary initializers in C# 6.0 But wait we have been using collection initializers to initialize the collections and can very well initialize a Dictionary also in .NET 4.0 and .NET 4.5 (Don't know about old version) like Dictionary<int, string> myDict = new Dictionary<int, string>() { { 1,"Pankaj"}, { 2,"Pankaj"}, { 3,"Pankaj"} }; So what is there new in C# 6.0, What

Lambda for getter and setter of property

半世苍凉 提交于 2019-11-27 04:27:58
In C# 6.0 I can write: public int Prop => 777; But I want to use getter and setter. Is there a way to do something kind of the next? public int Prop { get => propVar; set => propVar = value; } Diligent Key Presser First of all, that is not lambda, although syntax is similar. It is called " expression-bodied members ". They are similar to lambdas, but still fundamentally different. Obviously they can't capture local variables like lambdas do. Also, unlike lambdas, they are accessible via their name:) You will probably understand this better if you try to pass an expression-bodied property as a

Long string interpolation lines in C#6

…衆ロ難τιáo~ 提交于 2019-11-27 04:08:36
I've found that while string interpolation is really nice when applied to my existing code base's string Format calls, given the generally preferred column limit, the string rapidly becomes too long for a single line. Especially when the expressions being interpolated are complex. With a format string you have a list of variables that you can split into multiple lines. var str = string.Format("some text {0} more text {1}", obj1.property, obj2.property); Does anyone have any preferred means of breaking up these lines? I suppose you could do something like: var str = $"some text { obj1.property

Parameterless constructors in structs for C# 6

荒凉一梦 提交于 2019-11-27 03:04:29
问题 My understanding is that Parameterless constructors in structs are now allowed. But the following gives me a compile error in VS 2015 Community public struct Person { public string Name { get; } public int Age { get; } public Person(string name, int age) { Name = name; Age = age; } public Person() : this("Jane Doe", 37) { } } Error: "Structs cannot contain explicit parameterless constructors" Anyone know why? 回答1: The feature was present in older previews of C# 6.0, which is why some articles

What is the purpose of nameof?

↘锁芯ラ 提交于 2019-11-27 02:37:13
Version 6.0 got a new feature of nameof , but I can't understand the purpose of it, as it just takes the variable name and changes it to a string on compilation. I thought it might have some purpose when using <T> but when I try to nameof(T) it just prints me a T instead of the used type. Any idea on the purpose? What about cases where you want to reuse the name of a property, for example when throwing exception based on a property name, or handling a PropertyChanged event. There are numerous cases where you would want to have the name of the property. Take this example: switch (e.PropertyName