C# static

HTML
CSS
C#
SQL

static Keyword in C#

In C#, static is a keyword or modifier that belongs to the type rather than an instance. This means that an instance is not required to access static members. In C#, static members can include fields, methods, constructors, classes, properties, operators, and events. To access static members from another class, you reference them using the class name. If you are accessing static members within the same class, you can call them directly without needing to reference the class name.

Commonly used static members are fields & methods and other things you can learn in case in the future if it is required.

For Example:

We are calling static fields as name and age in the program class from the person class by using the reference of the class name and printing the name and age value in the console

using System;
class Person
{
    public static string name = “Hamza”;
    public static int age = 46;
}
class Program
{
    static void Main()
    {
        Console.WriteLine(“Name :” + Person.name);
        Console.WriteLine(“Age :” + Person.age);
    }
}

Output:

Name: Hamza
Age: 46

Explanation:

using System; 

Includes the System namespace, which provides basic input-output functionality

class Person 

Defines a class named Person Static field for storing the name

public static string name Hamza”;

Declares a static string field named ‘name’ and initializes it with the value “Hamza” Static field for storing the age

public static int age = 46;

Declares a static integer field named ‘age’ and initializes it with the value 46

class Program 

Defines a class named Program

static void Main() 

Defines the Main method, which is the entry point of the application

Accessing and printing the static field ‘name’ from the ‘Person’ class

Console.WriteLine(“Name: ” + Person.name);

Prints “Name: ” followed by the value of the static field ‘name’ in the Person class

Accessing and printing the static field ‘age’ from the ‘Person’ class

Console.WriteLine(“Age: ” + Person.age); 

Prints “Age: ” followed by the value of the static field ‘age’ in the Person class

2a. We are calling the static method named  Data in the program class from the person class by using the reference of the class name and printing the name and age value in the console

using System;
class Person
{
    public static void Data()
    {
        string name = “Hamza”;
        int age = 46;
        Console.WriteLine(“Name :” + name);
        Console.WriteLine(“Age :” + age);
    }
}
class Program
{
    static void Main()
    {
        Person.Data();
    }
}

Output:

Name: Hamza
Age: 46

Explanation:

using System; 

This line includes the System namespace, providing access to fundamental classes and methods

class Person 

This defines a class named ‘Person’

string name = “Hamza”; 

Declares and initializes a string variable named ‘name’ with the value “Hamza”

int age = 46; 

Declares and initializes an integer variable named ‘age’ with the value 46

Console.WriteLine(“Name :” + name); 

Outputs the value of ‘name’ variable to the console

Console.WriteLine(“Age :” + age); 

Outputs the value of ‘age’ variable to the console

class Program 

This defines a class named ‘Program’

static void Main() 

This is the entry point of the application, the ‘Main’ method

Person.Data(); 

Calls the ‘Data’ method of the ‘Person’ class to display name and age

2b. We are calling the non-static method named  Data in the program class from the person class by creating an instance(object) of the class and printing the name and age value in the console

using System;
class Person
{
    public void Data()
    {
        string name = “Hamza”;
        int age = 46;
        Console.WriteLine(“Name :” + name);
        Console.WriteLine(“Age :” + age);
    }
}
class Program
{
    static void Main()
    {
        Person call = new Person();
        call.Data();
    }
}

Explanation

using System;
class Person

This defines a class named Person.

public void Data()

This defines a public method named Data within the Person class.

string name = “Hamza”;

This define and initializes a string variable ‘name’ with the value “Hamza”.

int age = 46;

This define and initializes an integer variable ‘age’ with the value 46.

Console.WriteLine(“Name :” + name);

This prints “Name : Hamza” to the console.

Console.WriteLine(“Age :” + age);

This prints “Age : 46” to the console.

class Program

This defines a class named Program.

static void Main()

This is the Main method, the entry point of the program.

Person call = new Person();

This creates an instance of the Person class named ‘call’.

call.Data();

This calls the Data method and prints the name and age to the console.

3. We are calling the static method named Data in the same class without using the reference of the class name and printing the name and age value in the console

using System;
  class Person
{
    public  static void Data()
    {
        string name = “Hamza”;
        int age = 46;        Console.WriteLine(“Name :” + name);
        Console.WriteLine(“Age :” + age);
    }
    static void Main()
    {
        Data();
    }
}

Output:

Name: Hamza
Age: 46

Explanation

using System; 

This line includes the System namespace, providing access to fundamental classes and methods

class Person 

This defines a class named ‘Person’

public static void Data() 

This defines a static method named ‘Data’ inside the ‘Person’ class

string name = “Hamza”; 

Declares and initializes a string variable named ‘name’ with the value “Hamza”

int age = 46; 

Declares and initializes an integer variable named ‘age’ with the value 46

Console.WriteLine(“Name :” + name); 

Outputs the value of ‘name’ variable to the console

Console.WriteLine(“Age :” + age); 

Outputs the value of ‘age’ variable to the console

static void Main

This is the entry point of the application, the ‘Main’ method

Data(); 

Calls the ‘Data’ method of the ‘Person’ class to display name and age

Task:

Static Field:
Create a class named MathHelper with a static field PI of type double initialized to 3.14.
Create a static method CalculateCircleArea(double radius) that calculates and returns the area of a circle using the formula PI * radius * radius.
Use the static field and method in the main method to calculate and print the area of a circle with radius 5.

Static Property:
Create a class named Counter with a static property Count of type int.
Implement a static method IncrementCount() that increments the Count property by 1.
Create objects of the Counter class and call the IncrementCount() method multiple times to demonstrate the incrementing behavior of the static property.

Static Constructor:
Create a class named Logger with a static field logFilePath initialized to a default log file path.
Implement a static constructor that initializes logFilePath based on the current date, e.g., “log-2024-07-06.txt”.
Create objects of the Logger class (although you won’t access them directly in this case), and observe how the static constructor initializes the static field.

Static Method:
Create a class named Utility with a static method IsPrime(int number) that checks if the given number is a prime number.
Use the static method in the main method to check and print whether numbers like 7, 12, and 23 are prime.

Static Class:
Create a static class named Constants that contains constant fields for common mathematical constants like PI, Euler’s number (e), Golden ratio, etc.
Use these constant fields in a simple calculation (e.g., area of a circle using PI) in the main method and print the results.