C# - Object Creation

Mario Legenda

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?

nozzleman

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

    [...]

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Object Creation & Lists

From Dev

dynamic object creation in c++?

From Dev

Thread creation using a function of an object in C++

From Dev

Object creation in C# inheritance hierarchy

From Dev

c++ getting error for object creation expected ;

From Dev

Thread creation using a function of an object in C++

From Dev

C++ object creation without constructor call

From Dev

C++ vector of object creation error

From Dev

In C++ are constructors called before or after object creation?

From Dev

C++: Separating object's creation from use (for testing purposes)

From Dev

Dynamic List Creation based on given object type in C#

From Dev

Difference in COM object creation in F# and C#

From Dev

What does asterisk before brackets on object creation mean in C++?

From Dev

Operator overloading in Fortran with no object creation such as in friend functions (C++)

From Dev

Difference in COM object creation in F# and C#

From Dev

Dynamic Object creation in a Form from a datatable c#

From Dev

Understanding effective object creation

From Dev

order of object creation, cpp

From Dev

Global object and creation order

From Dev

Java object creation and referencing

From Dev

Python Method object creation

From Dev

Object creation optimization

From Dev

Tracking Object Creation in Android

From Dev

PHP Object creation Looping

From Dev

Peculiar way for object creation?

From Dev

Wrapper Object Explicit Creation

From Dev

Expect the creation of a new object

From Dev

Swift: Dynamic Object Creation

From Dev

Unspecified Implicit Object Creation