Home
Programming Languages
Networking
Web Technology
Testing
OS
Database
Search Engine Optimization
Interview FAQs
Free eBooks

C#'s Value Types
Type Meaning Bits Range
bool true/false    
byte 8-bit unsigned integer 8 0 - 255
sbyte 8-bit signed integer 8 -128 - 127
char character 16 0 - 65,535
decimal numeric type for financial calculations 128 1E-28 - 7.9E+28
double double-precision floating point 64 5E-324 - 1.7E+308
float single-precision floating point 32 1.5E-45 - 3.4E+38
int integer 32 -2,...,...,... - 2,...,...,...
uint unsigned integer 32 0 - 4,294,967,295
long long integer 64 -9,.......... to 9,..........
ulong unsigned long integer 64 0 - 18,.......
short short integer 16 -32,768 - 32,767
ushort unsigned short integer 16  0 - 65,535
       

C#'s Bitwise Operators

Operator

Result
& Bitwise AND
| Bitwise OR
^ XOR
>> Shift right
<< Shift left
~ One's complement (unary NOT)

In C#, all class objects must be dynamically allocated. Destructor in C# is called just prior to garbage collection while destructor in C++ is called when an object goes out of scope. However, all destructors will be called before a program terminates.

Arrays in C# are implemented as objects and dynamically allocated using the new operator. Ex. int[] sample = new int[10];  Also, Strings in C# are objects.

Operating on Strings: To test two strings for equality, you can use the == operator. Normally, when the == operator is applied to object references, it determines if both references refer to the same object. This differs for objects of type string. When the == is applied to two string references, the contents of the strings are compared for equality. != is the same as == operator. However, the other relational operators, such as < or >= , compare the references.     

String Methods:
Method Description
static string Copy (string str) Returns a copy of str
int CompareTo (string str) Returns <0 if the string is <str; >0 if the string >str, and 0 if the strings are equal.
int IndexOf (string str) Searches the string for the substring by str. Returns the index of the first match, or -1 on failure.
int LastIndexOf (string str) Searches the string for the substring by str. Returns the index of the last match, or -1 or failure.
string ToLower () Returns a lowercase version of the string.
string ToUpper () Returns an uppercase version of the string.
   

C#'s Access Specifiers:
Public: can be accessed by any other code in your program.
Private: can be accessed only by other members of its class.
Protected: applies only when inheritance is involved.
Internal: applies mostly to the use of an assembly.

C#'s ref and out parameters: it's pass by reference. They are not limited to value parameters. They can also be used on reference parameters.
ref: an argument passed by ref must be initialized before the call.
out: an argument passed by out is assumed to have no initial value. Instead, the method must assign the parameter a value before the method's termination.

Method overloading is one of the ways that C# implements polymorphism.

Signature: the name of a method plus it's parameter list. It does not include the return type and params parameter.

Static Method can be called through its class name, without any object of that method being created.
1) A static method does not have a this reference.
2) A static method can directly call only other static methods. It can not directly call an instance method of its class
3) A static method must directly access only static data.
4) A static method can call instance methods and access instance variables of its class, but it must do through an object     of that class.
5) Static constructor is called automatically and before the instance constructor. It must be private.

Operator Overloading:
General form:
public static return-type operator op(param=type operand) { .... }
public static return-type operator op(param-type1 operand, param-type2 operand2) { .... }
The return-type is often the same type as the class for which the operator is being overloaded.
For unary operators, the operand must be of the same type as the class for which the operator is being defined.
For binary operators, at least one of the operands must be of the same type as its class.
Operator parameters must not use the ref or out modifier.

Conversion Operators:
General form:
public static implicit operator target-type (source-type v) { return value; }
public static explicit operator target-type (source-type v) { return value; }
target-type is the target-type that you are converting to, source-type is the type you are converting from, and value is the value of the class after conversion.
Implicit conversion: the conversion is invoked automatically.
Explicit conversion: the conversion is invoked when a cast is used.

Indexer: allows an object to be indexed like an array. The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints. One dimensional indexer has the following general form: 
element-type this [int index] {
    get { .... }
    set { .... }
}
Indexers do not have to support both get and set method. It can be overloaded.

Properties: combine a field with the methods that access it. The key benefit of a property is that its name can be used in expressions and assignments like a normal variable, but in actuality the get and set accessors are automatically invoked. The general form: 
type name {
    get { .... }
    set { .... }
}
Properties do not define storage locations. Thus, a property manages access to a field. It does not, itself, provide that field and it can not be passed as a ref or out parameter to a method. Properties do not have to support both get and set method. It can not be overloaded. And a property should not alter the state of the underlying variable when the get method accessor is called.

Inheritance: base & derived class
The general form: class derived-class-name : base-class-name { // body of class }
C# does not support the inheritance of multiple base classes into a single derived class. (Note: this is different from C++)
There are three kinds of accesses of class members and methods: public, protected, private.
Public class members and methods can be accessed by any other classes.
Protected class members and methods can be accessed by the derived classes.
Private class members and methods will remain private to its class. They are not accessible by any code outside its class, including derived classes.

Constructors and Inheritance: the constructor for the base class constructs the base classportion of the object, and the constructor for the derived class constructs the derived class part.
A derived class can call a constructor defined in its base class using the general form:
derived-constructor(parameter-list) : base(arg-list) {
// body of constructor
}
The constructors are called in order of derivation.
C# is a strongly typed language. A reference variable for one class type can not normally refer to an object of another class type. However, a reference variable of a base class can be assigned a reference to an object of any class derived from that base class. It is important to understand that it is the type of the reference variable - not the type of the object that it refers to - that determines what members can be accessed.
Copy Constructor: When you copy one object to another, C# will copy the reference to the first object to the new object, which means that you now have two references to the same object. To make an actual copy, you can use a copy constructor, which is just a standard constructor that takes an object of the current class as its single parameter. For example:
class CopyConstructorExample {
    static void Main() {
    Customer customer = new Customer("Paul");
    Customer customerCopy = new Customer(customer);
    customer.Name = "Sam";
    System.Console.WriteLine("The new customer's name is {0}", customerCopy.Name);
    }
}

class Customer {
    private string name;
    public Customer(string name) {
        this.name = name;
    }

    public Customer(Customer customer) {
        this.name = customer.name;
    }

    public string Name {
        get {
            return name;
        }
        set {
            name = value;
        }
    }
}

It'll display: The new customer's name is Paul.

Inheritance and Name Hiding:
When a derived class to define a member that has the same name as a member in its base class, the member in the base class is hidden within the derived class. The derived class member must be preceded by the new keyword to prevent the warning message. You can use base.member to access a hidden name. base.member can be either a method or an instance variable.

Virtual Methods and Overriding:
A virtual method is a method that is declared as virtual in a base class and redefined in one or more derived classes. C# determines which version of the method to call based upon the type of the object referred to by the reference (not the type of the reference)--- and this determination is made at runtime.
The process of redefining a virtual method inside a derived class is called method overriding. When overriding a method, the type signature of the override method must be the same as the virtual method that is being overridden. Also, a virtual method can not be specified as static or abstract. Overridden methods allow C# to support runtime polymorphism.

Abstract Classes: A base class that defines only a generalized form that will be shared by all of its derived classes, leaving it to each derived class to fill in the details. An abstract method is automatically virtual, it is an error to use virtual and abstract together.
The general form: abstract type name (parameter-list);
The abstract modifier can not be applied to static methods. Properties can also be abstract.
A class that contains one or more abstract methods must also be declared as abstract by preceding its class declaration with the abstract specifier. When a derived class inherits an abstract class, it must implement all of the abstract methods in the base class. If it does not, then the derived class must also be specified as abstract.

You can use sealed to prevent inheritance, like: sealed class A { .... }, then class B : A { ... } is illegal.

Object Class: one special class defined by C#. It's an implicit base class of all other classes and for all other types (including the value types). In other words, all other types are derived from object. There are some methods defined in the object class:  
Methods Purpose
public virtual bool Equals (object ob) Determines whether the invoking object is the same as the one referred to by ob
public static bool Equals (object ob1, object ob2) Determines whether ob1 is the same as ob2
protected Finalize() Performs shutdown actions prior to garbage collection. In C#, Finalize() is accessed through a destructor.
public virtual int GetHashCode() Returns the hash code associated with the invoking object.
public type GetType() Obtains the type of an object at runtime.
protected object MemberwiseClone() Makes a "shallow copy" of the object. (The members are copied, but objects referred to by members are not.)
public static bool ReferenceEquals (object ob1, object ob2) Determines whether ob1 and ob2 refer to the same object.
public virtual string ToString() Returns a string that describes the object.

Boxing and Unboxing: When an object reference refers to a value type, a process known as boxing occurs. Boxing causes the value of a value type to be stored in an object instane. Thus, a value type is "boxed" inside an object. Boxing occurs automatically. Unboxing is the process of retrieving a value from an object. This action is performed using a cast from the object reference to the desired value type. For example:
using System;
class BoxingDemo {
    public static void Main() {
        int x;
        object obj;

        x= 10;
        obj = x; // box x into an object

        int y = (int)obj; // unbox obj into an int
        Console.WriteLine(y);
    }
}
It'll display the value 10.

Object is a base class for all other types and that boxing and unboxing of the value types take place automatically, it's possible to use object as a generic data type. But reserve object's generic nature for specialized situations.

References:


Google
1
Hosted by www.Geocities.ws