Last active
August 29, 2015 14:25
-
-
Save fatbigbright/655bd7ea2cced7ec66c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace NewFeatures | |
{ | |
//1. Using Static Members | |
using static System.Math; | |
class Foo | |
{ | |
//0. Getter-only auto-properties & Initializer for auto-properties | |
//"private set" is not needed anymore for readonly properties | |
public string None { get; } = "Nothing"; | |
} | |
class Program | |
{ | |
//3. Expression Bodied properties | |
//equals to string GetHating(string a, string b) => { return $"{a} hates {b}."; } | |
static string GetHating(string a, string b) => $"{a} hates {b}."; | |
//4. Index initializer | |
static Dictionary<string, string> GetDic(string a, string b) => | |
new Dictionary<string, string> { | |
["love"] = $"{a} loves {b}." | |
,["hate"] = $"{a} hates {b}." | |
,["kill"] = $"{b} wanna kill {a}." | |
}; | |
static async void DemoException() | |
{ | |
//6. exception filter & await in try...catch...finally | |
try | |
{ | |
throw new Exception("Anyway"); | |
} | |
catch(Exception e) when (e.Message.Contains("Anyway")) | |
{ | |
await Task.Delay(1000); | |
Console.WriteLine("Exception Caught!"); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
//1. Using Static Members continued | |
//2. String Interpolation | |
Console.WriteLine($"Sqr(4)={Sqrt(4)}"); | |
//3. Expression Bodied properties continued | |
Console.WriteLine(GetHating("Megatron", "Optimus")); | |
//4. Null-Conditional Operator | |
//also available for event, such as "OnChanged?.Invoke(sender, args);" | |
Dictionary<string, string> dic = GetDic("Shingen", "Kenshin"); | |
if(dic["kill"]?.GetType() == typeof(String) && dic["kill"]?.Length > 0) | |
{ | |
Console.WriteLine(dic["kill"]); | |
} | |
//5. nameof operator | |
//refletion machanisum, when the name of an object changed, hard-coding of its name may be missed | |
//so nameof operator can help automatically get name of the object to avoid problem of hard-coding | |
Console.WriteLine($"name of {nameof(dic)} is {nameof(dic)}"); | |
//6. exception filter & await in try..catch...finally continued | |
DemoException(); | |
/**********************************/ | |
Console.WriteLine("This is the End!"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment