Using static members:

Can use this to use enum members directly without prefixing the enum name everytime.

using static System.Math;

String interpolation:

A $ before a string means it is an interploated string.

return $"\({x}, {y}\)";

Expression-bodied methods:

Allows implementation of a method with a lambda and just a single expression.

public void AgeIncrement\(\) => Age++;

public override string ToString\(\) => $"\({x}, {y}\)";

public JObject ToJson\(\) => new JObject\(\) { \["x"\] = x, \["y"\] = y };

Expression-bodied properties:

The DogCreationTime is a read-only property.

Initialise a property with a method call.

public double Dist => Sqrt\(x \* x + y \* y\);

public string BackwardsName => new string\(Name.Reverse\(\).ToArray\(\)\);

public DateTime DogCreationTime { get; } = DateTime.Now;

public string SecretName { get; } = generateSecretName\(\);

public string SecretName => generateSecretName\(\); // Does this work, not sure, try it.



private static string generateSecretName\(\)

{

    Random rand = new Random\(\);

    int numLetters = rand.Next\(4, 13\);  // 4-12 characters

    string name = "";

    for \(int i = 1; i <= numLetters; i++\)

        name = name + \(char\)\('a' + rand.Next\(0, 26\)\);



    return name;

}

Null-conditional operators:

var a = b?.Name;

OnChanged?.Invoke\(this, args\);

Exception filters and await expressions:

try { ... }

catch \(ConfigurationException e\) when \(e.IsSevere\) { await LogAsync\(e\); }

catch \(ConfigurationException e\) if \(1 = 1\) { await LogAsync\(e\); }

finally { await CloseAsync\(\); }

results matching ""

    No results matching ""