Tutorial by Examples: a

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...
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; } }
public class Dog { private const string _birthStringFormat = "yyyy-MM-dd"; [XmlIgnore] public DateTime Birth {get; set;} [XmlElement(ElementName="Birth")] public string BirthString { get { return Birth.ToString(_birthStringFormat); } ...
string requestUri = "http://www.example.com"; string responseData; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(parameters.Uri); WebResponse response = request.GetResponse(); using (StreamReader responseReader = new StreamReader(response.GetResponseStream())) { ...
string requestUri = "http://www.example.com"; string responseData; using (var client = new WebClient()) { responseData = client.DownloadString(requestUri); }
HttpClient is available through NuGet: Microsoft HTTP Client Libraries. string requestUri = "http://www.example.com"; string responseData; using (var client = new HttpClient()) { using(var response = client.GetAsync(requestUri).Result) { response.EnsureSuccessStatus...
string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri) { Method = reque...

Page 3 of 1099