xxxxxxxxxx
(static) >> means that the method belongs to the Program class
and not an 'object' of the Program class.
xxxxxxxxxx
In general, static means “associated with the class, not an instance”.
// Search c# static review for more detail
xxxxxxxxxx
In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.
xxxxxxxxxx
The purpose is for a shareable tool that can be used when needed,
it's like a css utility class. It makes no sense to instantiate this since
its supposed to be used frequently. It is constant/unchanging.
-Static members
A shared tool for use in any appropriate need, example DateTime.Now or
Math.PI or Console.WriteLine().
-Static classes
A static class is like a toolset that contains static members/tools.
The purpose of a static class is to provide a convenient way to organize
and access utility functions, shared functionalities, constants, aka static
members.
By making the class static, it restricts the creation of instances and
ensures that all members are accessible directly through the class itself.
This promotes encapsulation, as the static class encapsulates related
functionality within a single entity.
xxxxxxxxxx
public static class MyStaticClass
{
public static int MyStaticMethod()
{
return 42;
}
}
// Usage
int result = MyStaticClass.MyStaticMethod();
xxxxxxxxxx
public class MyClass
{
//Instance variable to be automatically set to five
public int instanceVar = 5;
//Variable belonging to the type/class
public static int typeVar = 10;
}
public class MainClass
{
void Main()
{
//Field is accesible as it is a variable belonging to the type as a whole
Console.WriteLine(MyClass.typeVar);
//The following would throw an error, because the type isn't an instance
//Console.WriteLine(MyClass.instanceVar)
//Create an instance of the defined class
MyClass instance = new MyClass();
//Writes 5, because we told it to be set to 5 upon creation of the object
Console.WriteLine(instance.instanceVar);
instance.instanceVar += 22; //Add 22 to the instance's variable
NyClass second = new MyClass(); //Create a second instance, named 'second'
Console.WriteLine(instance.instanceVar);//27, unless i did my math wrong :/
//The second instance has it's own variable separate from the first
Console.WriteLine(second.instanceVar);//5
}
}
xxxxxxxxxx
using System;
class Example
{
static int _count;
public static int Count
{
get
{
// Side effect of this property.
_count++;
return _count;
}
}
}
class Program
{
static void Main()
{
Console.WriteLine(Example.Count);
Console.WriteLine(Example.Count);
Console.WriteLine(Example.Count);
}
}
////////////////////
1
2
3
xxxxxxxxxx
Static class - class that can't be instantiated
While normal classes purpose is to create hierarchies, expand each other's logic
and stuff, static classes are easier to use when you need your logic and
properties be used from one certain place.
--------------------------------------------------------------------------------
public static class TextPrinter
{
public static int counter = 0;
public static void Print_1() { Console.WriteLine("Printed Text 1"); }
public static void Print_2() { Console.WriteLine("Printed Text 2"); }
public static void Print_3() { Console.WriteLine("Printed Text 3"); }
}
--------------------------------------------------------------------------------
You can't call TextPrinter element like this:
TextPrinter txtPt = new TextPrinter();
txtPt.Print_1(); or txtPt.counter++;
But you can like this: // No additional operations, just class name
TextPrinter.Print_1();
TextPrinter.counter++;
It's much easier that way if you need to keep general data, or have general
logic that can be operated using one simple object.
xxxxxxxxxx
using System;
using static System.Console;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
WriteLine("Hello, world!"); // Accessing WriteLine directly without Console class
}
}
}