Final Keyword in C# – sealed with const and readonly

In java there is keyword “final“, which is used to avoid overloading / inheritance of method / class respectively.

In c# there is no keyword like “final” but the same thing is achieved by keyword “sealed“.

A class which is marked by keyword sealed cannot be inherited.

If you have ever noticed, structs are sealed. You cannot derive a class from a struct.

Example:

namespace OOPS_Concept
{
    sealed class SealedClassDemo
    {
        public void test1()
        {
            Console.WriteLine("Method Test1");
        }
    }

    class childclass : SealedClassDemo
    {
    }
}

Above code will generate an error saying that class cannot be inherited.

Sealed Method:

In C#, Methods cannot be “sealed” directly. Methods of only derived class can be made sealed with keyword sealed and override.

namespace OOPS_Concept
{
    class SealedMethodDemo
    {
        public virtual void Method1()
        {
            Console.Write("Base class Method1");
        }
    }

    class ChildClass : SealedMethodDemo
    {
        public sealed override void Method1()
        {
            Console.Write("Derived class Method1");
        }
    }
}

One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.


readonly:

The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

refer this MSDN document to read more on keyword “readonly” in C#


const:

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified. A constant declaration introduces one or more constants of a given type. The declaration takes the form:

public const double x = 1.0, y = 2.0, z = 3.0;

Note : The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants, as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;

MSDN article on const keyword

Posted

in

by

Tags:


Related Posts

Comments

One response to “Final Keyword in C# – sealed with const and readonly”

  1. Lisa Edward Avatar

    This is definitely one of the best articles I have read in
    this website! Thanks Mate

    Mosquito Net in Chennai

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading