2. Fiyaz Hasan (Fizz)
Intern, Microsoft Bangladesh.
Living in Dhaka. Founder of Geek Hour, Technical Blog Writer, Gamer, Guitarist and of
course a Software Engineer.
www.fiyazhasan.me
www.facebook.com/alsoknownasfizz
3. StaticNamespaceasusing using System;
namespace NewInCSharp
{
class Program
{
static void Main(string[] args)
{
MySelf.WhoAmI();
Console.ReadKey();
}
}
static class MySelf
{
public static void WhoAmI()
{
Console.WriteLine("I'm Fizz!");
}
}
}
using static System.Console;
using static NewInCSharp.MySelf;
namespace NewInCSharp
{
class Program
{
static void Main(string[] args)
{
WhoAmI();
ReadKey();
}
}
static class MySelf
{
public static void WhoAmI()
{
WriteLine("I'm Fizz!");
}
}
}
static namespace using
4. StringInterpolation using System;
using System.Collections.Generic;
using static System.Console;
namespace NewInCSharp
{
class Program
{
private static void Main(string[]
args)
{
string name = "Murphy Cooper";
string planet = "Cooper Station";
WriteLine("{0} is actually named
after {1}", planet, name);
ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using static System.Console;
namespace NewInCSharp
{
class Program
{
private static void Main(string[]
args)
{
string name = "Murphy Cooper";
string planet = "Cooper Station";
WriteLine($"{planet} is actually
named after {name}");
ReadLine();
}
}
} String interpolation
5. DictionaryInitializers using System.Collections.Generic;
using static System.Console;
namespace NewInCSharp
{
class Program
{
private static void Main(string[] args)
{
Dictionary<string, string> alien = new Dictionary<string, string>()
{
{"Name", "Fizzy"},
{"Planet", "Kepler-452b"}
};
foreach (KeyValuePair<string, string> keyValuePair in alien)
{
WriteLine(keyValuePair.Key + ": " + keyValuePair.Value + "n");
}
ReadLine();
}
}
}
6. DictionaryInitializers(continues) using System.Collections.Generic;
using static System.Console;
namespace NewInCSharp
{
class Program
{
private static void Main(string[] args)
{
Dictionary<string, string> alien = new Dictionary<string, string>()
{
["Name"] = "Fizzy",
["Planet"] = "Kepler-452b"
};
foreach (KeyValuePair<string, string> keyValuePair in alien)
{
WriteLine(keyValuePair.Key + ": " + keyValuePair.Value + "n");
}
ReadLine();
}
}
}
New way of initializing dictionary
7. Auto-PropertyInitializers using static System.Console;
namespace NewInCSharp
{
class Program
{
static void Main(string[] args)
{
Employee employee = new Employee();
employee.Name = "Bill Gates";
WriteLine("Name: " + employee.Name + "nSalary: " + employee.Salary);
ReadKey();
}
public class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; }
public Employee()
{
Salary = 10000;
}
}
}
}
8. Auto-PropertyInitializers(continues) using static System.Console;
namespace NewInCSharp
{
class Program
{
static void Main(string[] args)
{
Employee employee = new Employee();
employee.Name = " Bill Gates";
WriteLine("Name: " + employee.Name + "nSalary: " + employee.Salary);
ReadKey();
}
public class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; } = 10000;
}
}
} Constructor gone & Initialized
Property
9. nameofexpression class Program
{
private static void Main(string[] args)
{
try
{
CallSomething();
}
catch (Exception exception)
{
WriteLine(exception.Message);
}
ReadKey();
}
private static void CallSomething()
{
int? number = null;
if (number == null)
{
throw new Exception(nameof(number) +
" is null");
}
}
} Good for Refactoring
class Program
{
private static void Main(string[] args)
{
try
{
CallSomething();
}
catch (Exception exception)
{
WriteLine(exception.Message);
}
ReadKey();
}
private static void CallSomething()
{
int? x = null;
if (x == null)
{
throw new Exception("x is null");
}
}
}
10. awaitincatch/finally private static void Main(string[] args)
{
Task.Factory.StartNew(() => GetWeather());
Console.ReadKey();
}
private async static Task GetWeather()
{
HttpClient client = new HttpClient();
try
{
var result = await client.GetStringAsync("http://api.openweathermap.org/data/2.5/weather?q=Dhaka,
Console.WriteLine(result);
}
catch (Exception exception)
{
try
{
var result = await client.GetStringAsync("http://api.openweathermap.org/data/2.5/weather?q=Ne
Console.WriteLine(result);
}
catch (Exception)
{
throw;
}
}
}
Wasn’t possible in C# 5.0 but now
ALL IS WELL in C# 6.0
11. class Program
{
private static void Main(string[] args)
{
SuperHero hero = new SuperHero();
if (hero.SuperPower == String.Empty)
{
hero = null;
}
Console.WriteLine(hero != null ?
hero.SuperPower : "You aint a super hero.");
Console.ReadLine();
}
}
public class SuperHero
{
public string SuperPower { get; set; } = "";
}
Nullconditionaloperator class Program
{
private static void Main(string[] args)
{
SuperHero hero = new SuperHero();
if (hero.SuperPower == String.Empty)
{
hero = null;
}
Console.WriteLine(hero?.SuperPower ??
"You aint a super hero.");
Console.ReadLine();
}
}
public class SuperHero
{
public string SuperPower { get; set; } = "";
}
12. class Program
{
private static void Main(string[] args)
{
List<SuperHero> superHeroes = null;
SuperHero hero = new SuperHero();
if (hero.SuperPower != String.Empty)
{
superHeroes = new List<SuperHero>();
superHeroes.Add(hero);
}
Console.WriteLine(superHeroes != null ? superHeroes[0].SuperPower : "There is no such thing as
super heroes.");
Console.ReadLine();
}
}
public class SuperHero
{
public string SuperPower { get; set; } = "";
}
Nullconditionaloperator(continues)
13. class Program
{
private static void Main(string[] args)
{
List<SuperHero> superHeroes = null;
SuperHero hero = new SuperHero();
if (hero.SuperPower != String.Empty)
{
superHeroes = new List<SuperHero>();
superHeroes.Add(hero);
}
Console.WriteLine(superHeroes?[0].SuperPower ?? "There is no such thing as super heros.");
Console.ReadLine();
}
}
public class SuperHero
{
public string SuperPower { get; set; } = "";
}
Nullconditionaloperator(continues)
14. public class Movie : INotifyPropertyChanged
{
public string Title { get; set; }
public int Rating { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Nullpropagation
handler?.Invoke(this, new PropertyChangedEventArgs(name));
Null Propagation
15. internal class Program
{
private static void Main(string[] args)
{
double x = 1.618;
double y = 3.142;
Console.WriteLine(AddNumbers(x, y));
Console.ReadLine();
}
private static double AddNumbers(double x, double y)
{
return x + y;
}
}
Expressionbodies(onmethod)
private static double AddNumbers(double x, double y) => x + y;
New expression bodied function syntax
16. internal class Program
{
private static void Main(string[] args)
{
Person person = new Person();
WriteLine("I'm " + person.FullName);
ReadLine();
}
public class Person
{
public string FirstName { get; } = "Fiyaz";
public string LastName { get; } = "Hasan";
public string FullName => FirstName + " " + LastName;
}
}
Expressionbodies(onproperty)
Expression bodies on property-like function members
17. internal class Program
{
private static void Main(string[] args)
{
Shape shape = new Shape();
ShapeUtility.GenerateRandomSides(shape);
WriteLine(shape.IsPolygon());
ReadLine();
}
}
public class Shape
{
public int Sides { get; set; }
}
public static class ShapeUtility
{
public static bool IsPolygon(this Shape shape)
{
return shape.Sides >= 3;
}
public static void GenerateRandomSides(Shape shape)
{
Random random = new Random();
shape.Sides = random.Next(1, 6);
}
}
StaticusingwithExtensionmethod
Extension Method
Static Method
18. internal class Program
{
private static void Main(string[] args)
{
Shape shape = new Shape();
GenerateRandomSides(shape);
WriteLine(shape.IsPolygon());
ReadLine();
}
}
public class Shape
{
public int Sides { get; set; }
}
public static class ShapeUtility
{
public static bool IsPolygon(this Shape shape)
{
return shape.Sides >= 3;
}
public static void GenerateRandomSides(Shape shape)
{
Random random = new Random();
shape.Sides = random.Next(1, 6);
}
StaticusingswithExtensionmethod
(continues)
using static NewInCSharp.ShapeUtility;
Since extension methods are static
methods, if static using are used then
extension methods cant be found in
global scope. So they are imported as
extension methods. And you can’t just
invoke them like normal static functions