Difference between nameof and typeof

Two reasons: nameof turns into a compile-time constant. typeof(…).Name requires a bit of reflection. It’s not overly expensive, but it can hurt in some cases. Second, it’s used for other things than type names. For example, arguments: void SomeMethod(int myArgument) { Debug.WriteLine(nameof(myArgument)); } You can also get the name of class members and even locals. … Read more

What does the => operator mean in a property or method?

What you’re looking at is an expression-bodied member not a lambda expression. When the compiler encounters an expression-bodied property member, it essentially converts it to a getter like this: public int MaxHealth { get { return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; } } (You can verify this for yourself by pumping the code into a … Read more

Public readonly field v.s. get-only property

Making it a property rather than a field means it can be used on interfaces. The exact implementation (although auto-properties don’t really have much implementation…) is also abstracted, so you could in the future base it on a combination of fields without breaking (compile) compatibility.

Why is the execution order of inner ‘finally’ and outer ‘when’ swapped in C# 6.0?

You might have been taught that when exception handling occurs, every method is considered separately. That is, since your inner method has a try…finally, any exception will first trigger the finally, and then it will “look” for a try higher up. This isn’t true. From the ECMA specification of CLR (ECMA-335, I.12.4.2.5 Overview of exception … Read more

Error CS1056: Unexpected character ‘$’ running the msbuild on a tfs continuous integration process

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers. Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line That feature is a syntactic sugar for C#6, try to install the latest version of the framework 4.6.2 https://www.microsoft.com/en-us/download/details.aspx?id=53345 Then go to your Project properties … Read more

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

Interpolated strings are simply converted to calls to string.Format. So your above line actually reads private const string WEB_API_PROJECT = string.Format(“{0}project.json”, WEB_API_ROOT); And this is not compile time constant as a method call is included. On the other hand, string concatenation (of simple, constant string literals) can be done by the compiler, so this will … Read more

How to initialise ReadOnlyDictionary?

If you don’t mind having an IReadOnlyDictionary instead of a ReadOnlyDictionary, you could use this, since Dictionary implements IReadOnlyDictionary: private static IReadOnlyDictionary<string, byte> _validRevisions = new Dictionary<string, byte> { { “1.0”, 0x00 }, { “1.1”, 0x01 }, { “1.2”, 0x02 }, { “1.3”, 0x03 } }; public static IReadOnlyDictionary<string, byte> ValidRevisions => _validRevisions;

Why does nameof return only last name?

Note that if you need/want the “full” name, you could do this: $”{nameof(order)}.{nameof(User)}.{nameof(Age)}”.GetLastName(); as long as all of these names are in the current scope. Obviously in this case it’s not really all that helpful (the names won’t be in scope in the Razor call), but it might be if you needed, for example, the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)