C# is an object-oriented programming language. In Object-Oriented Programming methodology
A program consists of various objects that interact with each other by means of actions. The actions that an object may take are called methods.
Objects of the same kind are said to have the same type or, more often, are said to be in the same class.
using System;
// Dog class
class Dog
{
public void Bark()
{
Console.WriteLine("Woof! Woof!");
}
}
class Program
{
static void Main()
{
// Creating an object of the Dog class
Dog dog = new Dog();
// Calling the method on the Dog object
dog.Bark(); // Output: "Woof! Woof!"
}
}
Output
Woof! Woof!
-
class Dog
:
This defines a class calledDog
. It’s like a blueprint for creating objects that represent dogs in our program. -
public void Bark()
:
This is a method (action) inside theDog
class. -
class Program
:
This defines another class calledProgram
, which will be our starting point for the program. -
static void Main()
:
This is the entry point of the program. TheMain
method is where the program starts executing. -
Dog dog = new Dog();
:
This line creates an object of theDog
class. It’s like creating a new dog based on the blueprint defined in theDog
class. -
dog.Bark();
:
This line calls theBark
method on thedog
object. It’s like telling the dog to perform the “Bark” action