Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Adding objects that implement interfaces to a dictionary

I have a dictionary:

private Dictionary<Type, IExample> examples;

I have two classes that implement the interface:

public class Example1 : IExample
{
}

public class Example2 : IExample
{
}

I have created a way to get an instance from the dictionary if it exists but am trying to figure out a way to instantiate a new object if it doesn't exist.

public T GetExample<T>() where T : IExample
{
    // Return the signal if it exists
    if (examples.ContainsKey(typeof(T)))
    {
        IExample value;

        if (!examples.TryGetValue(typeof(T), out value))
        {
            // unable to get value
        }

        return (T)value;
    }

    // Stuck on this line here. How exactly do I instantiate a new example if it doesn't exist.
    examples.Add(typeof(T), new );

    return default(T);
}

Is such a thing possible?

like image 662
user923 Avatar asked Apr 20 '17 08:04

user923


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You would need to add a generic type parameter constraint on your generic method for parameter-less constructor and then you can instantiate the type T parameter like new T():

public T GetExample<T>() where T : IExample,class,new()
{
       IExample value;

       if (examples.TryGetValue(typeof(T), out value))
       {
           return (T)value;
       }


   T obj =  new T(); // create instance of T and use further down in code it's reference
   examples.Add(typeof(T),obj );

   return obj ;
}

and return default(T); would return null not a new instance of T as for class (Reference Types) default value is null, i doubt you want to do return new T(); there, which will create a new object and will return the reference to it back to caller.

like image 50
Ehsan Sajjad Avatar answered Sep 30 '22 00:09

Ehsan Sajjad