I'm a PHP programmer for some time. Two days ago, I went to a job interview where they gave me an assigment to do in ASP.NET ( C# ). I would really like to get out of the php sphere of influence and learn a decent language that can challenge me. So, I have a question
Do all instances have to be instantiated at runtime? In php, I can do something like this...
class SomeObject {}
$objString = "SomeObject";
$objInstance = new $objString();
I can't do that in C#, probably beacuse it's a compiled language. In C#, would I have to create a Factory pattern who will instantiate objects. That would also mean that if I have to instantiate 10 object in that Factory, that there would be 10 if
statements which is ugly.
I found Activator object with its Activator::createInstance()
method but I could't get it to work. Also there's Reflection, but both of these ( as I'm aware of ) are a performance impact.
So, is there a way to dynamicly create objects or could it be that in C#, i can immediatlly create all objects that my program will use, which is really tempting?
EDIT
Ok, so let's say that I have 5 object that are used in 5 different occassions. I run the program, the program evaluates that it needs one of those object and instantiates it. The other four are never instantiated. I close the program.
Second time, I run the program with different parameters, and 2 of those 5 objects are created, other three never came into existence.
This is easy in PHP. Let's put Activator and other tools aside, is it good practice in C# world to create all the 5 objects when I know that maybe, only one of them will be used?
I don't know if i got your question wrong, but creating object of your given class SomeObject
in C# is as easy as the following:
var obj = new SomeObject();
Using the Activator
-Class it should look sth. like one of the following
var obj2 = Activator.CreateInstance(typeof(SomeObject));
var obj3 = Activator.CreateInstance<SomeObject>();`
var someObjType = Type.GetType("SomeObject"); // if you have to use a string
var obj4 = Activator.CreateInstance(someObjType);
Note that using the Activator
-Class for instanciating objects isn't necessary in most cases. The first example is the standard way if you know the Type
of the Class at compiletime.
UPDATE
regarding your update, since i don't know the details what comes to my mind is lazy instanciation. Since everything, including the entry point of your application is an object in C#, you can solve the issue using properies with backed fields like in the following example
class Program
{
// backing fields
private SomeObject obj1;
private SomeObject obj2;
private SomeObject obj3;
private SomeObject obj4;
private SomeObject obj5;
// this way, obj1 only gets instanciated when needed
public SomeObject Obj1
{
get
{
if (obj1 == null)
{
obj1 = new SomeObject();
}
return obj1;
}
}
// same for the other objects
[...]
}
I you are concerned about the memory usage of your object, i recommend you to learn about how to properly implement IDisposable
UPDATE 2
To provide the possibility recommended in the comments by @ Mad Sorcerer, you could back the fields using the Lazy
-Class which takes some effort of your shoulder, the effect is quite the same as in the previous update.
class Program
{
// Lazy backing fields
private Lazy<SomeObject> obj1 = new Lazy<SomeObject>();
private Lazy<SomeObject> obj2 = new Lazy<SomeObject>();
private Lazy<SomeObject> obj3 = new Lazy<SomeObject>();
private Lazy<SomeObject> obj4 = new Lazy<SomeObject>();
private Lazy<SomeObject> obj5 = new Lazy<SomeObject>();
// this way, obj1 only gets instanciated when needed
public SomeObject Obj1
{
get { return obj1.Value; }
}
// same for the other objects
[...]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With