The static keyword in C# is used to declare static members or static classes. Static members and classes belong to the type itself rather than an instance of the type. This means they are shared across all instances of the class (or exist without any instance).
Key Uses of static
1. Static Members (Fields, Properties, Methods, Events):
2.Static Classes:
3. Static Constructors:
A static constructor initializes static data or performs actions that only need to be performed once.
It is called automatically before any static member is accessed or an instance is created.
Static properties are used to encapsulate static fields with get and set logic.
public class AppConfig
{
private static string _appName = "MyApplication";
public static string AppName
{
get => _appName;
set => _appName = value;
}
}
Usage
Console.WriteLine(AppConfig.AppName); // Output: MyApplication
AppConfig.AppName = "NewName";
Console.WriteLine(AppConfig.AppName); // Output: NewName
2. Static Fields and Methods
public class Calculator
{
public static int Add(int a, int b) => a + b; // Static method
public static int Counter = 0; // Static field
}
Usage
int sum = Calculator.Add(5, 10); // Call static method without creating an object
Console.WriteLine(sum); // Output: 15
Calculator.Counter++; // Access static field
Console.WriteLine(Calculator.Counter); // Output: 1
The static keyword in C# is used to declare static members or static classes. Static members and classes belong to the type itself rather than an instance of the type. This means they are shared across all instances of the class (or exist without any instance).
Key Uses of static
1. Static Members (Fields, Properties, Methods, Events):
2.Static Classes:
3. Static Constructors:
A static constructor initializes static data or performs actions that only need to be performed once.
It is called automatically before any static member is accessed or an instance is created.
Static properties are used to encapsulate static fields with get and set logic.
public class AppConfig { private static string _appName = "MyApplication"; public static string AppName { get => _appName; set => _appName = value; } }
Usage
Console.WriteLine(AppConfig.AppName); // Output: MyApplication AppConfig.AppName = "NewName"; Console.WriteLine(AppConfig.AppName); // Output: NewName
2. Static Fields and Methods
public class Calculator { public static int Add(int a, int b) => a + b; // Static method public static int Counter = 0; // Static field }
Usage
int sum = Calculator.Add(5, 10); // Call static method without creating an object Console.WriteLine(sum); // Output: 15 Calculator.Counter++; // Access static field Console.WriteLine(Calculator.Counter); // Output: 1
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved