Skip to content

Instantly share code, notes, and snippets.

@thibaultlaurens
Last active May 1, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thibaultlaurens/4461585 to your computer and use it in GitHub Desktop.
Save thibaultlaurens/4461585 to your computer and use it in GitHub Desktop.
Oxford Brookes MSc - C# disassembly
interface ICustomInterface
{ }
public class Class2 : ICustomInterface
public class CustomList<T> where T : ICustomInterface
{
void AddToList(T element)
{}
}
public class Class1
{
public Class1()
{
CustomList<string> myCustomList1 = new CustomList<string>();
CustomList<Class2> myCustomList1 = new CustomList<Class2>();
}
}
{
// Code size 24 (0x18)
.maxstack 2
.locals init ([0] class [mscorlib]System.Collections.Generics.List`1<string> cars)
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: newobj instance void [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
IL_000b: stloc.0
IL_000c: ldloc.0
IL_000d: ldstr "bmw"
IL_0012: callvit instance void class [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
IL_0017: ret
} // end of method Class1::.ctor
public class List<T>
{
private T[] items;
private int size;
public List()
{
//the capacity of the list is not implemented in this simple version
this.items = new T[10];
}
public void Add(T item)
{
this.items[this.size++] = item;
}
public void Remove(T item)
{
//get the index of the item
int index = Array.IndexOf<T>(this.items, item, 0, this.size);
--this.size;
if (index < this.size)
//removes the item by puting elements after it at its index
Array.Copy((Array)this.items, index + 1, (Array)this.items, index, this.size - index);
this.items[this.size] = default(T);
}
// Gets or sets the element at the index
public T this[int index]
{
get
{
if (index >= this.size)
throw new Exception("Argument out of range");
return this.items[index];
}
set
{
if (index >= this.size)
throw new Exception("Argument out of range");
this.items[index] = value;
}
}
public int Count
{
get { return this.size; }
}
}
public class Car
{
public string Name { get; set; }
}
class Program
{
private static void Main(string[] args)
{
List<Car> CarList = new List<Car>();
Car bmw = new Car { Name = "BMW" };
Car mercedes = new Car { Name = "Mercedes" };
CarList.Add(bmw);
CarList.Add(mercedes);
CarList.Remove(mercedes);
Console.WriteLine("Number of cars in the list: " + CarList.Count);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment