c#-6.0

Escaping Quotes inside new C# 6 String Syntax

半城伤御伤魂 提交于 2019-11-30 16:46:58
I'm really excited about the new features in C# 6, including the new string syntax: var fullName = $"My Name is {FirstName} {LastName}"; However, I can't figure out how to escape quotes inside the braces to do the follow: bool includePrefix = true; var fullName = $"My name is {includePrefix ? "Mr. " : ""}{FirstName} {LastName}"; C# 6 doesn't like that. I've had to revert to using String.Format in that second case. Is it possible to escape quotes using the new syntax? Update: Yes, I have tried using the \ escape, but it's not recognized. wrap your logic inside parentheses, inside the brackets:

static property in c# 6

白昼怎懂夜的黑 提交于 2019-11-30 14:41:51
问题 I'm writing a small code to more understand about property and static property . Like these: class UserIdentity { public static IDictionary<string, DateTime> OnlineUsers { get; set; } public UserIdentity() { OnlineUsers = new Dictionary<string, DateTime>(); } } or class UserIdentity { public IDictionary<string, DateTime> OnlineUsers { get; } public UserIdentity() { OnlineUsers = new Dictionary<string, DateTime>(); } } Since I changed it to: class UserIdentity { public static IDictionary

Why does nameof return only last name?

孤街浪徒 提交于 2019-11-30 14:29:11
问题 nameof(order.User.Age) return only "Age" instead of "order.User.Age" What is the reason to do it in more restricted way? If we want only last name we could do something like public static GetLastName(this string x) { return string.Split(x, '.').Last(); } nameof(order.User.Age).GetLastName() And with one operator we could get both, "Age" and "order.User.Age". But with current implementation we can only get "Age". Is there some logic behind this decision? Edit: For example, such behavior is

static property in c# 6

若如初见. 提交于 2019-11-30 11:21:43
I'm writing a small code to more understand about property and static property . Like these: class UserIdentity { public static IDictionary<string, DateTime> OnlineUsers { get; set; } public UserIdentity() { OnlineUsers = new Dictionary<string, DateTime>(); } } or class UserIdentity { public IDictionary<string, DateTime> OnlineUsers { get; } public UserIdentity() { OnlineUsers = new Dictionary<string, DateTime>(); } } Since I changed it to: class UserIdentity { public static IDictionary<string, DateTime> OnlineUsers { get; } public UserIdentity() { OnlineUsers = new Dictionary<string, DateTime

C#6's new Collection Initializer - Clarification?

邮差的信 提交于 2019-11-30 11:19:14
I've read that : The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object But looking at : var Dic = new Dictionary<string,int>{ {"x",3}, {"y",7} }; VS var Dic = new Dictionary<string,int>{ ["x"]=3, ["y"]=7 }; I don't see where the benefit is. it looks the same. Both are nothing more than a name-value collection. They swapped pairs of curly braces for pairs of square brackets and some commas Question: What is the added value for using the new syntax ? a real world example would be much appreciated. The main

Convert Contains To Expression Tree

情到浓时终转凉″ 提交于 2019-11-30 09:58:57
问题 Related To: Create a Lambda Expression With 3 conditions Please consider this Code: from a in myTbl where a.Address.Contains(strToCheck) select a How I can convert this to Expression Tree and write above code with Expressions? Main Problem is converting a.Address.Contains(strToCheck) to Expression Tree . Edit 1) Address is a string field and strToCheck is a string Thanks 回答1: a.Address.Contains(strToCheck) represents a call to string.Contains instance method on a.Address instance with

Operator '?' cannot be applied to operand of type 'T'

与世无争的帅哥 提交于 2019-11-30 09:13:14
问题 Trying to make Feature generic and then suddenly compiler said Operator '?' cannot be applied to operand of type 'T' Here is the code public abstract class Feature<T> { public T Value { get { return GetValue?.Invoke(); } // here is error set { SetValue?.Invoke(value); } } public Func<T> GetValue { get; set; } public Action<T> SetValue { get; set; } } It is possible to use this code instead get { if (GetValue != null) return GetValue(); return default(T); } But I am wondering how to fix that

How can I add C# 6.0 to Visual Studio 2013? [duplicate]

拜拜、爱过 提交于 2019-11-30 08:02:27
问题 This question already has answers here : How to enable C# 6.0 feature in Visual Studio 2013? (6 answers) Closed 4 years ago . Is there any way to add C# 6.0 to Visual Studio 2013? If I can't why is that? 回答1: The best you can currently do for VS2013 is download the April End User Preview, which is pretty outdated by now. The VS2013 compiler (as is) doesn't "understand" C#-6 features. Most, if not all of the C# new features are syntactic sugar which the compiler interprets and emits different

C# 6 how to format double using interpolated string?

不想你离开。 提交于 2019-11-30 05:34:06
I have used some new features of C# 6 incl. interpolated string for simple usage (showing message which contains string variables like $"{EmployeeName}, {Department}"). Now I want to use interpolated string for showing formatted double value. Example var aNumberAsString = aDoubleValue.ToString("0.####"); How can I write it as interpolated string? something like $"{aDoubleValue} ...." You can specify a format string after an expression with a colon ( : ): var aNumberAsString = $"{aDoubleValue:0.####}"; A colon after the variable specifies a format, Console.Write($"{aDoubleValue:0.####}"); 来源:

How to initialise ReadOnlyDictionary?

安稳与你 提交于 2019-11-30 01:49:19
问题 I have an unchanging dictionary that is exposed in a class. Currently my code looks like using System.Collections.Generic; using System.Collections.ObjectModel; public class FooClass { private static ReadOnlyDictionary<string, byte> _validRevisions = new ReadOnlyDictionary<string, byte>( new Dictionary<string, byte>() { { "1.0", 0x00 }, { "1.1", 0x01 }, { "1.2", 0x02 }, { "1.3", 0x03 } } ); public static ReadOnlyDictionary<string, byte> ValidRevisions => _validRevisions; // other FooClass