3

Possible Duplicate:
C#.NET - Why do members of a static class need to be declared as static? Why isn't it just implicit?

I am getting an interesting error, in that when I call a method (which I don't explicitly declare as static) from within a statically declared class, I get a message saying

An object reference is required for the non-static field, method, or property 'MangoTree.Twitter.OAuthClient.PerformRequest(System.Collections.Generic.Dictionary, string, string, string, MangoTree.Twitter.OAuthClient.RequestType)'

When I explicitly declare the method as static, the error goes away, and I can remove the static modifier from the class declaration and the error stays away. What's confusing me is that I was under the impression that when I declared the class as static, everything within the class should automatically be static as well, without me having to explicitly declare it so.

Community
  • 1
  • 1
Rahul Gupta-Iwasaki
  • 1,683
  • 2
  • 21
  • 39
  • 3
    replica of http://stackoverflow.com/questions/6005109/c-net-why-do-members-of-a-static-class-need-to-be-declared-as-static-why-isn....with a great answer of Eric.... – Usman Jul 13 '12 at 04:48

5 Answers5

5

What's confusing me is that I was under the impression that when I declared the class as static, everything within the class should automatically be static as well

All members of a static class must indeed be static, but it is not happening automatically: you must explicitly declare all the members static. The purpose of declaring a class static is to let the compiler perform a check that all members are static, and to prevent any attempt at creating an instance of your static class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

All members of static class must be static.

Please read this article for why?

http://www.codeproject.com/Articles/15269/Static-Keyword-Demystified

Dinesh
  • 3,652
  • 2
  • 25
  • 35
0

If you create a static class in C#, methods inside need to be declared static:

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

Example:

static class CompanyInfo
{
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }
    //...
}
Luxspes
  • 6,268
  • 2
  • 28
  • 31
0

You cant call a non static member function without creating an instance of the class. So in your first case since static class cant be instantiated you are getting the error when trying to call a non static function from a static function.

However if your class is non static and there are two methods of which one is static, you can very well call the non-static member function inside static function like this

public class Foo
{
    public void Test()
    {
    }

    public static void Test1()
    {
         Foo foo = new Foo();
         foo.Test1();
    }
}

Since static members and classes are instantiated well before instantiating the normal classes , the static function will not know the existence of a normal function.

In the second case since both the functions are declared static there is no problem.

Jeeva
  • 4,585
  • 2
  • 32
  • 56
0

C# compiler marks static classes as 'sealed abstract' because of which you're never able instantiate static classes or inherit from the static classes. And because of this It serves no real value to define non-static members in a static class because you will never be able to instantiate the class hence never be able to call the non-static members. So you have to define only static members in a static class. Weather a class should be static or not is design design decision.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137