The Var Keyword- Implicitly Typed Local Variables

The var keyword has been introduced to C# 3.0. When the compiler encounters a var keyword, it implicitly initialize the variable to the type of the initialization expression. For example, if the compiler sees var s = "dummy string" it converts it to string s = "dummy string".

Therefore, an implicitly typed variable must be initialized. For example,
var s
is not a legal declaration.

The var keyword can not be used as a function parameter

Advertisements

Object Initialization Expressions

The object initialization expressions is another new C#3.0 feature. what is really cool about object initialization expressions is that they allow you to intialize new objects without calling the object constructor or set the properties. Below is an example of object initialization expressions.


public class Employee
{
    public int ID;
    public int Age;
    public int Salary;
    public string Name;
}

List employees = new List()
{
    new Employee(){ID=1,Age=27,Name = "John", Salary=3000},
    new Employee(){ID=1,Age=37,Name = "Sara", Salary=5000},
    new Employee(){ID=1,Age=31,Name = "Tommy", Salary=6000},
};

Ten Must-Have Tools Every Developer Should Download Now

http://msdn.microsoft.com/en-us/magazine/cc300497.aspx

C# Extension Methods

Extension Methods is new to C# 3.0 and allows you to attach new methods to .Net data types. I have created a simple example. I have added an extension method to the .Net Int32 type to calculate the square value of an integer.

public static class Program
{
static void Main(string[] args)
{
   Int32 num = 2;
   Console.WriteLine(num.Square());
}
public static Int32 Square(this Int32 n)
{
   return n * n;
}
}

I have added the Square function to the Int32 type by adding the this keyword  before the first parameter in the function which informs the compiler that this method is an extension method of the parameter type.

Note: Both the extension method and its class must be static

In case you have an extension method and an instance method with the same name and signature, the instance method will take precedence over the extension method.

Note: Events, Properties, and operators can not be extended.

Creating FTP site on IIS6 programitacally using C#

Inorder to create FTP site on IIS6

        public void CreateFTPSite(string serverName, string siteName, string siteID)

        {

            try

            {   

                //Create website on IIS

                DirectoryEntry root = new DirectoryEntry(“IIS://” + serverName + “/msftpsvc”);

                DirectoryEntry newSite = root.Children.Add(siteID, “IIsFTPServer”);

                newSite.Properties[“ServerComment”][0] = “codeleacher.com”;

                newSite.CommitChanges();

                root.CommitChanges();

 

                SetSingleProperty(“IIS://” + serverName + “/msftpsvc/”+siteID.ToString(), “ServerBindings”,    “:2122:”);

 

        private static void SetSingleProperty(string metabasePath, string propertyName, object newValue)

        {

            try

            {

                DirectoryEntry path = new DirectoryEntry(metabasePath);

                PropertyValueCollection propValues = path.Properties[propertyName];

                if (null == propValues.Value)

                    propValues.Value = “foo”;

 

                string oldType = propValues.Value.GetType().ToString();

                string newType = newValue.GetType().ToString();

                if (newType == oldType)

                {

                    path.Properties[propertyName][0] = newValue;

                    path.CommitChanges();

                }

                else

                    Console.WriteLine(” Failed in SetSingleProperty; type of new value does not match property”);

            }

            catch (Exception ex)

            {

                if (“HRESULT 0x80005006” == ex.Message)

                    Console.WriteLine(” Property {0} does not exist at {1}”, propertyName, metabasePath);

                else

                    Console.WriteLine(“Failed in SetSingleProperty with the following exception: \n{0}”, ex.Message);

            }

        }

            }

            catch (Exception ex)

            {

                throw new Exception(“Error”);

            }

        }

Add users to the IIS_WPG group using C#.Net

The following function adds a user to the IIS_WPG group using the ntrights.exe utility by Microsoft. Note that in order for this function to work you have to have the ntrights utility downloaded. To download it check Microsoft’s wensite:

http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

public static void AddUserToWPG(string currentFolderPath, string user)
{
  try
  {
    Process process = new Process();
    //run the script
    process.StartInfo.FileName = System.Environment.SystemDirectory + @"\net.exe";
    process.StartInfo.Arguments = string.Format(" localgroup \"{0}\" {1} /add",     wpgGroup, user);
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = true;
    process.Start();
    process.WaitForExit();

    process.StartInfo.FileName = Path.Combine(currentFolderPath, "ntrights.exe");
    process.StartInfo.Arguments = string.Format(" +r \"seTcbPrivilege\" -u \"{0}\"",     user);
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = true;
    process.Start();
    process.WaitForExit();
  }
  catch
  {
     throw new Exception("your message");

  }

}

Change the ASP.Net version of a website on IIS6 using C#.Net

Sometimes, while creating a website pragmatically, you wish to change the ASP.Net version the site is using. The following function change the ASP.Net version a website is using to ASP.Net 2.0

public static void UpdateASPNetVersion(string siteidentifier, string virtualDirectoryName)
{
   //get virtual directory and site name for m context parameters
   string targetSite = "W3SVC/" + siteidentifier + "/ROOT";
   string targetVDir = virtualDirectoryName;
   Process process = new Process();
   //run the script
   process.StartInfo.FileName =         Path.GetDirectoryName(System.Environment.SystemDirectory) + @"\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe";
   process.StartInfo.Arguments = string.Format(" -s {0}/{1}", targetSite, targetVDir);
   process.StartInfo.CreateNoWindow = false;
   process.Start();
   process.WaitForExit();
}

You can of course change the ASP.Net version to any other version by choosing a different directory.