Q:

C# program to demonstrate the static class

0

C# program to demonstrate the static class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate the static class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the static class in C#

using System;

public static class Sample
{
    static int num1;
    static int num2;

    public static void Set(int n1, int n2)
    {
        num1 = n1;
        num2 = n2;
    }

    public static int GetSum()
    {
        return (num1 + num2);
    }
}

class Test
{
    static void Main(string[] args)
    {
        Sample.Set(10, 20);

        Console.WriteLine("Sum: " + Sample.GetSum()); 
    }
}

Output:

Sum: 30
Press any key to continue . . .

Explanation:

In the above program, we created a static class Sample that contains two static data member num1 and num2. The Sample class also contains two static methods Set() and GetNum().

The Set() method is used to set the values of data members. The GetSum() method is used to return the sum of data members.

Now look to the Test class that contains the Main() method. The Main() method is the entry point for the program.

Sample.Set(10, 20);
Console.WriteLine("Sum: " + Sample.GetSum()); 

In the above code, we set the data members of the Sample class using the Set() method, and then get the sum of data members using the GetSum() method that will be printed on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to get the count of total created objec... >>
<< C# program to demonstrate the IDictionary interfac...