C# static

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:

1. We are calling the static method named Data and static field name value 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 int value = 24;
    public  static void Data()
    {
        string name = “Hamza”;
        int age = 46;        
Console.WriteLine(“Name :” + name);
Console.WriteLine(“Age :” + age);
    }
    static void Main()
    {
      Console.WriteLine(value);
        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 int value = 24;

Declares and initializes a public static int field named ‘value’ with the value ’24’

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

Console.WriteLine(value);

Printing value field of the ‘Person’ class in the console.

Data(); 

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

2. We are calling the static field name as value and static data method  in the program class from the person class by using the reference of the class name and printing the field  and data  value in the console

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

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 int value = 24;

Declares and initializes a public static int field named ‘value’ with the value ’24’

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

class Program 

Defines a class named Program

static void Main() 

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

Console.WriteLine(Person.value);

Printing value field of ‘Person’ class in the console using class reference of Person class.

Person.Data(); 

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

3. We are calling the non-static method named  Data  and the filed named value 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 int value = 24;
    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();
       Console.WriteLine(call.value);
        call.Data();
    }
}

Explanation

using System;
class Person

This defines a class named Person.

public int value = 24;

Declares and initializes a public int field named ‘value’ with the value ’24’

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’.

Console.WriteLine(call.value);

This calls the value field and prints the value = 24 to the console.

call.Data();

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

Differences

Course Video :

Task:

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.