BCA / B.Tech 10 min read

Classes of .NET With C#

Classes in .NET with C# in Hindi | What is a Class in .NET? 


Introduction Classes: Introduction Classes are used to explain basic concepts in programming. For example, if you want to understand the structure of a class in .NET and C#, this could be the initial code.

Code:

using System;

class Introduction
{
    public void DisplayMessage()
    {
        Console.WriteLine("Hello! This is an example of the Introduction Class.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Introduction intro = new Introduction();
        intro.DisplayMessage();
    }
}

Description:

  • The Introduction Class has a method DisplayMessage(), which shows a message.
  • In the Main() Method, an object of the Introduction class is created and the method is called.
 Class of Addition: This class is created to find the sum (addition) of two numbers.

Code:

using System;

class Addition
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Addition addition = new Addition();
        Console.WriteLine("The sum of two numbers is: " + addition.Add(10, 20));
    }
}

Description:

  • The Add(int a, int b) method takes two inputs and finds their sum.
  • It is called in the main part of the program.

Class of Factorial: This class is useful for finding the factorial. The meaning of factorial is: n! = n * (n-1) * (n-2) ... * 1

Code:

using System;

class Factorial
{
    public int CalculateFactorial(int n)
    {
        int fact = 1;
        for (int i = 1; i <= n; i++)
        {
            fact *= i;
        }
        return fact;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Factorial factorial = new Factorial();
        Console.WriteLine("Factorial of 5: " + factorial.CalculateFactorial(5));
    }
}

Description:

  • The CalculateFactorial(int n) method finds the factorial of a number.
  • The result is obtained by using a For Loop.

Radio Class: This class is for controlling the volume and channel of a radio.

Code:

using System;

class Radio
{
    public int Volume { get; set; }
    public string Channel { get; set; }

    public void Play()
    {
        Console.WriteLine($"Playing Channel: {Channel} at Volume: {Volume}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Radio radio = new Radio();
        radio.Channel = "FM 101";
        radio.Volume = 5;
        radio.Play();
    }
}

Description:
  • Data is set through the Volume and Channel properties.
  • The Play() method shows the radio's information.

Alarm Class: This class is for setting an alarm.

Code:

using System;

class Alarm
{
    public string Time { get; set; }

    public void SetAlarm(string time)
    {
        Time = time;
        Console.WriteLine($"Alarm has been set for: {Time}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Alarm alarm = new Alarm();
        alarm.SetAlarm("7:00 AM");
    }
}

Description:

  • The SetAlarm(string time) method sets the time of the alarm.
  • The user can set the time and confirm the alarm.