This blog is moved to
http://amalhashim.wordpress.com

Saturday, February 28, 2009

C# 4.0 - Optional Parameters

Days of creating multiple overloads to avoid parameters are gone. You no more have to create many methods and pass the default one there. C# 4.0 brings to us the concept of “optional” parameters.

How it works, let’s check it. Suppose you have a method like below,

public static class TestClass

{

public static void ParamMethod(string Name, double Salary,

string Department, int Age = 30)

{

Console.WriteLine("Name = {0}", Name);

Console.WriteLine("Age = {0}", Age);

Console.WriteLine("Salary = {0}", Salary);

Console.WriteLine("Department = {0}", Department);

}

}

Here you have mentioned the default value using “=” for the parameter “Age”.

Rule!!! You always have to put optional parameters after the “non-optional” parameters.

C# 4.0 - Named Arguments

For methods having numerous arguments we tend to get confused. And this is also pain for others while reviewing the code. C# 4.0 gives us an opportunity to pass parameters with name.

How it works, let’s check it. Suppose you have a method like below,

public static class TestClass

{

public static void ParamMethod(string Name, double Salary,

string Department, int Age = 30)

{

Console.WriteLine("Name = {0}", Name);

Console.WriteLine("Age = {0}", Age);

Console.WriteLine("Salary = {0}", Salary);

Console.WriteLine("Department = {0}", Department);

}

}

Now the calling would look like,

TestClass.ParamMethod(Name:"Amal", Salary:1000.00,

Department:"Software",Age:31);

You can alter the sequence of the parameters too,

TestClass.ParamMethod("Tupur", 2000.00,

Department: "IT", Age: 26);

No compiler overhead is associated with it.

Rule, Non-optional parameters has to be specified either sequentially or by name.

C# 4.0 - New extension method "Zip"

List<string> names = new List<string>

{"amal", "asif", "munna" , "arif", "sabith" };

List<int> ages = new List<int>{ 25, 20, 15, 12, 10 };

var zipped = names.Zip(ages, (name, age) => name + " is " + age + " years old.");

foreach (var item in zipped)

{

Console.WriteLine(item);

}

Thursday, February 19, 2009

Store/Retrieve file from Database

// Store file in SQL Server

FileStream objFileStream = new FileStream("[Path of File]", FileMode.Open);
byte[] Data = new byte[objFileStream.Length];
objFileStream.Read(Data, 0, Convert.ToInt32(objFileStream.Length));

SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["My"].ToString());
objConnection.Open();
SqlCommand objCommand = new SqlCommand("Bytes_Insert");
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.Add(new SqlParameter("@Data", Data));
objCommand.ExecuteNonQuery();
objConnection.Close();
objFileStream.Close();

// Retrieve file from SQL Server

SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["My"].ToString());
objConnection.Open();
SqlCommand objCommand = new SqlCommand("Bytes_ListAll");
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.StoredProcedure;

SqlDataAdapter adpt = new SqlDataAdapter(objCommand);
DataSet ds = new DataSet();
adpt.Fill(ds);

byte[] Data = (byte[]) ds.Tables[0].Rows[0]["Data"];
File.WriteAllBytes("[Path to store File]", Data);

Wednesday, February 18, 2009

Bring window to front

[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);

private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}

Tuesday, February 10, 2009

Debugging .Net Application with SOS

Right Click project properties
Select Debug
Enable "Enable unmanaged code debugging".


Put some breakpoint in your application.
Once the breakpoint got hit. Open Immediate window and enter the following command

load SOS.

!help

will give detailed information about the command you are looking for.


Its an awesome tool... isn't it?