Tutorial by Examples: e

When a type is defined without a constructor: public class Animal { } then the compiler generates a default constructor equivalent to the following: public class Animal { public Animal() {} } The definition of any constructor for the type will suppress the default constructor genera...
public class Animal { public string Name { get; set; } public Animal() : this("Dog") { } public Animal(string name) { Name = name; } } var dog = new Animal(); // dog.Name will be set to "Dog" by default. var cat = new Ani...
Adding the volatile keyword to a field indicates to the compiler that the field's value may be changed by multiple separate threads. The primary purpose of the volatile keyword is to prevent compiler optimizations that assume only single-threaded access. Using volatile ensures that the value of the ...
The fixed statement fixes memory in one location. Objects in memory are usually moving arround, this makes garbage collection possible. But when we use unsafe pointers to memory addresses, that memory must not be moved. We use the fixed statement to ensure that the garbage collector does not relo...
Declaration: class MyGenericClass<T1, T2, T3, ...> { // Do something with the type parameters. } Initialisation: var x = new MyGenericClass<int, char, bool>(); Usage (as the type of a parameter): void AnotherMethod(MyGenericClass<float, byte, char> arg) { ... } ...
Declaration: void MyGenericMethod<T1, T2, T3>(T1 a, T2 b, T3 c) { // Do something with the type parameters. } Invocation: There is no need to supply type arguements to a genric method, because the compiler can implicitly infer the type. int x =10; int y =20; string z = "tes...
Declaration: interface IMyGenericInterface<T1, T2, T3, ...> { ... } Usage (in inheritance): class ClassA<T1, T2, T3> : IMyGenericInterface<T1, T2, T3> { ... } class ClassB<T1, T2> : IMyGenericInterface<T1, T2, int> { ... } class ClassC<T1> : IMyGenericI...
A constructor of a base class is called before a constructor of a derived class is executed. For example, if Mammal extends Animal, then the code contained in the constructor of Animal is called first when creating an instance of a Mammal. If a derived class doesn't explicitly specify which constru...
For an instance of a type: var theString = "hello"; var theType = theString.GetType(); From the type itself: var theType = typeof(string);
using System; using System.Reflection; using System.Linq; public class Program { public static void Main() { var members = typeof(object) .GetMembers(BindingFlags.Public | BindingFlags.Static | ...
Get Instance method and invoke it using System; public class Program { public static void Main() { var theString = "hello"; var method = theString .GetType() .GetMethod("Substring", ...
When an object graph is finalized, the order is the reverse of the construction. E.g. the super-type is finalized before the base-type as the following code demonstrates: class TheBaseClass { ~TheBaseClass() { Console.WriteLine("Base class finalized!"); } } ...
To avoid duplicating code, define common methods and attributes in a general class as a base: public class Animal { public string Name { get; set; } // Methods and attributes common to all animals public void Eat(Object dinner) { // ... } public void Stare()...
An interface is used to enforce the presence of a method in any class that 'implements' it. The interface is defined with the keyword interface and a class can 'implement' it by adding : InterfaceName after the class name. A class can implement multiple interfaces by separating each interface with ...
public class Animal { public string Name { get; set; } } public interface INoiseMaker { string MakeNoise(); } //Note that in C#, the base class name must come before the interface names public class Cat : Animal, INoiseMaker { public Cat() { Name = "Ca...
Returns an int holding the size of a type* in bytes. sizeof(bool) // Returns 1. sizeof(byte) // Returns 1. sizeof(sbyte) // Returns 1. sizeof(char) // Returns 2. sizeof(short) // Returns 2. sizeof(ushort) // Returns 2. sizeof(int) // Returns 4. sizeof(uint) // Returns 4....
public void SerializeFoo(string fileName, Foo foo) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.Open(fileName, FileMode.Create)) { serializer.Serialize(stream, foo); } }
public Foo DeserializeFoo(string fileName) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.OpenRead(fileName)) { return (Foo)serializer.Deserialize(stream); } }
<Foo> <Dog/> </Foo> public class Foo { // Using XmlElement [XmlElement(Name="Dog")] public Animal Cat { get; set; } }
<Store> <Articles> <Product/> <Product/> </Articles> </Store> public class Store { [XmlArray("Articles")] public List<Product> Products {get; set; } }

Page 3 of 1191