What is the difference between a static variable in C++ vs. C#?

后端 未结 3 1392
再見小時候
再見小時候 2021-01-17 23:24

Do static variables have the same or similar functionality in C# as they do in C++?

Edit:

With C++ you can use static variables in many different contexts -

3条回答
  •  终归单人心
    2021-01-17 23:47

    Static has multiple meanings in C++.

    Static variables in C# basically only have a single meaning: variables scoped to a type. In C#, static on a type is used to denote a type-scoped variable. Static on a method is a type-scoped method. Static can also be used on a class to denote that the entire class is comprised only of static methods, properties, and fields.

    There is no equivelent to static variables within a function scope, or non-class scoped static values.


    Edit:

    In reponse to your edit, C# basically only uses static for class members. Globals and local static function variables are not supported in C#. In addition, as I mentioned above, you can flag an entire class "static", which basically just makes the compiler check that there are no non-static members in the class.

提交回复
热议问题