I have the following class:
class Application
{
private Process _process;
private static string _applicationPath = string.Empty;
public void Start(string arguments)
{
if (!File.Exists(_applicationPath))
Deploy();
_process = Process.Start(_applicationPath, arguments);
}
public void SomeMethod()
{
//method that manipulate _process
}
private void Deploy()
{
// copying, installation steps (takes some time) and assign _applicationPath
}
}
Application
will use the same _applicationPath
. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it's explicitly passed in a method parameter.
C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field by using the name of the class. No instance is required. We can also use the static keyword when defining a field.
Public static fields are useful when you want a field to exist only once per class, not on every class instance you create. This is useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Static and Non-Static Methods in C# If you want to consume any non-static members with a static method then you need to create an object and then through the object, you can access the non-static members.
Like their public equivalent, private static methods are called on the class itself, not instances of the class. Like private static fields, they are only accessible from inside the class declaration. Private static methods may be generator, async, and async generator functions. This can lead to unexpected behavior when using this.
Difference between Static and Non-Static fields of a class. Static variables or fields belong to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Non-static fields are instance fields of an object.
Because there is no instance variable, we access the members of a static class by using the class name itself. C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field by using the name of the class. No instance is required.
Non-static fields are instance fields of an object. They can only be accessed or invoked through an object reference. The value of static variable remains constant throughout the class.
It's bad that I'm using a static field?
It depends on what you're using it for. In this case since you change it with a non-static method (Deploy()
), then yes, it's probably bad. If it should be the same for all instances, then set it in a static constructor or property (assuming that the application configuration will set it).
Is this an example of violation of SRP-principle (SOLID)?
What are the responsibilities of this class? Can you extract Deploy
and Start
logically or does one require the other?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With