Primitive Types | |
Boolean : true or false | Int16, Int32, Int64 : signed integer values |
Byte :0 to 255 | Uint16, UInt32, Uint64 : unsigned integer values |
SByte : signed byte values from -128 t0 127 | Singe: 32-bit single-precision floating-point number |
Char : 16-bit Unicode character | Double: 64-bit double-precision floating-point number |
Value Types | Reference Types |
-Structures | -Classes |
-Enumerations | -Interfaces |
-Delegates |
Interfaces
-Defines a set of members(methods,properties,events)
-Add the interface in struct or class
-A class can inherit one base class and implement many interfaces
-A structure can not inherit from another structure,but it can implement many interfaces
public enum Cars { BMW = 4, Lexus = 3, Mercedes = 2, Infinity = 1 }
public enum Gearbox { Auto = 1, Manual = 2, Triptronic = 3 }
public struct Race : IComparable<Race>
{
public Cars Car { get; set; }
public Gearbox Gearbox { get; set; }
public int CompareTo(Race other)
{
int sortOrder = other.Car.CompareTo(this.Car);
if (sortOrder == 0)
{
sortOrder = other.Gearbox.CompareTo(this.Gearbox);
}
return sortOrder;
}
}
Customer Interface Ideas
-Dependency injection scenarios
-Mock dependencies in unit tests
Generics
public class ObjectContainer { | public class IntContainer { |
Same logic with Generics
public class Container<T> {
int position;
T[] items = new T[10];
public void Increase(T item){
items[position++] = item;
}
public T Decrease(){
return items[--position];
}
}
//usage
var container = new Container<string>();
container.Increase("Four");
container.Increase("Six");
string result = container.Decrease();
Workflow
1.Defines generic with placeholders(type parameters)
2.Specific concrete types(type arguments) upon use
3.Type arguments resolved at compile time
Constraint Type | Details |
where T : struct | Must be a value type |
where T : class | Must be a reference type |
where T : <base class> | Must inherit from base class |
where T : <interface> | Must implement the interface |
where T : U | There are 2 type arguments(T and U).T must inherit from U |
where T : new() | Must have a public parameterless constructor |
Delegates and Lambda Expressions
-Delegates are the .NET type-safe version of function pointers(C# 9 has true function pointers)
Action Delegates : Represent a method that returns void
Func Delegates : Represent a method that has a return type
EventHandler Delegates : Intended for event signatures
public void Calculate()
{
Func<int, int, int> sampleDelegate =
new Func<int, int, int>(SampleMethod);
Console.WriteLine(sampleDelegate.Invoke(6, 5));//11
Console.WriteLine(sampleDelegate(6, 5)); //11
//use var to infer variable type
var sampleDelegate2 = new Func<int, int, int>(SampleMethod);
//use C# lambda expression to inline code
var sampleDelegate3 = new Func<int, int, int>((first,second)
=> first + second);
}
public int SampleMethod(int first, int second){
return first + second;
}
Reflection and Attributes
Reflection : Look inside. .NET assembly and discover what types and metadata are there
1.Load an assembly
2.List type members(methods,properties,fields), list method parameters(return,in,out)
Why use reflection to invoke a method when you can reference directly instead?
Dynamically extensible applications
Attribute : A tag that can be applied to parts of your code.
Tools can use reflection to find these attributes and provide services
Targets -> Class, Struct , Enum, Interface , Delegate, Field, Property, Event
Attributes are useless without a corresponding library or tool
Attributes are always paired with a code library
It’s appropriate time to make some plans for the future and it is time to be happy.
I have read this post and if I could I desire to
suggest you some interesting things or advice.
Perhaps you can write next articles referring to this article.
I wish to read more things about it!