Wednesday, 4 January 2012

Arithmetic

Operators
C# operators and their precedence closely resemble the operators in other languages of the C family.

Similar to C++, classes can overload most operators, defining or redefining the behavior of the operators in contexts where the first argument of that operator is an instance of
that class, but doing so is often discouraged for clarity.
Following are the built-in behaviors of C# operators.

Arithmetic
The following arithmetic operators operate on numeric operands (arguments a and b in the "sample usage" below).
Sampleus- Read Explanation
age
a + b a plus b The binary operator + returns the sum of its arguments.
a - b a minus b The binary operator - returns the difference between its arguments.
a * b a times b The binary operator * returns the multiplicative product of
its arguments.
The binary operator / returns the quotient of its arguments.
a / b a divided by b If both of its operators are integers, it obtains that quotient
using integer division (i.e. it drops any resulting remainder).
The binary operator % operates only on integer arguments.
a % b a mod b It returns the remainder of integer division of those arguments.
(See modular arithmetic.)
a++ a plus plus The unary operator ++ operates only on arguments that have
an l-value. When placed after its argument, it increments
that argument by 1 and returns the value of that argument
before it was incremented.
++a plus plus a The unary operator ++ operates only on arguments that have
an l-value. When placed before its argument, it increments
that argument by 1 and returns the resulting value.
a-- a minus minus The unary operator -- operates only on arguments that have
an l-value. When placed after its argument, it decrements
that argument by 1 and returns the value of that argument
before it was decremented.
The unary operator -- operates only on arguments that have
a --a minus minus an l-value. When placed before its argument, it decrements
that argument by 1 and returns the resulting value.

Operators

  C# operators and their precedence closely resemble the operators in other languages of the C family.
Similar to C++, classes can overload most operators, defining or redefining the behavior of the operators in contexts where the first argument of that operator is an instance of  that class, but doing so is often discouraged for clarity. Following are the built-in behaviors of C# operators.

Reasoning

Much of the naming standards are derived from Microsoft's .NET Framework libraries. These standards have proven to make names readable and understandable "at a glance". By using the correct conventions when naming objects, you ensure that other C# programmers who read your code will easily understand what objects are without having to search your code for their definition.

Scope and extent

The scope and extent of variables is based on their declaration. The scope of parameters and local variables corresponds to the declaring method or statement block, while the scope of fields is associated with the instance or class and is potentially further restricted by the field's access modifiers.
The extent of variables is determined by the run time environment using implicit reference counting and a complex garbage collection algorithm.

Conversion

Values of a given type may or may not be explicitly or implicitly convertible to other types depending on predefined conversion rules, inheritance structure, and explicit cast definitions.

Predefined conversions
Many predefined value types have predefined conversions to other predefined value types. If the type conversion is guaranteed not to lose information, the conversion can be implicit (i.e. an explicit cast is not required).

Inheritance polymorphism
A value can be implicitly converted to any class from which it inherits or interface that it implements. To convert a base class to a class that inherits from it, the conversion must be  explicit in order for the conversion statement to compile. Similarly, to convert an interface instance to a class that implements it, the conversion must be explicit in order for the conversion statement to compile. In either case, the runtime environment throws a conversion exception if the value to convert is not an instance of the target type or any of its derived types.

Custom types

The predefined types can be aggregated and extended into custom types.
Custom value types are declared with the struct or enum keyword. Likewise, custom reference types are declared with the class keyword.

Arrays
Although the number of dimensions is included in array declarations, the size of each dimension is not:
string[] s;
Assignments to an array variable (prior to the variable's usage), however, specify the size of each dimension:
s = new string[5];
As with other variable types, the declaration and the initialization can be combined:
string[] s = new string[5] ;
It is also important to note that like in Java, arrays are passed by reference, and not passed by value. For example, the following code snippet successfully swaps two elements in an integer array:
static void swap (int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

Integral types

Because the type system in C# is unified with other languages that are CLI-compliant, each integral C# type is actually an alias for a corresponding type in the .NET framework. Although the names of the aliases vary between .NET languages, the underlying types in the .NET framework remain the same. Thus, objects created in assemblies written in other languages of the .NET Framework can be bound to C# variables of any type to which the value can be converted, per the conversion rules below. The following illustrates the cross-language compatibility of types by comparing C# code with the equivalent Visual Basic .NET code:
// C#
public void UsingCSharpTypeAlias()
{
int i = 42;
}
public void EquivalentCodeWithoutAlias()
{
System.Int32 i = 42;
}

Visual Basic .NET
Public Sub UsingVisualBasicTypeAlias()
Dim i As Integer = 42
End Sub
Public Sub EquivalentCodeWithoutAlias()
Dim i As System.Int32 = 42
End Sub
Using the language-specific type aliases is often considered more readable than using the fully-qualified .NET Framework type names.
The fact that each C# type corresponds to a type in the unified type system gives each value type a consistent size across platforms and compilers. That consistency is an important distinction from other languages such as C, where, e.g. a long is only guaranteed to be at least as large as an int, and is implemented with different sizes by different compilers.  As reference types, variables of types derived from object (i.e. any class) are exempt from the consistent size requirement. That is, the size of reference types like System.IntPtr, as opposed to value types like System. Int, may vary by platform. Fortunately, there is rarely a need to know the actual size of a reference type.
There are two predefined reference types: object, an alias for the System.Object class, from which all other reference types derive; and string, an alias for the System.String class. C# likewise has several integral value types, each an alias to a corresponding value type in the System namespace of the .NET Framework. The predefined C# type aliases expose the methods of the underlying .NET Framework types. For example, since the .NET Framework's System.Int32 type implements a ToString() method to convert the value of an integer to its string representation,
C#'s int type exposes that method:
int i = 97;
string s = i.ToString();
// The value of s is now the string "97".
Likewise, the System.Int32 type implements the Parse() method, which can therefore be accessed via C#'s int type:
string s = "97";
int i = int.Parse(s);
// The value of i is now the integer 97.
The unified type system is enhanced by the ability to convert value types to reference types (boxing) and likewise to convert certain reference types to their corresponding value types (unboxing). This is also known as casting.
object boxedInteger = 97;
int unboxedInteger = (int)boxedInteger;
Boxing and casting are, however, not type-safe: the compiler won't generate an error if the programmer mixes up the types. In the following short example the mistake is quite obvious, but in complex programs it may be real hard to spot. Avoid boxing, if possible.
object getInteger = "97";
int anInteger = (int)getInteger; // no compile-time error, the program will crash, however The built-in C# type aliases and their equivalent .NET Framework types follow:

Integers
C# Alias         .NET Type            Size (bits)         Range
  sbyte           System.SByte              8             -128 to 127
  byte             System.Byte                8               0 to 255
  short            System.Int16              16            -32,768 to 32,767
  ushort          System.UInt16           16             0 to 65,535
  char             System.Char              16             A unicode character of code 0 to 65,535
   int               System.Int32              32            -2,147,483,648 to 2,147,483,647
   uint             System.UInt32           32              0 to 4,294,967,295
   long            System.Int64              64           -9,223,372,036,854,775,808 to                                                                         9,223,372,036,854,775,807                                  
   ulong           System.UInt64          64             0 to 18,446,744,073,709,551,615

Floating-point
     C# Alias    .NET Type          Size (bits)   Precision                            Range
      float          System.Single         32           7 digits                       1.5 x 10-45 to 3.4 x 1038
     double       System.Double       64          15-16 digits                 5.0 x 10-324 to 1.7 x 10308
     decimal      System.Decimal     128       28-29 decimal places    1.0 x 10-28 to 7.9 x 1028

Other predefined types
 C# Alias   .NET Type          Size (bits)                               Range
  bool       System.Boolean     32                true or false, which aren't related to any integer in C#.
  object    System.Object       32/64            Platform dependant (a pointer to an object).
   string     System.String         16 * length   A unicode string with no special upper bound.