MyDevBank

MyDevBank

summaries and tech views for new developers C#,asp.net for web\mobile development,databases,iis6 and more.
Creating this site was an idea that popped up after some programing years, when you have to get back to the same things you all ready forgot, while new stuff is arriving to town. In this site you can find summaries on tech stuff im into, so for a stable wide base knowledge pyramid ,enjoy!!

C# Keywords

Interface

Interface defines a contract. a class that implement an interface must implement its members.
Interface members can be methods,properties and events.
an Interface can inherit from other base interfaces
interface IMyInterface: IBase1, IBase2
{
  void MethodA();
  void MethodB();
}

a class can implement interfaces.
and can inherit a class and implement interfaces together like taht:
class MyClass: BaseClass, IMyInterface1 , IotherInterface
{}

Delegate

A delegate in C# is similar to a function pointer in C or C++.
Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.
The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

the delegate declaration with the same method signature as the referenced method
public delegate string MyStringDele(string str);

creating an instance of the delegate. using the method that the delegate is pointing to and sending a parameter
protected void Page_Load(object sender, EventArgs e)
{
  MyStringDele StringDele = new MyStringDele(MyClass1.StringFunc);
  xval.Text = StringDele("some text");
}

the method from another class in this case that the delegate is referencing
public static string StringFunc(string str)
{
  return str + " added by StringFunc";
}

using

The using statement defines a scope at the end of which an object will be disposed.
You create an instance in a using statement to ensure that Dispose is called on the object when the using statement is exited.

code:

using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlCommand cm = new SqlCommand(commandString, cn))
    {
cn.Open();
cm.ExecuteNonQuery();
    }
}

lock

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. useful when using a limited resource preventing parallel users to over consume or trying to change the same thing in the same time.

code:

lock (this) <- the this stand for this class which returns the amount
{
    if (balance >= amount)
    {
        Console.WriteLine("Balance before Withdrawal : " + balance);
        Console.WriteLine("Amount to Withdraw : -" + amount);
        balance = balance - amount;
        Console.WriteLine("Balance after Withdrawal : " + balance);
        return amount;
    }
    else
    {
        return 0; // transaction rejected
    }
}

ref & out

When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable.
out parameter is similar to ref, but there are few implementation differences when you use it in C#.

code:

public RefOut()
{
  // "b" is not necessary to be initialized while c does
  int a = 0,b,c = 1,d = 0;
  d = ParamTest(a,out b,ref c);
}
public static int ParamTest(int a, out int b, ref int c)
{
  a=10;
  b=20;
  c=30;
  return 40;
}
result : a = 0 b = 20 c = 30 d = 40

Comment | Related Tags

HOME| IIS6 pipeline| ASP.NET pipeline| Http Modules| Http Handlers| Caching| Web Services| Page Life Cycle| Ajax| Jquery| Session State
ViewState| SEO| Mobile Basics| Mobile Handsets| C# Modifiers| C# Keywords| N-Tier| Design patterns| Stack & Heap| WCF
Databases| Relational Model| functionalities| Stored procedures| fishmarket
MyDevBank
© 2010 All Rights Reserved