Class and Object

Class and Object

In C#, a class is a blueprint or a template that defines the structure, behavior, and properties of an object. It encapsulates data and behavior in a single unit and can be thought of as a user-defined data type.

An object, on the other hand, is an instance of a class. When a class is defined, no memory is allocated, but when an object is created from the class, memory is allocated to hold the object’s data. Objects are created using the “new” keyword, and they can interact with other objects or methods that are defined within their class.

Here’s an example of a simple class in C#:

csharp

Copy code

publicclassPerson { publicstring Name { get; set; } publicint Age { get; set; } publicvoidSayHello() { Console.WriteLine(“Hello, my name is ” + Name + ” and I am ” + Age + ” years old.”); } }

This class defines a Person object with a Name and Age property, as well as a SayHello method that can be used to print a greeting to the console. To create an instance of this class, you can use the following code:

java

Copy code

Personjohn = newPerson(); john.Name = “John”; john.Age = 30; john.SayHello();

This creates a new Person object named “john” with a Name of “John” and an Age of 30, and then calls the SayHello method to print a greeting to the console.

Share this post
[social_warfare]
Structure and enumerator
Installation

Get industry recognized certification – Contact us

keyboard_arrow_up