Static Classes, Good or Bad? [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have read something's, about static classes, mostly about that static classes are "evil" in Java, and I was wondering what does the Static calss actually do?
What are the applications to it Unity C#, and C# in general?

"The static modifier makes an item non-instantiable, it means the static item cannot be instantiated. If the static modifier is applied to a class then that class cannot be instantiated using the new keyword. If the static modifier is applied to a variable, method or property of class then they can be accessed without creating an object of the class, just use className.propertyName, className.methodName."
Static class basically means that there is just one instance of the object.
It can be good or bad, depends on what you need, for example if you have an int to store the player money you can use static int money and then get or set the variable
from anywhere, but if you want to create something multiple time (like enemies etc') you cann't use it.
Here is a link to read more about the Static class

Related

Using static variables and methods vs. non-static [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am developing an ASP/c# webform where I am using JQuery as well. I came into a scenario where I need to call. C# function from JQuery. In order to that, I found that function in c# has to be a static method (web method).
The problem is that I need to access all variables, arrays, etc which I used to populat some data and these are not stated c variables. Also, from the web method I need to re-use some the functions which are not static. I ended up gradually just changing all methods and variables to static.
I would like to know if the approach I am taking is correct, and whether there is any pitfall of using static variables/methods and what in simple words makes a difference between static/none-static.
Static variables can be called directly by using class names such as
public class IhaveStatic
{
public static string Hello = "Hello I am A";
}
When you use static this means this will be in memory for life time of your process.
now consider another class as
public class IhaveNoStatic
{
public string Hello = "Hello I am B"
}
public class C
{
Console.WriteLine(IhaveStatic.Hello); // Correct
IhaveNoStatic obj = new IhaveNoStatic();
Console.WriteLine(obj); // Correct
Console.WriteLine(IhaveNoStatic.Hello); // Compile time error
}
as you can see that you need to create object of that class "IhaveNoStatic" to access non-static variable. So, this will be in memory until there is an instance of that class exist.
So, basically it's on your requirement but it is good to use less static variable in your programs.

Should a class with only static methods be static? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a class with only static methods. Should the class itself be made static too? Does it matter?
Does it matter?
Making a class static ensures that it can never be instantiated by generating a compiler error should the user attempt to do so. If the class, consisting of only static members, is simply not intended to be instantiated, there is no reason not to make it static. You can choose not to do so, but instances of such a class aren't going to be very useful, and users creating these instances are going to be left quite confused.
On the other hand, if you intend for instances of this class to be created but you expect derived classes to implement their own instance members, chances are that this class should be abstract, rather than static (and perhaps those instance members should be stated upfront via abstract definitions or an interface).
In general: Yes.
You can prevent the programmer to create object instances of a certain class by making the class static. If this is what you intend, then do it. This prevents mistakes, by showing (other collegues, etc.) that the class is not intended to be instantiated.
public static class A
{
// Some static member
}
A a = new A(); // Compilation error

c# and functional purity [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I've been having to use c# lately, which I don't have much experience in. a conundrum I keep finding myself in is, when building a class, having state be dependent on the state initialized before it
class foo{
public bar_ {get;}
public dum_ {get;}
public foo (){
bar_ = BuildBar();
dum_ = BuildDum(bar_);
}
}
its a bit redundant for BuildDum to carry a parameter if it's just going to use something already accessable from a member. on the other hand I like explicitly pointing out dependencies a function relies on
I guess I am asking: what is the best way to handle the situation?
Both ways are fine. The current version of BuildDum could be made static, in which case it's perfectly fine for the method to not access any member variables, because it cannot do it anyway:
private static Dum BuildDum(Bar b) {
...
}
If you make BuildDum that accesses bar_ directly, you should also make it access _dum, i.e. it should be a non-static void:
private void BuildDum() {
...
_dum = ...
}

When to use setter and getter if they are just used in one class [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a class and use private vars in that class, is there any need then for setter or getter?
I think they are just needed if I want to access the vars from another class right?
You're sort of right.
If you need to expose any of your private fields to other classes, you'll want to generate a property with the appropriate access modifier (public, protected, internal, or protected internal).
If you don't need to expose your private fields, reading from/assigning to a field directly is usually fine.
But even so, having private properties encapsulating private fields might be useful in some cases, such as:
when you need to validate a value before assigning it to the field;
lazy loading a value before reading from it, etc.

A class of Global Variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Recently I am working a server that transfers commands and data and in my global variables, I have a 100 constants that I use through out my program for communication protocols. Is there a way I can make a class of global variables and then access that class when needed?
One way would be to create a class and mark it as static:
public static class GlobalVariables
{
public static int GlobalInt;
public static float GlobalFloat;
}
You'll be able to access these anywhere in your program.
If you do not want anyone to be able to edit these values, you could mark them with the const keyword:
public const int GlobalInt = 15;
Create a static class and mark your fields with const keyword, it's implicitly static and you won't be able to overwrite them by accident.
In addition to the suggestions above, is there any chance that these "constants" might change in the future? The reason I ask is that you mentioned they were communication protocols. If its things like addresses, ports or anything else that might change, consider using the static class and on that class use a static constructor to read the values from the configuration or have the static properties of that class just refer to the configuration. You don't want a firewall change to force you to have to recompile your code.
If a value has no chance of changing, it should be a const.

Categories

Resources