User:Nvineeth/TODO

Value Types

 * Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.


 * All value types are derived implicitly from the System..::.ValueType.


 * Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.


 * Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null.
 * All of the simple types -- those integral to the C# language -- are aliases of the .NET Framework System types. For example, int is an alias of System..::.Int32.

Reference Type

 * string is a reference type
 * Variables of reference types, referred to as objects, store references to the actual data. This section introduces the following keywords used to declare reference types: class, interface, delegate. This section also introduces the following built-in reference types: object, string

Arrays
Arrays are reference types: They can be, Single Dimensional, Multi Dimensional, Jagged. Implicitly typed arrays were introduced later.

Jagged Arrays
Jagged array example: (array of multidimensional array)

Example of Implicitly typed arrays with anonymous types:

Strings

 * Basics
 * A string is an object of type String whose value is text. Internally, the text is stored as a readonly collection of Char objects, each of which represents one Unicode character encoded in UTF-16. There is no null-terminating character at the end of a C# string (unlike C and C++); therefore a C# string can contain any number of embedded null characters ('\0'). The length of a string represents the number of characters regardless of whether the characters are formed from Unicode surrogate pairs or not.


 * In C#, the string keyword is an alias for String. Therefore, String and string are equivalent, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. In addition, the C# language overloads some operators to simplify common string operations.


 * String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. Ex:  results in the compiler error --


 * There can be Regular and Verbatim String Literals

The StringBuilder class creates a string buffer that offers better performance if your program performs many string manipulations. The StringBuilder string also enables you to reassign individual characters, something the built-in string data type does not support.

See also: LINQ and Strings

Anonymous Functions
As described elsewhere, a delegate is a type that wraps a method call. A delegate instance can be passed between methods just like a type, and it can be invoked like a method. An anonymous function is an "inline" statement or expression that can be used wherever a delegate type is expected. You can use it to initialize a named delegate or pass it instead of a named delegate type as a method parameter.

There are two kinds of anonymous functions, which are discussed individually in the following topics:
 * Lambda Expressions (C# Programming Guide).
 * Anonymous Methods (C# Programming Guide)

See:The Evolution of Delegates in C#

Structs

 * A struct type is a value type
 * Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.


 * Structs share almost all the same syntax as classes, although structs are more limited than classes:
 * Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
 * A struct may not declare a default constructor —a constructor with no parameters — or a destructor. (since they are value types?)


 * Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
 * when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed. The output of the following example shows that only the value of the class field is changed when the class instance is passed to the ClassTaker method. The struct field, however, does not change by passing its instance to the StructTaker method. This is because a copy of the struct is passed to the StructTaker method, while a reference to the class is passed to the ClassTaker method.

Enums

 * Value types
 * Two types, simple and flags. To make the enum values as flags, use the [Flags] attribute.

readonly and const
The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants

Operators

 * In C#, there are two different kinds of equality: reference equality and value equality.
 * To check for reference equality, use . To check for value equality, use.
 * By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality. When a type is immutable, meaning the data contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. Overriding operator == in non-immutable types is not recommended.
 * Overloaded operator == implementations should not throw exceptions. Any type that overloads operator == should also overload operator !=. For example:


 * The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. The reverse is also true, and similar for, and for <= and >=.


 * Operator [] cannot be overloaded, uses indexers instead.

Interface

 * Interfaces can contain events, indexers, methods and properties.


 * Explicit Interface Implementation:If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period.


 * Example of Indexers in an interface:

Polymorphism -- new and override

 * When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.


 * Replacing a member of a base class with a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property on a derived class. The new keyword is placed before the return type of a class member that is being replaced. When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class.


 * In order for an instance of a derived class to completely take over a class member from a base class, the base class has to declare that member as virtual. This is accomplished by adding the virtual keyword before the return type of the member. A derived class then has the option of using the override keyword, instead of new, to replace the base class implementation with its own.


 * fields cannot be used with virtual / override qualifier, but can be used with new keyword. Fields cannot be virtual; only methods, properties, events and indexers can be virtual. When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class.


 * A derived class can stop virtual inheritance by declaring an override as sealed. This requires putting the sealed keyword before the override keyword in the class member declaration. Sealed methods can be replaced by derived classes using the new keyword.


 * Note : to use the  keyword, the function must be declared as  . Absence of  ,   keyword, defaults to   and compiler throws a warning.


 * By default, C# methods are not virtual — if a method is declared as virtual, any class inheriting the method can implement its own version. To make a method virtual, the virtual modifier is used in the method declaration of the base class. The derived class can then override the base virtual method by using the override keyword or hide the virtual method in the base class by using the new keyword. If neither the override keyword nor the new keyword is specified, the compiler will issue a warning and the method in the derived class will hide the method in the base class.

Events and Delegates

 * Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class). If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event.


 * If the delegate uses reference parameters, the reference is passed sequentially to each of the three methods in turn, and any changes by one method are visible to the next method. When any of the methods throws an exception that is not caught within the method, that exception is passed to the caller of the delegate and no subsequent methods in the invocation list are called. If the delegate has a return value and/or out parameters, it returns the return value and parameters of the last method invoked.


 * Covariance and contravariance provide a degree of flexibility when matching method signatures with delegate types. Covariance permits a method to have a more derived return type than what is defined in the delegate. Contravariance permits a method with parameter types that are less derived than in the delegate type.


 * TODO - add and remove event accessors. If two or more interfaces supply the events with same name, then we need to explicitly handle this with add and remove event accessors.


 * One use for accessor-declarations is to expose a large number of events without allocating a field for each event, but instead using a Dictionary to store the event instances. This is only useful if you have a very large number of events, but you expect most of the events will not be implemented.

Nullable Types

 * Nullable types are instances of the System..::.Nullable<(Of <(T>)>) struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.
 * The ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.


 * Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)


 * The syntax T? is shorthand for Nullable<(Of <(T>)>), where T is a value type. The two forms are interchangeable.


 * Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; or double? d = 4.108. However, a nullable type can also be assigned the value null: int? x = null.


 * Use the Nullable<(Of <(T>)>)..::.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault;


 * Use the HasValue and Value read-only properties to test for null and retrieve the value, for example if(x.HasValue) j = x.Value;


 * Identifying a nullable type:

Generic Programming

 * Constraints on Type Parameters are possible.
 * Inheriting from Generic classes.
 * In Inheritance, there can be two types, Open type constructed, and Closed type constructed. In closed type constructed, the template parameter type is speficied.
 * Explain: Generic interfaces can inherit from non-generic interfaces if the generic interface is contra-variant, which means it only uses its type parameter as a return value. In the .NET Framework class library, IEnumerable inherits from IEnumerable because IEnumerable only uses T in the return value of GetEnumerator and in the Current property getter.
 * Difference with C++ template: C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints. For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter, which will produce an error at the time of instantiation of the template with a type that does not support these operators. C# disallows this; the only language constructs allowed are those that can be deduced from the constraints.

keyword
In generic classes and methods, one issue that arises is how to assign a default value to a parameterized type T when you do not know the following in advance:

* Whether T will be a reference type or a value type. * If T is a value type, whether it will be a numeric value or a struct.

Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.

Iterators

 * Overview--Pro C# with .NET 3.0


 * Iterators are a new feature in C# 2.0. An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or struct without having to implement the entire IEnumerable interface. Instead, you provide just an iterator, which simply traverses the data structures in your class. When the compiler detects your iterator, it will automatically generate the Current, MoveNext and Dispose methods of the IEnumerable or IEnumerable interface.


 * An iterator is a section of code that returns an ordered sequence of values of the same type.
 * An iterator can be used as the body of a method, an operator, or a get accessor.
 * The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration. For more information, see yield.
 * Multiple iterators can be implemented on a class. Each iterator must have a unique name just like any class member, and can be invoked by client code in a foreach statement as follows: foreach(int x in SampleClass.Iterator2){}
 * The return type of an iterator must be IEnumerable, IEnumerator, IEnumerable, or IEnumerator.
 * It is also possible to use named iterators to support different ways of iterating through the same collection of data. For example, you could provide one iterator which returns elements in ascending order, and one which returns elements in descending order. An iterator can also have parameters to enable clients to control all or part of the iteration behavior.

Lambda

 * Overview:Pro C# 2008 Troelsen


 * All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type.
 * lambdas are a concise way to express anonymous methods.
 * They can refer outer variables. Also if no args are required, then lambda expression can be written as,
 * Outer variables are captured, that is their lifetime is extended.

LINQ

 * A query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language. Different languages have been developed over time for the various types of data sources, for example SQL for relational databases and XQuery for XML. Therefore, developers have had to learn a new query language for each type of data source or data format that they must support. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.


 * All LINQ query operations consist of three distinct actions:
 * Obtain the data source.
 * Create the query.
 * Execute the query.


 * Types that support IEnumerable<(Of <(T>)>) or a derived interface such as the generic IQueryable<(Of <(T>)>) are called queryable types.


 * The query expression contains three clauses: from, where and select. (If you are familiar with SQL, you will have noticed that the ordering of the clauses is reversed from the order in SQL.) The from clause specifies the data source, the where clause applies the filter, and the select clause specifies the type of the returned elements.


 * The query execution can be of two types: Deferred Execution, Immediate Execution. Queries that perform aggregation functions over a range of source elements must first iterate over those elements. Examples of such queries are Count, Max, Average, and First. These execute without an explicit foreach statement because the query itself must use foreach in order to return a result. You can also force execution by putting the foreach loop immediately after the query expression. However, by calling ToList or ToArray you also cache all the data in a single collection object.


 * Data Transformations with LINQ.


 * The LINQ queries get internally translated to method calls, and these methods are extension methods on the IEnumerable interface.

C# 3.0 Features

 * Overview : C# 3.0 Features That Support LINQ

Unsafe

 * See : C# 3.0 in a nutshell


 * a pointer should point to a  variable, since the Garbage collector may move the object to different location as a part of optimization and de-fragmentation.

Application domains

 * You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.

Assembly and GAC

 * Overview : Pro C# 2008 and the .NET 3.5 Platform
 * Diagram: Pro C# 2008 and the .NET 3.5 Platform


 * An assembly is a fundamental building block of any .NET Framework application. For example, when you build a simple C# application, Visual Studio creates an assembly in the form of a single portable executable (PE) file, specifically an EXE or DLL.


 * Assemblies contain metadata that describe their own internal version number and details of all the data and object types they contain. For more information see Assembly Manifest.


 * An assembly manifest contains all the metadata needed to specify the assembly's version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information.


 * An assembly can be single filed, or multi-filed.


 * .NET assembly can be of two types, private and shared.


 * In order to share an assembly with other applications, it must be placed in the Global Assembly Cache (GAC).


 * Strong Name of an assembly is used to uniquely identify a shared assembly and to over come the drawbacks of COM which used GUID.


 * Overview on Strong Name, digital signature : C# 3.5 by Troelsen

Attributes

 * Attributes add metadata to your program. Metadata is information embedded in your program such as compiler instructions or descriptions of data.
 * Your program can examine its own metadata using Reflection. See Accessing Attributes With Reflection.
 * Attributes are commonly used when interacting with COM.
 * Example usages: Create Unions, Common usages : Conditional, Obsolete warnings, Assembly info like title, author, company and AttributeUsage.

Expression Trees

 * Expression trees represent language-level code in the form of data. The data is stored in a tree-shaped structure. Each node in the expression tree represents an expression, for example a method call or a binary operation such as x < y.
 * Overview : Acclerated C# 2008.

Extension Methods
Using extension methods, we can add functionality to pre-compiled types while providing the illusion these methods were there all along.


 * When defining extension methods,
 * First restriction, is that they must be defined within a static class.
 * The second point is that all extension methods are marked as such by using the  keyword as a modified on the first (and only the first) parameter of the method in question.
 * The extension method can be called either from the correct instance in memory or statically via the defining static class.