Marker Interfaces in C#

Introduction

Marker interfaces, also known as tagging interfaces, are interfaces that do not contain any method or property declarations. They are used to mark or tag a class so that the program can perform some specific operation based on the presence or absence of the marker interface.

In this blog post, we will explore the concept of marker interfaces in C# with a real-world example.

Example

Let’s consider a real-world scenario where we need to store some data into a database. We have different types of data, such as text, images, videos, and so on. We need to develop a database storage system that can store different types of data.

One way to implement this is to create a base class named Data that contains all the common properties and methods required for storing data. Then, we can create derived classes for each type of data, such as TextData, ImageData, and VideoData.

public abstract class Data
{
    public string Name
    {
        get;
        set;
    }
    public string Path
    {
        get;
        set;
    }
    public DateTime CreatedDate
    {
        get;
        set;
    }
    public abstract void Save();
}
public class TextData: Data
{
    public string Content
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save text data into the database
    }
}
public class ImageData: Data
{
    public byte[] Content
    {
        get;
        set;
    }
    public int Width
    {
        get;
        set;
    }
    public int Height
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save image data into the database
    }
}
public class VideoData: Data
{
    public byte[] Content
    {
        get;
        set;
    }
    public int Length
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save video data into the database
    }
}

Now, let’s say we want to add a feature that allows us to archive data. We want to mark certain types of data as “archivable” and only archive those types. One way to do this is to add a marker interface named IArchivable and implement it in the derived classes for which we want to provide archiving functionality.


public interface IArchivable
{}
public class TextData: Data, IArchivable
{
    public string Content
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save text data into the database
    }
}


public class ImageData: Data
{
    public byte[] Content
    {
        get;
        set;
    }
    public int Width
    {
        get;
        set;
    }
    public int Height
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save image data into the database
    }
}

public class VideoData: Data, IArchivable
{
    public byte[] Content
    {
        get;
        set;
    }
    public int Length
    {
        get;
        set;
    }
    public override void Save()
    {
        // Code to save video data into the database
    }
}

Now, we can modify our archiving code to check for the presence of the IArchivable marker interface and only archive those types of data.

public class Archiver
{
    public void Archive(Data data)
    {
        if (data is IArchivable archivable)
        {
            // Code to archive the data
        }
        else
        {
            // Code to log a message that the data cannot be archived
        }
    }
}

Conclusion

Marker interfaces are a powerful feature of C# that allow us to mark or tag a class so

that the program can perform some specific operation based on the presence or absence of the marker

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post