Understanding Staticity in C #

I am learning C # and it is hard for me to understand the static .

Let's say I have the following code:

 using System; using System.IO; using System.IO.Ports; class PortThing { SerialPort port; void InitPort() { if(!File.Exists("/dev/whatever")) { System.Console.WriteLine("Device not found."); port = null; } //else port = something } public static void Main(string[] args) { InitPort(); System.Console.WriteLine("Done."); } } 

As far as I understand, the static method refers to the class, and not to the object of this class. Therefore, static methods cannot refer to non-static methods / fields, since they require creating an instance of the class.

The compiler complains about Main() calling InitPort() and wants to make it static. I could do this, but that would require making port static field. Following this line of thought, everything will end up being static.

How am I wrong?

+4
source share
5 answers

You get it right. Static methods can only access static members. Non-static members require an instance of the class to access them. So you can do this:

 public static void Main(string[] args) { new PortThing().InitPort(); System.Console.WriteLine("Done."); } 

This way you call the InitPort instance InitPort for this instance of the class, and you can leave the port field non-static.

+10
source
 public static void Main(string[] args) { PortThing pt = new PortThing(); pt.InitPort(); System.Console.WriteLine("Done."); } 
+3
source

You are not getting anything wrong.

The problem here is that Main does not have to be static in a normal class. It should be static here because it provides an entry point to your program. In other words, you need to have a method to run, but you cannot create an object before starting, so this method must be static .

Nothing prevents you from actually creating an object of the PortThing class and usually using it:

 public static void Main(string[] args) { var pt = new PortThing(); pt.InitPort(); System.Console.WriteLine("Done."); } 
+2
source

You got it right: the static method belongs to the class, not the object.

But in this case, if you do not want to make everything static, you can simply create an object of type PortThing, in other words, create an instance of this class and call a non-static method.

Or, if this non-static method does not have access to any class properties, at least non-stationary, you CAN make it static.

+1
source

Static methods and variables are available without an instance of the class.

The most important thing to know about static variables is that in each process there is only one instance of this variable (more precisely, AppDomain, however most processes have only 1 AppDomain). I believe that static variables are global variables. You can create static class constructors to initialize these static variables. The static constructor is called once when this class gets the first access. But keep in mind concurrency issues regarding multi-threaded applications that allow access and modification of static elements, especially static collections!

Static methods can be considered as a function in C. They can be called without creating any object, which is slightly better for performance (but should only be used in those places where they make sense). For example, I would use the int method CountNumberOfCommas (string s) as a static method in a static utility class, since this is an operation that does not depend on any instance of the class.

You are not doing anything wrong. You just need to choose one of two options: you can create an instance of the program and call InitPort, or make InitPort static, and not create an instance of the program. Creating all the static ones is essentially the same as writing a C program, where you define only functions.

0
source

Source: https://habr.com/ru/post/1333038/


All Articles