开发者

Why static Structures are not allowed in C#? [duplicate]

This question already has answers here: Why can't you declare a static struct in C#, but they can have static methods? (3 answers) Closed 9 years ago.

I always used to consider structures as some sort of lesser privileged things, or something with lesser features. Maybe because of the OOP concepts blowing everything into Classes.

From the little amount of exposure to C#, I understand that Setting a class static, ensures that all its members & functions are static. Also we cannot have a constructor to initialize that class as there is only a single instance.

public static struct mystruct
{
    public static int a;
}

I was pointed out right here at Stack overflow that this is a wrong method. Can someone elaborate.

I got the appropriate error saying "static is not valid for this item" when I created a new cs file & compiled it in console. Strangely when I added this to an existing working project to see if the compiler would complain but to my surprise it doesn't. Any reasons for this开发者_StackOverflow中文版??


A static class is just a container for static members (of any kind - fields, events, properties, and most commonly methods).

A static struct would be exactly the same, so wouldn't provide any advantages - but readers might think it had some special meaning. To avoid confusion, it's therefore prohibited. Conceptually it makes just as much sense as a static class, of course - the difference between structs and classes is really in terms of how instances of them behave, and as there would be no instances of static types of either kind, that difference would be moot.

(Not that I was in the design meeting where this was decided, of course. Eric Lippert may well be able to find some notes about it. The above is just an educated guess. The annotated C# 3 spec is silent on the matter, as far as I can see.)


This doesn't accomplish anything. You'd still have a collection of static methods just like you do with a static class. A struct in C# implies it is a value type instead of a reference type, which has no meaning at the static level.


File that under "that's just the way it is". Since you can do it with a static class, there is not reason to allow static structs. It would just confuse people as to what the difference between them is. They had to pick one or the other.


Also we cannot have a constructor to initialize that class as there is only a single instance.

Actually, there is no instance of a static class. Which kind of explains the lack of support for static structs -- or rather, the lack of any need for such a thing.

The distinction between reference types and value types in .NET (class and struct in C#) is entirely about how instances of these types are handled. An instance of a reference type is accessed via a reference to that instance in the form of a variable. Copies to such references are passed around between method calls. An instance of a value type is accessed directly, and copies of the instance itself are passed around between method calls.

With no instance to speak of, this distinction becomes irrelevant; so any type that consists purely of static members might as well be a static class.


No Instance Field Initializers

In a class we are allowed to create a field/variable and initialize it at the same time A structure cannot contain such initialization. These fields must be initialized through functions or by using the object itself. Fields cannot be given initial values at the time of creation. The following code gives an error:

struct Point
{ 
    public int x = 20; // Error its not possible to initialize
    public int y=20; // Error its not possible to initialize
} 

However a struct can contain static fields, which can be initialized inside the struct. The following example shows the use of static fields inside a struct.

struct  Point  {
        public static int x = 25;
        public static int y = 50;
}

Struct & Methods

A C# struct can also contain methods. The methods can be either static or non-static. But static methods can access only other static members and they can't invoke by using an object of the structure. They can invoke only by using the struct name.

struct Point  
{    
    static int x = 25;    
    static int y = 50;
    public void SetXY(int i, int j)    
    {        
        x = i;        
        y = j;    
    }     

    public static void ShowSum()            
    {        
        int sum = x + y;        
        Console.WriteLine("The sum is {0}",sum);    
    }
}

as found on "http://www.csharpfriends.com/articles/getarticle.aspx?articleid=120"


The concept of static types doesn't make sense for structs. Structs imply copy-by-value semantics - which only makes sense when you can instantiate a type. Static classes were introduced to make it easier to group together static methods in a type that can't be instantiated. Allowing static structs would be both redundant and confusing.

Think about it this way. How would static class Foo and static struct Foo differ in behavior? And if they don't ... why introduce a static struct concept at all? It could only confuse people into thinking there is a difference...


Because static is not defined to be allowed in the language to be applied to structs.

static class already defines the same ability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜