Created
January 21, 2016 01:15
-
-
Save justinAurand/a22af2865ea26007f94c to your computer and use it in GitHub Desktop.
C# 6.0 new features in a fairly simple C# file.
This file contains 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
// C#6 feature: Auto-property Initializer | |
public Guid Id { get; private set; } = Guid.NewGuid(); | |
// C#6 feature: Auto-property Initializer (Getter only) | |
public Guid Id { get; } = Guid.NewGuid(); | |
// C#6 feature: String Interpolation | |
string test = $"{this.FirstName} {this.LastName} ({this.Id})"; | |
FormattableString test2 = $"{this.FirstName} {this.LastName} ({this.Id})"; | |
Console.WriteLine(test2.ArgumentCount + " " + test2.Format); | |
// C#6 feature: Using Static | |
using static System.Console; | |
class Program | |
{ | |
static void Main() | |
{ | |
WriteLine("LungButter"); | |
} | |
} | |
// C#6 feature: Expression Bodied Member (property) | |
public string FullName | |
{ | |
get | |
{ | |
return $"{this.FirstName} {this.LastName}"; | |
} | |
} | |
// ...can now be... | |
public string FullName => $"{this.FirstName} {this.LastName}"; | |
// This only works for one liners! | |
// C#6 feature: Expression Bodied Member (void method) | |
public void Echo(string message) => Console.WriteLine($"This message is: {message}"); | |
// This only works for one liners! | |
// C#6 feature: Expression Bodied Member (method that returns) | |
public string Yell(string message) => message.ToUpper(); | |
// This only works for one liners! | |
// C#6 feature: Dictionary Initializer | |
var contacts = new Dictionary<int, Contact> | |
{ | |
[1] = new Contact { FirstName = "Justin", LastName = "Aurand" }, | |
[2] = new Contact { FirstName = "Jerk", LastName = "Face" } | |
}; | |
// C#6 features: Add Extension (for collection initiatlizer) | |
public static Contact Add(this ContactsCollection list, Contact contact) | |
{ | |
return list.Insert(contact); | |
} | |
// Now you can use inline initialization for your ContactsCollection class! | |
// C#6 feature: Null Conditional Access | |
public static bool IsMaxLengthValid(this Contact contact) | |
{ | |
var length = contact?.LastName?.Length ?? 0; | |
return length <= 20; | |
} | |
// C#6 feature: nameof Operator | |
public static string GetFirstName(this Contact contact) | |
{ | |
if (contact == null) | |
throw new ArgumentNullException(nameof(contact)); | |
return contact.FirstName; | |
} | |
// C#6 feature: Await Inside Catch Block (works for finally, too!) | |
private static async void ReadFileAsync() | |
{ | |
var filename = "test.txt"; | |
try | |
{ | |
var result = await FileUtil.ReadFileAsync(filename); | |
Console.WriteLine(result); | |
} | |
catch (Exception) | |
{ | |
await FileUtil.LogAsync("Unknown Error."); | |
} | |
} | |
// C#6 feature: Exception Filter | |
private static async void ReadFileAsync() | |
{ | |
var filename = "test.txt"; | |
try | |
{ | |
var result = await FileUtil.ReadFileAsync(filename); | |
Console.WriteLine(result); | |
} | |
catch (FileNotFoundException ex) when (ex.FileName.EndsWith(".txt")) | |
{ | |
await FileUtil.LogAsync($"File not found: {ex.FileName}"); | |
} | |
catch (Exception) | |
{ | |
await FileUtil.LogAsync("Unknown Error."); | |
} | |
} | |
// You can download the Rosyln C# compiler as a Nuget package. | |
static void RoslynDemo() | |
{ | |
var code = File.ReadAllText("Person.cs"); | |
var tree = SyntaxFactory.ParseSyntaxTree(code); | |
// Create compilation | |
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); | |
var reference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); | |
var compilation = CSharpCompliation.Create("test") | |
.WithOptions(options) | |
.AddReferences(reference) | |
.AddSyntaxTrees(tree); | |
// Show Diagnostics | |
var diagnostics = compilation.GetDiagnostics(); | |
foreach (var diagnostic in diagnostics) | |
Console.WriteLine(diagnostic.ToString()); | |
// Execute Code | |
using (var stream = new MemoryStream()) | |
{ | |
compilation.Emit(stream); | |
var assembly = Assembly.Load(stream.GetBuffer()); | |
var type = assembly.GetType("CS60Demo.Person"); | |
dynamic Person = Activator.CreateInstance(type); | |
person.SayHello(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment