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
Related
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
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 2 years ago.
Improve this question
I was looking at the TextReader class, but I only see method prototypes, I can't see the definitions anywhere. I'm interested in how things work under the hood and I tried looking for the definition of public void Dispose(); but trying to peek or go to definition in VS2019 just returns me here. Where are they stored?
How is it possible to prototype like this? I tried doing it and it wouldn't allow me. This particular method is not virtual.
I'm guessing by "method prototypes" you mean the headers of the methods, i.e. these things:
public virtual Task<String> ReadLineAsync()
The body of the method (or in your words, "definition") is not shown in Visual Studio. One place you can find them, is https://referencesource.microsoft.com/. For example, here is the source for TextReader.
Note that TextReader is an abstract class. Some of the methods in an abstract class can have no bodies by design (though this is not the case with TextReader). For an interface, all methods have no bodies*. If you want to see the implementations of those abstract methods, you need to go to a concrete implementation. For TextReader, this could be StreamReader.
You can create these so called "prototypes" by creating an interface (though it's not the same kind of thing as the one you see in TextReader):
interface IFoo {
int Method1(string param);
string Method2(string p1, string p2);
...
}
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.
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 6 years ago.
Improve this question
I don't know whether this question will make sense or if it's a naive question, but I haven't been able to find a satisfactory answer.
I would like to be able to share code across multiple static classes. Is there an "equivalent" of an abstract base class for static classes (i.e. can I have one static class inherit from another one)? If not, why not? Is there another way of sharing code across multiple static classes in a manner analogous to what you do with an abstract base class?
Also, how does a static class compare to a "sealed" class?
You're quite right. Reflection will show that a static class is both abstract and sealed:
public static class MyStaticTest {
}
...
// I'm abstract
Console.WriteLine(typeof(MyStaticTest).IsAbstract ? "I'm abstract" : "");
and that's why you can't create an instance (compile time error): new MyStaticTest();. However, reflection will show that any static class is sealed as well:
// I'm sealed
Console.WriteLine(typeof(MyStaticTest).IsSealed ? "I'm sealed" : "");
and so you can't inherit from it (compile time error): public class MyClass: MyStaticTest {..}. Thus, all you can do with static class is to declare static members (static fields, properties, methods etc.)
The abstract keyword enables to create classes and class members that are incomplete and must be implemented in a derived class whereas The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
I think its complete for Above
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 7 years ago.
Improve this question
I am learning C#, but I do not understand what's the use of method hiding? I searched the web but I still don't understand exactly how it works. Can anyone explain how it works?
Method hiding is a technique used to replace a non-virtual method of a base class with new functionality in the child class. It has some nasty behaviour that can easily catch people out. To explain by way of an example:
class BaseClass
{
public void Foo()
{
Console.WriteLine("BaseClass");
}
}
class ChildClass : BaseClass
{
public new void Foo()
{
Console.WriteLine("ChildClass");
}
}
ChildClass obj1 = new ChildClass();
BaseClass obj2 = obj1;
obj1.Foo(); // Prints "ChildClass"
obj2.Foo(); // Prints "BaseClass"
If Foo had been declared virtual, new would not be needed and both obj1.Foo() and obj2.Foo() would have printed ChildClass in the example above.
The only other thing you need to know about it is that these days using inheritance is generally frowned upon (do a search of either "inheritance is evil" or "composition vs inheritance" for reams of info on why this is). You therefore shouldn't need to worry about method hiding (unless someone inflicts it upon you with their old and/or misguided code).
Method hiding refers to Information Hiding which is a very basic pattern generally used in programming. Basically you usually want to achieve a clean separation of your components and implementation details from interfaces.