13

I recently discovered that structs in C# can have methods.

Quite accidentally, I found myself to have been using the static method of an empty struct in my code, rather than the static method of a static class that I thought I was working with!

e.g.

public struct Foo
{
    public static void Bar(Param param)
    {
        ...
    }
}

It's not really being used as a struct at this point, as it has no properties at all!

Is this very different from using a static method of a class (static or otherwise)? Are there any reasons to prefer one over the other? (My gut tells me that using the static struct method is, at minimum, less intuitive)

2 Answers 2

24

No, static members belong to the type and not to instances of the type. There is no difference (neither with respect to performance nor semantics) between declaring static class members and static struct members.

It is important to note that if a type's only function is to contain static members, you should use a static class instead. With structs, there is an implicit and unchangeable public, no-argument constructor. If the type will not have any instance methods, the ability to create instances should be removed. Declaring a class static is the same as declaring it abstract sealed, so developers will not be able to accidentally create instances that have no purpose.

Sign up to request clarification or add additional context in comments.

5 Comments

Declaring a class static has the added benefit (over abstract sealed) of allowing the compiler to flag any usages of the type in variable declarations or casts as errors.
@mikez That too, as well as flagging any instance member declarations as errors.
@mikez I think a more important distinction is that abstract sealed class is simply invalid C#.
@svick Of course, my point was that one shouldn't think of static class as "the same as" abstract sealed class. A static class has the additional restrictions compared to those conferred by abstract and sealed.
There is this funny thing about a struct with no non-static data that your instance is already assigned before you use that "implicit" no-argument instance constructor. For example the following is OK: static void Example() { Foo local; var okStr = local.ToString(); var okBool = local.Equals(local); } Here Foo is a struct like in the question. The funny thing is that local looks unassigned, but since there are no instance fields, every field is (vacuously) assigned, so this is not a use of unassigned variable. The C# compiler is happy, and it runs.
3

The behavior is no different. C# bastardized structs by hugely increasing the intersection of struct and class features. Personally I would just use a class because it seems more correct to me (certainly more in line with the conventions in most languages).

2 Comments

One could define methods and constructors for structures in C++ already, so it is not a brand new feature that was first introduced in C#.
What about a struct which has [StructLayout(LayoutKind.Explicit)] set? That couldn't be replaced by a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.