Skip to main content

Static vs Singleton in C#

In C#, a static class is a class that cannot be instantiated and all its members must also be static. A static class is used to contain methods that are not associated with a particular object, but rather with the class itself.

A singleton is a design pattern that ensures that a class has only one instance and provides a global access point to it. Singletons are implemented by creating a class with a private constructor and a static method that returns a reference to the only instance of the class.

Here is an example of a static class in C#:

public static class MyMath
{
    public static int Add(int x, int y)
    {
        return x + y;
    }

    public static int Subtract(int x, int y)
    {
        return x - y;
    }
}


To use the methods in the MyMath class, you would simply call them using the class name:

int result = MyMath.Add(1, 2);

Here is an example of a singleton in C#:

public class MySingleton
{
    private static MySingleton instance;
    private MySingleton() {}

    public static MySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new MySingleton();
            }
            return instance;
        }
    }

    public void DoSomething()
    {
        // Implementation goes here
    }
}

To use the MySingleton class, you would call the Instance property to get a reference to the only instance of the class, and then call the method on that instance:

MySingleton.Instance.DoSomething();


It's important to note that a singleton can also be implemented using a static class and static methods, like this:

public static class MySingleton
{
    private static readonly MySingleton instance = new MySingleton();

    private MySingleton() {}

    public static MySingleton Instance
    {
        get
        {
            return instance;
        }
    }

    public void DoSomething()
    {
        // Implementation goes here
    }
}

In this case, the MySingleton class is a static class, but it has a private instance of itself and provides a static Instance property that returns a reference to that instance.

Comments

Popular posts from this blog

Prompt Engineering Fundamentals

  Introduction Generative AI is a transformative technology capable of producing text, images, audio, and code in response to user prompts. This capability is powered by Large Language Models (LLMs) like OpenAI's GPT series, which are trained to understand and generate natural language. Interacting with these models via prompts allows users to harness their potential without needing technical expertise. This chapter explores the essentials of prompt engineering, a field dedicated to optimizing prompt design for consistent and high-quality responses. Learning Goals By the end of this lesson, you will be able to: Explain what prompt engineering is and why it matters. Describe the components of a prompt and how they are used. Learn best practices and techniques for prompt engineering. Apply learned techniques to real examples using an OpenAI endpoint. Learning Sandbox Prompt engineering is more art than science, requiring practice and iterative refinement. This lesson includes a Jupyt...

AWS Cloud Containers

Amazon Web Services (AWS) offers a variety of services for deploying and managing applications in the cloud. One of these services is called Amazon Elastic Container Service (ECS), which allows you to run and manage Docker containers on AWS. Here is a brief overview of how Amazon ECS works: You package your application into a Docker container image and push it to a registry, such as Amazon Elastic Container Registry (ECR) or Docker Hub. You create an Amazon ECS task definition, which is a blueprint for your containerized application. The task definition specifies things like the Docker image to use, the CPU and memory requirements, and the environment variables to pass to the container. You create an Amazon ECS cluster, which is a group of Amazon EC2 instances that are running the Amazon ECS container agent. The cluster is where your tasks are placed and run. You create an Amazon ECS service, which is a long-running task that is hosted on your cluster. The service ensures that a speci...

Identified COVID-19 in X-ray images with deep learning.

  Project structure :       Our coronavirus (COVID-19) chest X-ray data is in the dataset/ directory where our two classes of data are separated into covid/ and normal/   I have created train_covid19.py file to train the model.   Three command line arguments (parameters) required to run this file :   --dataset: The path to our input dataset of chest X-ray images. --plot: An optional path to an output training history plot. By default the plot is named plot.png unless otherwise specified via the command line. --model: The optional path to our output COVID-19 model; by default it will be named covid19.model.     To load our data, we grab all paths to images in in the --dataset directory. Then, for each imagePath, we:   ·           Extract the class label (either covid or normal) from the path. ·           Load the image, and preprocess it by convertin...