s

C# static class edit button Edit

author
Rakesh Kumar Sutar | calendar 20 January 2021 | 2022

Introduction

A static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

Example

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{

    class Programs
    {
        static void Main(String[] args)
        {
            var result = Calculator.Sum(10, 25); 
            Calculator.Store(result);

            var calcType = Calculator.Type; 
            Calculator.Type = "Scientific";

            Console.WriteLine("\t" calcType);
            Console.WriteLine("\t" result);
            Console.ReadLine();
        }
    }

    public static class Calculator
    {
        private static int resultStorage = 0;

        public static string Type = "Arithmetic";

        public static int Sum(int num1, int num2)
        {
            return num1   num2;
        }

        public static void Store(int result)
        {
            resultStorage = result;
        }
    }

}

Output