Create a gist now

Instantly share code, notes, and snippets.

C# 6 new features
using System;
using System.Collections.Generic;
using System.Diagnostics;
using static System.Console;
namespace PlayingWithCSharp6
{
class Program
{
static void Main(string[] args)
{
StringInterpolationEnhancements();
UsingStaticClassesDemo();
NullPropgationFeature(new Student { Name = "Adil" });
DictionaryInitializer();
Console.ReadLine();
}
public static void StringInterpolationEnhancements()
{
string name = "Adil";
DateTime date = new DateTime(2015, 03, 13, 10, 15, 0);
int versionYear = 2015;
// Previously in C# 5
WriteLine(string.Format("This code was written by {0} on {1} using Visual Studio {2}", name, date, versionYear));
// In C# 6
WriteLine(string.Format($"This code was written by {name} on {date} using Visual Studio {versionYear}"));
// Infact you don't need to call string.format
WriteLine($"This code was written by {name} on {date} using Visual Studio {versionYear}");
// This is especially handy when you add more parameters in string format over the time and the order get confusing
string content = "blog";
WriteLine(string.Format("This {3} was written by {0} on {1} using Visual Studio {2}", name, date, versionYear, content));
WriteLine($"This {content} was written by {name} on {date} using Visual Studio {versionYear}");
}
public static void UsingStaticClassesDemo()
{
//Previously in C# 5
Console.WriteLine("This is how to use static classes");
// In C# 6 by adding namespace
// using static System.Console;
WriteLine("Just call the static method directly");
}
public static void NullPropgationFeature(Student student)
{
// Previously in C# 5
if (student != null)
{
WriteLine(student.Name);
if (student.Courses != null)
{
foreach (var course in student.Courses)
{
WriteLine(course.Id);
}
}
}
//In C# 6
WriteLine(student?.Name);
if (student.Courses != null)
{
foreach (var course in student.Courses)
{
WriteLine(course.Id);
}
}
}
private static void DictionaryInitializer()
{
Dictionary<string, int> dictionary = new Dictionary<string, int> {["Year"] = 2015,["Month"] = 03,["Day"] = 15 };
}
public class OtherEnhancements
{
// A getter only property with readyonly backing field
public int DemoInt { get; }
// An auto property intialized with default value
public string DemoString { get; set; } = "Demo";
public OtherEnhancements()
{
// Assignment in constructor for getter only field behaves like readonly field
this.DemoInt = 100;
}
public void DemoMethod()
{
try
{
}
// catch exception with filters
catch (System.IO.FileNotFoundException exception) when (exception.FileName == "abc.txt")
{
// handle only for particular file
// In previous version of C#, you have to check it inside
// and then re throw which would result in losing exception data
}
}
//Expression-bodied members
public override string ToString() => $"Value : {DemoInt}";
}
public class Student
{
public string Name { get; set; }
public IList<Course> Courses { get; set; }
}
public class Course
{
public int Id { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment