C# Classes ,Objects and Methods.

C# Classes ,Objects and Methods.

Table of contents

No heading

No headings in the article.

A class in C# is a blueprint of an object that is made up of behaviors known as methods. For example a person can be a class and a person walks or talks that's the behavior of the person in this case a method. Classes in C# are represented by the class keyword.

class Person{
public string name;
public string gender;
public int age;
}

A method describes the behavior of a class .Add () at the end to represent the method For example:

walk()
Talk()

Object is an instance of a class. In the class Person we could have personA or PersonB with the attributes name , gender and age from the person class. Objects are instantiated by the new keyword. To access the fields in a class in this case (name,gender,age) use the dot notation(.) .

var personA = new Person();

acess the fields in the class use the dot notation

personA.name ="Mary";
personA.gender="female";
personA.age = 23;

Create another object of personB

var personB = new Person();
personB.name ="John";
personB.gender="male";
personB.age = 29;
using System;

public class Program
{
    public static void Main()
    {
         var personB = new person();
   personB.name ="John";
personB.gender="male";
personB.age = 29; 

      Console.WriteLine($"Hi my name is { personB.name} I am {personB.age} years old");
    }
    class person{
    public string name;
 public string gender;
  public int age;
    }
}