Essential Types in .NET

By | June 16, 2023

Primitive Types
Boolean : true or falseInt16, Int32, Int64 : signed integer values
Byte :0 to 255Uint16, UInt32, Uint64 : unsigned integer values
SByte : signed byte values from -128 t0 127Singe: 32-bit single-precision floating-point number
Char : 16-bit Unicode characterDouble: 64-bit double-precision floating-point number
Value TypesReference 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 {
int position;
object[] items = new object[10];

public void Increase(object item){
items[position++] = item;
}

public object Decrease(){
return items[--position];
}
}

//usage
var container = new ObjectContainer();
container.Increase(4); //boxing the int into an object
container.Increase(6);
int result = (int)container.Decrease();
container.Increase("Four");
container.Increase("Six");
string result2 = container.Decrease().ToString();
public class IntContainer {
int position;
int[] items = new int[10];

public void Increase(int item){
items[position++] = item;
}

public int Decrease(){
return items[--position];
}
}

//usage
var container = new IntContainer();
container.Increase(4); //no boxing
container.Increase(6);
int result = container.Decrease(); //no cast needed
//container.Increase("Four"); //not allowed

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 TypeDetails
where T : structMust be a value type
where T : classMust be a reference type
where T : <base class>Must inherit from base class
where T : <interface>Must implement the interface
where T : UThere 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

One thought on “Essential Types in .NET

  1. Anthony Platt

    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!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *