Posts

Showing posts with the label csharp class

Understanding Classes in C#

Image
Introduction In C# class is a blueprint for creating objects. It contains data (properties) and behaviour (methods). We can build multiple objects with distinct properties and methods using classes without having to repeat the code. Class Syntax: Creating a generic C# Class public class Car //car is the class name and Pascal case { public string Model { get; set; } //properties public string Color { get; set; } public void Start() //method { Console.WriteLine("Car started."); } public void Stop() //method { Console.WriteLine("Car stopped."); } } A generic class in C# is created using a class keyword followed by class name. Class name, property name, method name must always be Pascal case i.e. Starting letter must be capital case. Class can have different properties and methods. We cannot use the class directly without creating an object. In order to use class we must create an object which is an inst...