Tutorial by Examples

Classes inherit from System.Object, are reference types, and live on the heap. When reference types are passed as a parameter, they are passed by reference. public Class MyClass { public int a; public int b; } Passed by reference means that a reference to the parameter is passed to t...
An enum is a special type of class. The enum keyword tells the compiler that this class inherits from the abstract System.Enum class. Enums are used for distinct lists of items. public enum MyEnum { Monday = 1, Tuesday, Wednesday, //... } You can think of an enum as a conve...
var dateString = "2015-11-24"; var date = DateTime.ParseExact(dateString, "yyyy-MM-dd", null); Console.WriteLine(date); 11/24/2015 12:00:00 AM Note that passing CultureInfo.CurrentCulture as the third parameter is identical to passing null. Or, you can pass a specific...
When passing formal arguments to a generic method, relevant generic type arguments can usually be inferred implicitly. If all generic type can be inferred, then specifying them in the syntax is optional. Consider the following generic method. It has one formal parameter and one generic type paramet...
Type constraints are able to force a type parameter to implement a certain interface or class. interface IType; interface IAnotherType; // T must be a subtype of IType interface IGeneric<T> where T : IType { } // T must be a subtype of IType class Generic<T> where T...
When we talk about the GC and the "heap", we're really talking about what's called the managed heap. Objects on the managed heap can access resources not on the managed heap, for example, when writing to or reading from a file. Unexpected behavior can occur when, a file is opened for readi...
It is possible to specify whether or not the type argument should be a reference type or a value type by using the respective constraints class or struct. If these constraints are used, they must be defined before all other constraints (for example a parent type or new()) can be listed. // TRef mus...
By using the new() constraint, it is possible to enforce type parameters to define an empty (default) constructor. class Foo { public Foo () { } } class Bar { public Bar (string s) { ... } } class Factory<T> where T : new() { public T Create() { re...
Every method has a unique signature consisting of a accessor (public, private, ...) ,optional modifier (abstract), a name and if needed method parameters. Note, that the return type is not part of the signature. A method prototype looks like the following: AccessModifier OptionalModifier ReturnTyp...
Calling a static method: // Single argument System.Console.WriteLine("Hello World"); // Multiple arguments string name = "User"; System.Console.WriteLine("Hello, {0}!", name); Calling a static method and storing its return value: string input = System.Con...
A method can declare any number of parameters (in this example, i, s and o are the parameters): static void DoSomething(int i, string s, object o) { Console.WriteLine(String.Format("i={0}, s={1}, o={2}", i, s, o)); } Parameters can be used to pass values into a method, so that th...
A method can return either nothing (void), or a value of a specified type: // If you don't want to return a value, use void as return type. static void ReturnsNothing() { Console.WriteLine("Returns nothing"); } // If you want to return a value, you need to specify its type. st...
You can use default parameters if you want to provide the option to leave out parameters: static void SaySomething(string what = "ehh") { Console.WriteLine(what); } static void Main() { // prints "hello" SaySomething("hello"); // prints &quot...
The yield keyword is used to define a function which returns an IEnumerable or IEnumerator (as well as their derived generic variants) whose values are generated lazily as a caller iterates over the returned collection. Read more about the purpose in the remarks section. The following example has a...
public IEnumerable<User> SelectUsers() { // Execute an SQL query on a database. using (IDataReader reader = this.Database.ExecuteReader(CommandType.Text, "SELECT Id, Name FROM Users")) { while (reader.Read()) { int id = reader.GetInt32(0...
You can extend the functionality of existing yield methods by passing in one or more values or elements that could define a terminating condition within the function by calling a yield break to stop the inner loop from executing. public static IEnumerable<int> CountUntilAny(int start, HashSet...
using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; namespace Demo { [Export(typeof(IUserProvider))] public sealed class UserProvider : IUserProvider { public ReadOnlyCollection<User> GetAllUsers() ...
using System; using System.ComponentModel.Composition; namespace Demo { public sealed class UserWriter { [Import(typeof(IUserProvider))] private IUserProvider userProvider; public void PrintAllUsers() { foreach (User user in this.user...
See the other (Basic) examples above. using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace Demo { public static class Program { public static void Main() { using (var catalog = new ApplicationCatalog()) ...
Just like other methods, extension methods can use generics. For example: static class Extensions { public static bool HasMoreThanThreeElements<T>(this IEnumerable<T> enumerable) { return enumerable.Take(4).Count() > 3; } } and calling it would be like: ...

Page 11 of 1336