Generics in C#

Generic is a type used to define a class, structure, interface, or method with placeholders to indicate that they can store or use one or more of the types. In C#, the compiler will replace placeholders with the specified type at compile time. We use generic frequently with collections.

Generics are useful for improving code reusability, type safety, and performance compared with non-generic types e.g. arraylist.

Here is a code sample;

//Generic example - using single generic type
public class Hospital<T>            //Here <T> is a a placeholder beside Hospital class
{
    private T Cases;                //Declared a variable named "Cases" of type T
    public Hospital(T value)        //Constructor take another variable named value
    {
        this.Cases = value;         //assinged received type to containting type "Cases"
    }

    public void Show()
    {
        Console.WriteLine(this.Cases);
    }
}

Let’s implement this in our .NET6 main class;

Console.WriteLine("Hello, World!");

//use Hospital class
Hospital<int> x = new Hospital<int>(100);       //replacing T with int
Hospital<string> y = new Hospital<string>("Hospital Cases");
x.Show();
y.Show();

We can use two generic types in a class;

//using two generic types
public class EliteHospital<T, U>            //Here <T> is a a placeholder beside Hospital class
{
    // PatientCount of type T
    public T? PatientCount {  get; set; }

    //Docotrs of U
    public U? Doctors { get; set; }  
}

This is how we can use this;

Console.WriteLine("Hello, World!");
//using EliteHospital class
EliteHospital<int, string> xx = new EliteHospital<int, string>();
xx.PatientCount = 100;
xx.Doctors = "Available";
Console.WriteLine(xx.PatientCount);
Console.WriteLine(xx.Doctors);

Console.WriteLine("Press any key to continue..");
Console.ReadKey();

Tree View control with JavaScript and Knockout for large trees

Most of the existing tree views used to crash my browser, they are making the naive mistake to load all the nodes in one shot. The simple solution was to load the nodes on demand, and when you open a node with thousands of children, and then you open another node which again has thousands of children, it is better to close the previous node and let go all their children than crash the browser.

Read more here;

primitive vs reference types in JavaScript

For those that have never studied primitive vs reference types, let’s discuss the fundamental difference.

If a primitive type is assigned to a variable, we can think of that variable as containing the primitive value. Each primitive value is stored in a unique location in memory.

If we have two variables, x and y, and they both contain primitive data, then they are completely independent of each other:

let x = 2;
let y = 1;

x = y;
y = 100;
console.log(x); // 1 (even though y changed to 100, x is still 1)

This isn’t the case with reference types. Reference types refer to a memory location where the object is stored.

let point1 = { x: 1, y: 1 };
let point2 = point1;

point1.y = 100;
console.log(point2.y); // 100 (point1 and point2 refer to the same memory address where the point object is stored)

That was a quick overview of primary vs reference types.