Tuesday 29 January 2013

Learn More About Static

For Asp.net Training with C# Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com

More about Static
Let’s have some more detail about static:-
1.       We cannot use this statement with static.
For example:-
See the following example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    public int x;
    public static int y;

    public void sum(int x, int y)
    {

        this.x = x;
   
    }
}
In this example we create two variable x and y in which y is static variable. We can use non static variable with this but we cannot use static with this.



2.       We cannot call non static method inside non static method
For example:-
See the following example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    public int x;
    public static int y;

   
    public void sum(int x, int y)
    {

        this.x = x;
      
        div(6, 7);
   
    }

    public static int div(int x, int y)
    {
       
        return x / y;
   
    }

   
}

As you can see that there are two methods in this class. One is non-static method Sum and static method div. we can call static method inside the non-static method but we cannot call non static method inside static method.

2 comments: