Tutorial by Examples: const

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...
A static constructor is called the first time any member of a type is initialized, a static class member is called or a static method. The static constructor is thread safe. A static constructor is commonly used to: Initialize static state, that is state which is shared across different instan...
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...
const is used to represent values that will never change throughout the lifetime of the program. Its value is constant from compile-time, as opposed to the readonly keyword, whose value is constant from run-time. For example, since the speed of light will never change, we can store it in a constan...
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...
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...
public class SingletonClass { public static SingletonClass Instance { get; } = new SingletonClass(); private SingletonClass() { // Put custom constructor code here } } Because the constructor is private, no new instances of SingletonClass can be made by consum...
class Example { public string Foobar { get; set; } public List<string> Names { get; set; } public Example() { Foobar = "xyz"; Names = new List<string>(){"carrot","fox","ball"}; } }
An enum cannot have a public constructor; however, private constructors are acceptable (constructors for enums are package-private by default): public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); // usual names for US coins // note that the above parentheses and the constructor...
The fundamental part of most classes is its constructor, which sets up each instance's initial state and handles any parameters that were passed when calling new. It's defined in a class block as though you're defining a method named constructor, though it's actually handled as a special case. cla...
ConstantsDescriptionApproximateMath.EBase of natural logarithm e2.718Math.LN10Natural logarithm of 102.302Math.LN2Natural logarithm of 20.693Math.LOG10EBase 10 logarithm of e0.434Math.LOG2EBase 2 logarithm of e1.442Math.PIPi: the ratio of circle circumference to diameter (π)3.14Math.SQRT1_2Square ro...
The dict() constructor can be used to create dictionaries from keyword arguments, or from a single iterable of key-value pairs, or from a single dictionary and keyword arguments. dict(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3} dict([('d', 4), ('e', 5), ('f', 6)]) # {'d': 4, 'e': ...
Arrays can be created by enclosing a list of elements in square brackets ([ and ]). Array elements in this notation are separated with commas: array = [1, 2, 3, 4] Arrays can contain any kind of objects in any combination with no restrictions on type: array = [1, 'b', nil, [3, 4]]
A class can have only one constructor, that is a method called initialize. The method is automatically invoked when a new instance of the class is created. class Customer def initialize(name) @name = name.capitalize end end sarah = Customer.new('sarah') sarah.name #=> 'Sarah' ...
Signed integers can be of these types (the int after short, or long is optional): signed char c = 127; /* required to be 1 byte, see remarks for further information. */ signed short int si = 32767; /* required to be at least 16 bits. */ signed int i = 32767; /* required to be at least 16 bits */ ...
The C language has three mandatory real floating point types, float, double, and long double. float f = 0.314f; /* suffix f or F denotes type float */ double d = 0.314; /* no suffix denotes double */ long double ld = 0.314l; /* suffix l or L denotes long double */ /* the differen...
You can filter what routes are available using constraints. There are several ways to use constraints including: segment constraints, request based constraints advanced constraints For example, a requested based constraint to only allow a specific IP address to access a route: constraints(...
When you make a subclass of a base class, you can construct the base class by using : base after the subclass constructor's parameters. class Instrument { string type; bool clean; public Instrument (string type, bool clean) { this.type = type; this.clean = c...

Page 1 of 13