Showing posts with label static class in c#. Show all posts
Showing posts with label static class in c#. Show all posts

Static Class in #

A static class is the same as a non-static class, but a static class cannot be instantiated. In other words, you cannot use the new keyword to create an object of the class type. The static keyword in the C# programming language allows you to define static classes and static members. A static class is similar to a class that is both abstract and sealed. To declare a class as static, you should mark it with the static keyword in the class declaration.



Syntax of Static Class


static class classname
{
    //static data members
   //static methods
}



Example of Static Class

using System;
namespace StaticClassExampleNameSpace
{
    public static class StaticClassExamleClass
    {
        public static double CelsiusToFahrenheit(string temperatureCelsius)
        {
            // Convert argument to double for calculations.
            double celsius = Double.Parse(temperatureCelsius);
            // Convert Celsius to Fahrenheit.
            double fahrenheit = (celsius * 9 / 5) + 32;
            return fahrenheit;
        }

        public static double FahrenheitToCelsius(string temperatureFahrenheit)
        {
            // Convert argument to double for calculations.
            double fahrenheit = Double.Parse(temperatureFahrenheit);
            // Convert Fahrenheit to Celsius.
            double celsius = (fahrenheit - 32) * 5 / 9;
            return celsius;
        }
    }
}


static class in c#

A static class can be created using a keyword called "static" used at the class definition. A static class can contain only static members (static data members and static methods). You can‘t create an object for the static class.
Note:
·        C# static class contains only static members.
·        C# static class is sealed.
·        C# static class cannot contain instance constructors.
·        C# static class cannot be instantiated.

Syntax:-


static class StaticClassname
{
    //Create static data members
   //Create static methods
}


Example:-1


public static class FileLoggerStaticClass
    {
        public static void Log(string message)

        {
            //Method to log data .
        }
    }


The C# static class is like the normal class but it cannot be instantiated.so a static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.

c# Static

Data members, member functions, properties and events can be declared either as static. C# includes "static" keyword just like other programming languages such as C++, so The Static keyword can be applied on classes, variables, methods, properties, operators, events and constructors Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events

The Static modifier makes an item non-instantiable, it means the static item cannot be instantiated. If the static modifier is applied to a class then that class cannot be instantiated using the new keyword. If the static modifier is applied to a variable, method or property of class then they can be accessed without creating an object of the class
Example:-
public static class MyFirstStaticClass

{

  public static int myFirstStaticVariable = 0;



  public static void MyFirstStaticMethod()

  {

    Console.WriteLine("This is a static method.");

  }



  public static int MyFirstStaticProperty { get; set; }

}





class Program

{



  static void Main(string[] args)

  {



    Console.WriteLine(MyFirstStaticClass.myFirstStaticVariable);



    MyFirstStaticClass.MyFirstStaticMethod();



    MyFirstStaticClass.MyFirstStaticProperty = 100;



    Console.WriteLine(MyFirstStaticClass.MyFirstStaticProperty);

  }

}


MyFirstStaticClass is a static class with a static variable, method, and property. All the static members can be accessed using className