Tuesday 21 June 2011

as keyword in c sharp


as 

The as operator is used to perform certain types of conversions between compatible reference types . The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception

Example


class ClassA { }
class ClassB { }
 
class MainClass
{
    static void Main()
    {
        object[] objArray = new object[6];
        objArray[0] = new ClassA();
        objArray[1] = new ClassB();
        objArray[2] = "hello";
        objArray[3] = 123;
        objArray[4] = 123.4;
        objArray[5] = null;
 
        for (int i = 0; i < objArray.Length; ++i)
        {
            string s = objArray[i] as string;
            Console.Write("{0}:", i);
            if (s != null)
            {
                Console.WriteLine("'" + s + "'");
            }
            else
            {
                Console.WriteLine("not a string");
            }
        }
    }
}
/*
Output:
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
*/
 
 

ref keyword in c sharp


ref C#

The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling methodTo use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword.An argument passed to a ref parameter must first be initialized

 For example:

class
 RefExample

{

    static void Method(ref int i)
    {
        i = 44;
    }
    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

Monday 20 June 2011

what is static keyword in c#


static (C# Reference)

The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes. The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. 
The following class is declared as static and contains only static methods:
static class CompanyEmployee
{
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/  }
}
A constant or type declaration is implicitly a static member.
A static member cannot be referenced through an instance. Instead, it is referenced through the type name. For example, consider the following class
While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
It is not possible to use this to reference static methods or property accessors.
If the static keyword is applied to a class, all the members of the class must be static.
Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.
  public class Employee
{
    public string id;
    public string name;
 
    public Employee4()
    {
    }
 
    public Employee (string name, string id)
    {
        this.name = name;
        this.id = id;
    }
 
    public static int employeeCounter;
 
    public static int AddEmployee()
    {
        return ++employeeCounter;
    }
}
 
class MainClass : Employee
{
    static void Main()
    {
        Console.Write("Enter the employee's name: ");
        string name = Console.ReadLine();
        Console.Write("Enter the employee's ID: ");
        string id = Console.ReadLine();
 
        // Create and configure the employee object:
        Employee e = new Employee (name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee.employeeCounter = Int32.Parse(n);
        Employee.AddEmployee();
 
        // Display the new information:
        Console.WriteLine("Name: {0}", e.name);
        Console.WriteLine("ID:   {0}", e.id);
        Console.WriteLine("New Number of Employees: {0}",
                      Employee.employeeCounter);
    }
}
    /*
    Input:
    sunil
    SU643G
    10
     * 
    Sample Output:
    Enter the employee's name: Matthias Berndt
    Enter the employee's ID: SU643G
    Enter the current number of employees: 10
    Name: sunil
    ID:   SU643G
    New Number of Employees: 11
    */






Tuesday 14 June 2011

How to Set the Default Value of a DropDownList in an ASP.NET


DropDownList Control Examples:
You can see the live samples and examples of DropDownList Control from the following links:
  • Setting DropDownList Default Value
  • DropDownList Control SelectedValue Property
  • DropDownList Control SelectedIndex Property
  • DropDownList Control JavaScript Validation
  • Populate DropDownList Items Dynamically
The second type of situation occurs when you are updating the old data then you have to display the default value of DropDownList control as previously saved value in the database. You can use the following code to set the list item as default selected value for dropdownlist control:



Example 1:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("Confections"));
Example 2:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("3"));
Example 3:
DropDownList1.Items.FindByValue("4").Selected = true;



<asp:DropDownList ID="ddlLanguage"runat="server"Style="width: 200px">
asp:DropDownList>

ddlLanguage.SelectedIndex = ddlLanguage.Items.IndexOf(ddlLanguage.Items.FindByText("English"));



Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger