Skip to content

Instantly share code, notes, and snippets.

@thebentern
Last active November 16, 2015 14:03
Show Gist options
  • Save thebentern/8765b164a086a0822a73 to your computer and use it in GitHub Desktop.
Save thebentern/8765b164a086a0822a73 to your computer and use it in GitHub Desktop.
C# 6 presentation
// Old busted
private int count = 0;
public int Count
{
get
{
return count;
}
set
{
count = value;
}
}
// New hotness
public int Count { get; set; } = 0;
//New hotness
Exception exception = null;
try
{
// Do some foo
}
catch (Exception ex)
{
exception = ex;
}
if (exception != null)
await AsyncLoggingService.Log(ex);
//New hotness
try
{
// Do some foo
}
catch (Exception ex)
{
await AsyncLoggingService.Log(ex);
}
try
{
//do some foo
}
catch(ApplicationException ex) when(ex.Message == "Bobbeh!")
{
WriteToBobbyLog(ex);
}
catch(ApplicationException ex) when(ex.Message == "Propane")
{
WriteToPropaneLog(ex);
}
// Old busted
public IEnumerable<int> ListOfNumbers
{
get
{
return Enumerable.Range(0,100);
}
}
// New hotness
public IEnumerable<int> ListOfNumbers => Enumerable.Range(0,100);
// Old busted
public void MyMethod(MyClass input)
{
if(input == null)
throw new ArgumentNullException("input");
doThing();
}
// New hotness
public void MyMethod(MyClass input)
{
if(input == null)
throw new ArgumentNullException(nameof(input));
doThang();
}
// Old busted
int count = 0;
if(response != null && response.Results != null)
count = response.Results.Count;
// New hotness
int count = response?.Results?.Count ?? 0;
public string Red = "FF";
public string Green = "99";
public string Blue = "00";
// Old busted
public string Color
{
get
{
return String.Format("{0}{1}{2}", Red, Green, Blue);
}
}
// New hotness
public string Color => $"{Red}{Green}{Blue}";
// Please don't do this. Use Path.Combine()
public string GetMyPath(string category, int id)
{
return $@"C:\MyDocuments\{category}\{id}";
}
// Old busted
public void DoSomething()
{
Console.WriteLine("Swiggity swooty!");
}
// New hotness
using static System.Console;
public void DoSomething()
{
WriteLine("Swiggity swooty!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment