You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var x = 1;
// same as string.Format("A {0} B", x)
var y1 = $"A {x} B"; // y == "A 1 B"
// $@ for multiline
var y2 = $@"A
{x}
B";
// Formatting
var y3 = $"A {1.23:F1} B"; // y == "A 1.2 B"
Operator ?.
// same as (person != null ? person.Name : null)
var name1 = person?.Name;
// same as (person != null ? person.Name : "(Unknown)")
var name2 = person?.Name ?? "(Unknown)";
Using static
using static System.Console;
public class Program {
public static void Main(string[] args) {
WriteLine("Test");
ReadKey();
}
}
// methods (same as Sum(int a, int b) { return a + b; })
public int Sum(int a, int b) => a + b;
// properties (same as Length { get { return End – Start; } })
public string Length => End - Start;
nameof()
public void M() {}
public void X() {
// Good for refactoring, compiler-verified:
Console.WriteLine(nameof(M)); // => M
}
Index initializers
var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
is actually the same as
because when person.Name is null it will also use "(Unknown)"