Skip to main content

Posts

Showing posts from June, 2022

Access Modifiers in C#

How many access modifiers in c# In this article, we will learn about the access modifiers in C#. 1. Public Access Modifier in C# The class member, that is defined as a Public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace. public class Vehicle { public void Color() { Console.WriteLine("Vehicle color red"); } } 2. Private Access Modifier in C# The private access modifier restrict the member variable or function to be called outside of the parent class. A private function or variable cannot be called outside of the same class. It hides its member variable and method from other class and methods. public class Vehicle { private string Name; public void Color() { Console.WriteLine("Vehicle color red"); } } In the above example, you cannot call name variable outside the class because it is declared as private. 3. Protected

What is the sealed class in C#

In this article, we will learn about the sealed class and method in C#. C# Sealed Class A sealed class, in C#, is a class that cannot be inherited by any class but can be initiated. When we don't want a class can be inherited by another class, We can declare that class as sealed class. A sealed class cannot have a derived class. We use the sealed keyword to create a sealed class. sealed class Vehicle { } class Car : Vehicle { } class Program { public static void Main(string[] args) { Car car = new Car(); Console.ReadLine(); } } In the above example, we have created a sealed class Vehicle and then we are trying to derived Car class from the Vehicle class. when we are trying to compile code it'll give following errors.       C# Sealed Method When we don't want method can be override by another class, We can declare that method as sealed method. We use a sealed keyword with an overridden method to create a sealed method. class Vehicle