Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a static struct method and a static class method?

Tags:

methods

c#

static

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)

like image 860
Raven Dreamer Avatar asked Nov 14 '12 02:11

Raven Dreamer


People also ask

What is difference between static method and static class?

A static class can only contain static members. A static method ensures that, even if you were to create multiple classB objects, they would only utilize a single, shared SomeMethod function. Technically, there's no difference, except that ClassA's SomeMethod must be static.

What is the difference between static method and class method?

Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self . They work like regular functions but belong to the class's namespace.

Can a struct have static method?

A struct can have its own static and instance methods (or functions) but are not created within the struct itself. These methods exist in a separate block using impl and they can be either static or instance methods.

What is the difference between static and nonstatic methods in Java?

The static method uses compile-time or early binding. The non-static method uses runtime or dynamic binding. The static method cannot be overridden because of early binding. The non-static method can be overridden because of runtime binding.


2 Answers

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.

like image 96
cdhowie Avatar answered Oct 06 '22 11:10

cdhowie


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).

like image 45
evanmcdonnal Avatar answered Oct 06 '22 12:10

evanmcdonnal