C# Keywords

Keywords in C#

1. Type keyword

These type keywords are fundamental in C#, allowing you to declare variables, define method return types, and manage data throughout your programs.

Operator Description Example
bool Represents Boolean values (true or false) bool male = true;
char Represents a single character char letter = ‘A’;
decimal It only gives output with 29-digit numbers with a decimal point or less than 29 decimal price = 19.9999999m;
double It only gives output with 15-digit numbers with a decimal point or less than 15 double price =19.99999999;
float It only gives output with 7-digit numbers with a decimal point or less than 7 if the number of digits is 7 it will round the last number otherwise not float percentage = 19.999999f;
int Represents an integer, whole number int count = 100;
object The object type can be used to store any type of data. object name= “Hello”; object Age = 24; object male = true
string Represents a sequence of characters string greeting = “Hello, World!”;
void Specifies that a method does not return any value public void Display() { /* code */ }

2. Modifier Keywords:

modifier keywords are commonly used to define the behavior and characteristics of classes, methods, fields, and other elements in C#. Each keyword has a specific purpose and usage in the language. Below are the examples of modifiers.

Abstract Class: Use abstract in a class when you want to create a base class that should not be instantiated on its own. Instead, it should serve as a blueprint for other classes.

Abstract Method: Use abstract in a method when you want to declare a method that does not have a body in the base class. This means that any class inheriting this abstract class must provide its own implementation of the method.

For example:

public abstract class Day
 {
    public abstract void Greet();
  }
public class Evening : Day
  {
     public override void Greet()
    {
        Console.WriteLine(“Good Evening”);
    }
  }
public class Night : Day
   {
public override void Greet()
     {
       Console.WriteLine(“Good Night”);
    }
 }

Explanation:

using System;

This line tells the program to use the System library which includes useful methods for our program.

public abstract class Day

Define an abstract class named Day. Abstract means we can’t create an object of this class directly.

public abstract void Greet();

This is an abstract method Greet() which means it has no implementation here and must be implemented in a derived class.

public class Evening : Day

Define a class named Evening that inherits from the Day class by using this symbol “ : ”.

This class must provide an implementation for the Greet() method because the Day class requires it.

public override void Greet()

The override keyword is used because we are providing our version of the Greet() method in Evening Class.

Console.WriteLine(“Good Evening”);

In this, we print “Good Evening” to the console.

public class Night : Day

Define a class named Night that inherits from the Day class by using this symbol “ : ”.

public override void Greet()

The override keyword is used because we are providing our version of the Greet() method in Night Class.

Console.WriteLine(“Good Night”);

In this, we print “Good Night” to the console.

Const – A constant is a value that remains the same throughout the program and cannot be modified once it has been assigned a value in a variable. Here’s a detailed explanation:

using System;
class Program
  {
      static void Main(string[] args)
       {
     const int NumberOfHoursInADay = 24;
     NumberOfHoursInADay = 26;
     int Age = 20;
     Age = 25;
    Console.WriteLine(“There are ” + NumberOfHoursInADay + ” hours in a day.”);
    Console.WriteLine(“The Age is ” + Age);
     }
}

Explanation

         static void Main(string[] args)
    {
        const int NumberOfHoursInADay = 24;

Declares a constant integer named NumberOfHoursInADay with a value of 24 in Main method.

NumberOfHoursInADay = 26;

The value of this constant cannot be changed.       

int Age = 20;

Declares an integer named Age with a value of 20

Age = 25;

changing an integer named Age with a value of 25

Console.WriteLine(“There are ” + NumberOfHoursInADay + ” hours in a day.”);

Prints “There are 24 hours in a day.”.

Console.WriteLine(“The Age is ” + Age);

Prints “There are 24 hours in a day.”.

In the above code, you will get the below error as you can see in the below image so you should comment on that line and run the code then you will not get any error

Virtual – Use of virtual as a sign that says, “Hey, you can change or customize this thing if you want!” It’s like having a base version of something that can be modified or extended in specific situations.

Override: The override keyword in C# is used to provide a new implementation for a method that is already defined in a base class. This allows a derived class to change or extend the behaviour of a method inherited from the base class.

In the below example, we are using a virtual & override modifier in a method called as Draw, the virtual and override modifiers are used as a combination to modify(change) the method

For example:

using System;
public class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine(“Drawing a shape”);
    }
}
public class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine(“Drawing a circle”);
    }
}

Explanation:

using System;
public class Shape

Defined a class named Shape

public virtual void Draw()

Inside the Shape class, there’s a method called Draw() that is marked as virtual. This means it can be changed or overridden in a class that inherits from Shape.

Console.WriteLine(“Drawing a shape”);

This line inside the Draw() method prints “Drawing a shape” to the console.

public class Circle : Shape

This line defines a new class called Circle that inherits from the Shape class which is a based class.

public override void Draw()

Inside the Circle class, there’s a method also called Draw(), but this one is marked with override. It means we are providing a new version of Draw() specific to circles, which will replace the one in the Shape class

Console.WriteLine(“Drawing a circle”);

prints “Drawing a Circle” to the console.

Sealed: The “sealed” keyword in C# is used to limit a class’s or a member’s ability to inherit from another class. A class or member thereof that has the designation “sealed” cannot be inherited by another class or overridden by a derived class.

For example:

using System;
sealed class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine(“Drawing a shape”);
    }
}
public class Circle : Shape   // This will give an error since the Shape class  is marked as sealed
{
    public override void Draw()
    {
        Console.WriteLine(“Drawing a circle”);
    }
}

Explanation:

using System;
sealed class Shape

We have a Shape class marked as sealed. The sealed keyword means that no other class can inherit from it.

public virtual void Draw()

Inside Shape, there’s a virtual method called Draw(). A virtual method allows derived classes to override it.

Console.WriteLine(“Drawing a shape”);

Print Drawing a shape  message to the console

public class Circle : Shape   // This will give an error since the Shape class  is marked as sealed

we have a Circle class that tries to inherit from Shape. However, this will cause an error because the Shape is sealed.

public override void Draw()
    {
        Console.WriteLine(“Drawing a circle”);

In the above code, you will get the below error as you can see in the below image

Static: The ‘static’ keyword in C# can be used for class, method  & variable. Once we add a static word, we can access class methods and variables in the main method.

For example

using System;
public static class Shape
{
    public static string rectangle = “Rectangle”;
    public static void square()
    {
        Console.WriteLine(“This is a square”);
    }
}
class Execution
{
    public static void Main()
    {
  Console.WriteLine(Shape.rectangle);
        Shape.square();
    }
}

Explanation:

using System;
public static class Shape

public: The class is accessible from any other class. class Shape: The name of the class.

static: The class itself is static, meaning it cannot be instantiated (you cannot create another class that copies the properties of the static class for example you can’t create a new class object called image = static class Shape.

public static string rectangle = “Rectangle”;

This is a static string variable named a rectangle  (also known as a class variable). It holds the value “Rectangle”.

public static void square()

This is a static method (also known as a class method). When called, it prints “This is a square” to the console.

Console.WriteLine(“This is a square”);
    }
}
class Execution
{
    public static void Main()
    {
        Console.WriteLine(Shape.rectangle);

Print the value of the Shape.rectangle (which is “Rectangle”).

Shape.square();

Call the Shape.square() method, which prints “This is a square” to the console.

Operator Description
Abstract Classes and methods
Const Fields/variables (constants)
Virtual & Override Methods and properties*
Sealed Classes and methods
Static Classes, methods, fields, properties, and constructors *

Properties and constructors will be explain in the future topics

3.Access modifier

These access modifier keywords control the visibility and accessibility of classes, methods, properties, and other members in C#.

Public: Accessible from any code in the same assembly or project

For example:

using System;
public class timeofday
{
    public static void Morning()
    {
        Console.WriteLine(“Good Morning”);
    }
    public static void Evening()
    {
        Console.WriteLine(“Good Evening”);
    }
}
class greeting
{
    static void Main()   
{
               timeofday.Morning();  
    }
}

Explanation:

Using system
public class timeofday

defines the class called timeofday

public static void Morning()

defines the function called as morning

Console.WriteLine(“Good Morning”);

defines a writeline function inside a function called as morning

public static void Evening()

defines a new  function called as evening

Console.WriteLine(“Good Evening”);

defines a writeline function inside a function called as evening

class greeting

defined a new class called as greeting

static void Main()

define the main method  to run the code

timeofday.Morning(); 

Define which function out of the 2 functions that is (morning & evening) to run by using the class name because both functions are in other class so we have to access by class name i.e. first-class name than the function name like timeofday.Morning();

 The output of this program should be Good morning. First-class names as time of the day used to save 2 methods inside the class that is morning & evening. Class greet is used to call the main method i.e. we are telling the computer it needs to execute/run something. The method morning inside the main method tells the name of the method that needs to run that is the morning

Private: Accessible only from within the same class 

Example 1:

using System;
public class timeofday
{
    private static void Morning()
    {
        Console.WriteLine(“Good Morning”);
    }
    public void Message()
    {
        Console.WriteLine(“Very Good Morning”);
    }  
    static void Main()
    {
       Morning();
    }
}

Explanation:

using System;

This line includes the System namespace, which contains fundamental classes like Console used for input and output operations.

public class timeofday

This declares a public class named timeofday. Classes are blueprints for creating objects and contain methods and fields.

private static void Morning()

This defines a private static method named Morning. The private modifier means this method can only be called within the timeofday class. The static keyword means this method belongs to the class itself rather than an instance of the class.

Console.WriteLine(“Good Morning”);

This line prints “Good Morning” to the console.

public void Message()

This defines a public instance method named Message. The public modifier means this method can be called from outside the timeofday class. This method does not use the static keyword, so it must be called on an instance of the timeofday class.

Console.WriteLine(“Very Good Morning”);

This line prints “Very Good Morning” to the console.

static void Main()

This defines the Main method, which is the entry point of the program. Execution begins from here. The static keyword means this method can be called without creating an instance of the timeofday class.

Morning();

This line calls the Morning method. Since Morning is a static method and Main is also static, we can call Morning directly without creating an instance of the timeofday class.

Private: Cannot be Accessible across the class.

Example 2:

using System;
public class timeofday
{
    private static void Morning()
    {
        Console.WriteLine(“Good Morning”);
    }
    public void Message()
    {
        Console.WriteLine(“Very Good Morning”);
    }
}
class program
{
    static void Main()
    {
        timeofday day = new timeofday();
        day.Morning();//day.Morning()’ is inaccessible due to its protection level becuase it it private 
   }
}

Explanation:

using System;

This line includes the System namespace, which contains fundamental classes like Console used for input and output operations. 

public class timeofday

This declares a public class named timeofday. Classes are blueprints for creating objects and contain methods and fields.

private static void Morning

This defines a private static method named Morning. The private modifier means this method can only be called within the timeofday class. The static keyword means this method belongs to the class itself rather than an instance of the class.

Console.WriteLine(“Good Morning”);

This line prints “Good Morning” to the console.

public void Message()

This defines a public instance method named Message. The public modifier means this method can be called from outside the timeofday class. This method does not use the static keyword, so it must be called on an instance of the timeofday class.

Console.WriteLine(“Very Good Morning”);

This line prints “Very Good Morning” to the console.

class program

This declares a public class named program, which contains the Main method, the entry point of the application.

static void Main()

This defines the Main method, which is the entry point of the program. Execution begins from here. The static keyword means this method can be called without creating an instance of the program class.

timeofday day = new timeofday();

This creates an instance of the timeofday class and assigns it to the variable day.

day.Morning();

This line attempts to call the Morning method on the instance day, but it will result in a compile-time error because Morning is a private method and cannot be accessed from outside the timeofday class.

You can see the error in the below image

Private:  We cannot access in derived class

Example 3:

using System;
public class timeofday
{
    private static void Morning()
    {
        Console.WriteLine(“Good Morning”);
    }
}
public class timeofnight : timeofday
{
   public void Message()
    {
        timeofday.Morning();
    }
}

As you can see the error in the below image

Protected: Accessible only from within the same class or derived classes. Protected modifier is rarely used and you can learn it once you have buildup expertise in C# coding structure hence we will not explain in detail

Course Video