c#-6.0

How does nameof work?

不羁岁月 提交于 2019-12-22 04:22:14
问题 I was just wondering how come nameof from C# 6, can access non static property just like if it was static. Here is an example public class TestClass { public string Name { get; set; } } public class Test { public Test() { string name = nameof(TestClass.Name); // whats so speciall about nameof //string name2 = TestClass.Name; this won't compile obviously, } } 回答1: It's not "accessing" the property - that operator is purely a compiler mechanism to inject the "name" of the argument into the code

How does nameof work?

穿精又带淫゛_ 提交于 2019-12-22 04:21:26
问题 I was just wondering how come nameof from C# 6, can access non static property just like if it was static. Here is an example public class TestClass { public string Name { get; set; } } public class Test { public Test() { string name = nameof(TestClass.Name); // whats so speciall about nameof //string name2 = TestClass.Name; this won't compile obviously, } } 回答1: It's not "accessing" the property - that operator is purely a compiler mechanism to inject the "name" of the argument into the code

call instead of callvirt in case of the new c# 6 “?” null check

微笑、不失礼 提交于 2019-12-22 03:16:07
问题 Given the two methods: static void M1(Person p) { if (p != null) { var p1 = p.Name; } } static void M2(Person p) { var p1 = p?.Name; } Why the M1 IL code use callvirt : IL_0007: brfalse.s IL_0012 IL_0009: nop IL_000a: ldarg.0 IL_000b: callvirt instance string ConsoleApplication4.Person::get_Name() and the M2 IL use call : brtrue.s IL_0007 IL_0004: ldnull IL_0005: br.s IL_000d IL_0007: ldarg.0 IL_0008: call instance string ConsoleApplication4.Person::get_Name() I just can guess that it because

call instead of callvirt in case of the new c# 6 “?” null check

孤人 提交于 2019-12-22 03:16:01
问题 Given the two methods: static void M1(Person p) { if (p != null) { var p1 = p.Name; } } static void M2(Person p) { var p1 = p?.Name; } Why the M1 IL code use callvirt : IL_0007: brfalse.s IL_0012 IL_0009: nop IL_000a: ldarg.0 IL_000b: callvirt instance string ConsoleApplication4.Person::get_Name() and the M2 IL use call : brtrue.s IL_0007 IL_0004: ldnull IL_0005: br.s IL_000d IL_0007: ldarg.0 IL_0008: call instance string ConsoleApplication4.Person::get_Name() I just can guess that it because

What is the experimental feature “indexed members”?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 02:00:13
问题 On the new Roslyn Preview site it mentions being able to try out potential language features, and lists three such features. The first two I've heard of before (for example here), but I can't figure out from the code sample what "indexed members" are. Can anyone explain what these are, either based on another source or the code sample? (It's worth nothing $x is not a valid identifier in C# 5.) UPDATE - According to the Roslyn Feature Status page, this feature has been withdrawn. 回答1: .$foobar

What is the experimental feature “indexed members”?

喜欢而已 提交于 2019-12-22 01:59:00
问题 On the new Roslyn Preview site it mentions being able to try out potential language features, and lists three such features. The first two I've heard of before (for example here), but I can't figure out from the code sample what "indexed members" are. Can anyone explain what these are, either based on another source or the code sample? (It's worth nothing $x is not a valid identifier in C# 5.) UPDATE - According to the Roslyn Feature Status page, this feature has been withdrawn. 回答1: .$foobar

Overloaded string methods with string interpolation

做~自己de王妃 提交于 2019-12-21 07:29:30
问题 Why does string interpolation prefer overload of method with string instead of IFormattable ? Imagine following: static class Log { static void Debug(string message); static void Debug(IFormattable message); static bool IsDebugEnabled { get; } } I have objects with very expensive ToString() . Previously, I did following: if (Log.IsDebugEnabled) Log.Debug(string.Format("Message {0}", expensiveObject)); Now, I wanted to have the IsDebugEnabled logic inside Debug(IFormattable) , and call

using statement with static class is not working in visual studio 2015 CTP

被刻印的时光 ゝ 提交于 2019-12-21 04:02:41
问题 I have written a following code for one of my C# 6.0 Sample application. It was working fine earlier with Visual Studio 2015 Preview. But now when I have downloaded the newest version of Visual Studio 2015 which launched before some time.(http://blogs.msdn.com/b/bharry/archive/2015/01/16/visual-studio-2015-ctp-5-is-available.aspx) it stopped working. Following is a code for that. using System.Console; namespace StaticClassUsing { class Program { static void Main(string[] args) { WriteLine(

Why does interpolating a const string result in a compiler error?

谁都会走 提交于 2019-12-20 16:27:10
问题 Why does string interpolation in c# does not work with const strings? For example: private const string WEB_API_ROOT = "/private/WebApi/"; private const string WEB_API_PROJECT = $"{WEB_API_ROOT}project.json"; From my point of view, everything is known at compile time. Or is that a feature that will be added later? Compiler message: The expression being assigned to 'DynamicWebApiBuilder.WEB_API_PROJECT' must be constant. Thanks a lot! 回答1: Interpolated strings are simply converted to calls to

Null propagation operator and extension methods

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:18:32
问题 I've been looking at Visual Studio 14 CTP along with C# 6.0 and playing with the null-propagation operator. However, I couldn't find why the following code does not compile. The features are not yet documented so I'm not sure whether this is a bug or extension methods simply are not supported with the ?. operator and the error message is misleading. class C { public object Get() { return null; } } class CC { } static class CCExtensions { public static object Get(this CC c) { return null; } }