locked
static in c RRS feed

  • Question

  • User901332479 posted

    i have used the following code but i cant get the concept of static 

    void func()
    {
    int x = 0;
    x++;
    Console.WriteLine(x);
    }
    static void Main(string[] args)
    {
    Program x = new Program();
    x.func();
    x.func();
    Console.ReadKey();
    }

    where i get both the time answer in one i cannot intialize outside the function and i cannot use it without initalization but it should be local please help me

    Friday, October 18, 2013 8:21 AM

Answers

  • User-417640953 posted

    Hello,

    Thanks for the post.

    C# doesn't support static locals at all.  Anything static needs to be a member of a type or a type itself. And VB.Net does support for static locals,

    however it's accomplished by re-writing your code at compile time to move the variable to the type level.

    For your code, I'd like to do like below.

    int func(int param)
            {
                int x = param;
                x++;
                Console.WriteLine(x);
                return x;
            }
            static void Main(string[] args)
            {
                
                Program x = new Program(); 
                x.func(x.func(0)); 
                //more.... 
                //x.func(x.func(x.func(0)));
                 
                Console.ReadKey();
    
            }

    Hope this helps, thanks.


    Best Regards!  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2013 3:36 AM

All replies

  • User464632190 posted

    Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this. Static methods can be public or private.
    Static
    Example

    This program defines both static methods and regular instance methods. The static methods use the static keyword somewhere in the method declaration signature, usually as the first keyword or the second keyword after public.
    Method call

    Static methods cannot access non-static class level members and do not have a 'this' pointer. Instance methods can access those members, but must be called through an instantiated object. This adds another level of indirection.

    using System;

    class Program
    {
    static void MethodA()
    {
    Console.WriteLine("Static method");
    }

    void MethodB()
    {
    Console.WriteLine("Instance method");
    }

    static char MethodC()
    {
    Console.WriteLine("Static method");
    return 'C';
    }

    char MethodD()
    {
    Console.WriteLine("Instance method");
    return 'D';
    }

    static void Main()
    {
    //
    // Call the two static methods on the Program type.
    //
    Program.MethodA();
    Console.WriteLine(Program.MethodC());
    //
    // Create a new Program instance and call the two instance methods.
    //
    Program programInstance = new Program();
    programInstance.MethodB();
    Console.WriteLine(programInstance.MethodD());
    }
    }

    Output

    Static method
    Static method
    C
    Instance method
    Instance method
    D

    Friday, October 18, 2013 8:26 AM
  • User-231977777 posted

    Put this line outside the func() 

    static int x = 0 ; 

    and remove "int x = 0 ; " from the func()

    Friday, October 18, 2013 8:28 AM
  • User901332479 posted

    sir then it would become a global varible i need to use it as an local varible ,

    then it is so that local variable cant be use it as an static 

    Friday, October 18, 2013 8:32 AM
  • User-417640953 posted

    Hello,

    Thanks for the post.

    C# doesn't support static locals at all.  Anything static needs to be a member of a type or a type itself. And VB.Net does support for static locals,

    however it's accomplished by re-writing your code at compile time to move the variable to the type level.

    For your code, I'd like to do like below.

    int func(int param)
            {
                int x = param;
                x++;
                Console.WriteLine(x);
                return x;
            }
            static void Main(string[] args)
            {
                
                Program x = new Program(); 
                x.func(x.func(0)); 
                //more.... 
                //x.func(x.func(x.func(0)));
                 
                Console.ReadKey();
    
            }

    Hope this helps, thanks.


    Best Regards!  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2013 3:36 AM