Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, 23 August 2012

Preprocessor Directives Uses

//Code Implement under the "MyFramWork" preprocessor directives

#if MyFramWork      
              string completionStack;
#endif



 //preprocessor directives Use with Method and Property   
 
 [Conditional("DEBUG")]
 public static void Check(Delegate callback)
 {

 }


 

Attribute Directive

 

 
 

Monday, 13 August 2012

Generic : Same property but different (compatible) types.


public abstract class Base
{
    public abstract void Use();
    public abstract object GetProp();
}
public abstract class GenericBase<T> : Base
{
    public T Prop { get; set; }

    public override object GetProp()
    {
        return Prop;
    }
}
public class StrBase : GenericBase<string>
{
    public override void Use()
    {
        Console.WriteLine("Using string: {0}", Prop);
    }
}
public class IntBase : GenericBase<int>
{
    public override void Use()
    {
        Console.WriteLine("Using int: {0}", Prop);
    }
}

Thursday, 9 August 2012

Generic

Defination:

Generics allow you to define type-safe data structures, without committing to actual data types, generics are similar to C++ templates

Finally its class template

Thursday, 2 August 2012

C# Constructor Calling

A constructor can invoke/call from base class using the base keyword.

public Manager(int initialdata) : base() //Base contructor without parameter
{
    //Add further instructions here.
}
public Manager(int annualSalary):base(annualSalary) //Base contructor with parameter
    {
        //Add further instructions here.
    }


A constructor can invoke/call another constructor in the same object using the this keyword.



//First Contructor
public Employee(int annualSalary)
{
    salary = annualSalary;
}

//Second Contructor
public Employee(int weeklySalary, int numberOfWeeks)
: this(weeklySalary * umberOfWeeks) //Call First Contruct after call this method.
{
//Add further instructions here.
}

Thursday, 23 February 2012

Lambda Action / Delegate

Common Delegate Call
 b.Click += delegate(object sender, EventArgs e) 
                    { 
                      Log("Sender :" + sender + "EventArgs " + e);  
                    };
Same as
Using Lambda 
1. Call Action with Parameter
  (o, e) => {
             // Statements  
          }
2. Call Action Without Parameter
  () => { // Statements }
3. Implement Action
Public void ImplementAction(Action a){ a(); }
4. Create Object
string className = "MyClass";
 var obj = (T) Activator.CreateInstance(Type.GetType(className));