C# Feature History
/// <summary>
/// This class performs an important function.
/// </summary>
// Declare a delegate:
delegate void Del(int x);
// Define a named method:
void DoWork(int k) { /* ... */ }
// Instantiate the delegate using the method as a parameter:
Del d = new Del(DoWork);
public event Del MyEvent
{
add
{
Console.WriteLine("Listener added!");
_myEvent += value;
}
remove
{
Console.WriteLine("Listener removed!");
_myEvent -= value;
}
}
® public class Stack<T>
® partial class A
// Create a delegate.
delegate void Del(int x);
// Instantiate the delegate using an anonymous method.
Del d = delegate(int k) { /* ... */ };
® The most common way to create an iterator is to implement the GetEnumerator method on the IEnumerable interface
® Then the type is usable in foreach statement
® Nullable types are instances of the System.Nullable<T> struct.
® int? num = null;
® The syntax T? is shorthand for Nullable<T>, where T is a value type.
public string Name
{
get { return m_name; }
protected set { m_name = value; }
}
® simplifies the syntax that assigns a method to a delegate.
® In delegate sample from C# 1.0, we can simplify
◊ Del d = DoWork
® covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments. Covariance preserves assignment compatibility and contravariance reverses it.
◊ Covariance
public delegate Mammals HandlerMethod();
// Covariance enables this assignment. Considering Dogs inherits from Mammals, and the following methods exist
// public static Mammals MammalsHandler()
// public static Dogs DogsHandler()
HandlerMethod handlerDogs = DogsHandler;
◊ Contravariance
// Event hander that accepts a parameter of the EventArgs type.
//private void MultiHandler(object sender, System.EventArgs e) // although the event expects the KeyEventArgs parameter. this.button1.KeyDown += this.MultiHandler; // for an event that expects the MouseEventArgs parameter. this.button1.MouseClick += this.MultiHandler;
® A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
® var x= new int[50];
® Object examples
◊ Cat cat = new Cat { Age = 10, Name = "Fluffy" };
◊ var pet = new { Age = 10, Name = "Fluffy" };
◊ new {p.ProductName, Price = p.UnitPrice};
® Collection
◊ List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() };
® public double TotalPurchases { get; set; }
® var person = new { firstName = "John", lastName = "Smith" };
® Public static MyCustomType ConvertToMyCustomType(this string obj);
® "an string".ConvertToMyCustomType();
from score in scores
where score > 80
select score;
® A lambda expression is an anonymous function that you can use to create delegates or expression tree types.
® More notes
® Expression<Func<int, bool>> lambda = num => num < 5;
® lambda.Compile()(8);
® Signatures in both parts of the partial type must match.
® The method must return void.
® No access modifiers are allowed. Partial methods are implicitly private.
using System.Dynamic;
public class Table : DynamicObject
{
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
Console.WriteLine(binder.Name + "method was dynamically called");
® CalculateBMI(weight: 123, height: 64)
® static int CalculateBMI(int weight, int height=128)
® A tuple is a data structure that has a specific number and sequence of elements
® var tuple = Tuple.Create("cat", 2, true);
® Covariance: i.e. You can assign an instance of IEnumerable<Derived> to a variable of type IEnumerable<Base>.
® Contravariance: i.e. You can assign an instance of IEnumerable<Base> to a variable of type IEnumerable<Derived>.
® Using Task class + async and await keywords for implementing/using the methods which will run in background
® More info
® help in tracking information about the caller
public static void ShowCallerInfo([CallerMemberName]
string callerName = null, [CallerFilePath] string
callerFilePath = null, [CallerLineNumber] int callerLine=-1)
{
® You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio!
® using static System.Console;
® WriteLine("Hello, World!");
® catch (Exception e) when (Thread.CurrentPrincipal.Identity.Name == "JBOGARD") {
® Allowed to use await in catch and finally block
® public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
® public DateTime TimeStamp { get; } = DateTime.UtcNow;
® public int Area => Length * Width;
® return value?.Substring(
® An interpolated string expression looks like a template string that contains expressions.
® String s= $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ? "" : "s")} old."
® WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode”
var myDict = new Dictionary<int, string>
{
[1] = "Pankaj",
[2] = "Pankaj",
[3] = "Pankaj"
};