1

I have 2 classes B and D. I have to create an array with 4 elements 2 of type B and 2 of type D. How do I do it?

B o1 = new B();
D o2 = new D();
B o3 = new B();
D o4 = new D();

The array should be something like this:

array[0] = o1; array[1] = o2; array[2] = o3; array[3] = o4;
5
  • I'm curious - what do you need this for? Commented Jun 28, 2013 at 8:41
  • I need to study for an exam. And couldn't find the answer on google. :) Commented Jun 28, 2013 at 8:45
  • Answer would be: makes only sense, if B and D inherit from A (class or interface), then create array of A. Although B and D inherit from Object, Object has to few properties to be useful here. Commented Jun 28, 2013 at 8:51
  • 1
    I am new to c#. I don't like it but I have to learn it. In PHP it's 10000 times easier; I am new to PHP. I don't like it. C# is 10000 times easier ;) Commented Jun 28, 2013 at 8:56
  • 1
    @Nolonar :))) Every man with his opinion. Commented Jun 28, 2013 at 8:58

3 Answers 3

14

If there is no common base class other than object, you just need:

object[] array = new object[4];
array[0] = o1;
// etc

Or in a single shot:

object[] array = { o1, o2, o3, o4 };

To use the members specific to B or D, you'd need to cast when you retrieved the values from the array, e.g.

B b = (B) array[0];
b.SomeMethodDeclaredOnB();

If B and D have common methods, you could declare those in an interface which both classes implemented, and change the type of the array:

IBD[] array = new IBD[4];
array[0] = o1;
...
array[0].SomeMethodDeclaredInIBD();

Or:

IBD[] array = { o1, o2, o3, o4 };
...
array[0].SomeMethodDeclaredInIBD();

Finally, to address this:

I am new to c#. I don't like it but I have to learn it. In PHP it's 10000 times easier;

I'm sure if I tried to use PHP I'd find the same experience in the exact opposite direction. Don't assume that C# is "worse" or "harder" than PHP - it's just different, and you're bound to find it harder to use a language you're not familiar with than your "comfort zone" language.

Sign up to request clarification or add additional context in comments.

11 Comments

In PHP it's like this: $array = array($o1, $o2, $o3, $o4); Much simpler. After that I can do this: $array = "here is a string". And The variable array changed it's type. Simple. Elegant. Easy to find on the web :)
@PaulMoldovan: And easy to make mistakes with. Don't underestimate the power of static typing. There's a simpler way to declare and populate the array in one go in C# as well - I'll edit that in.
Maybe you're right. But after i pass this exam I'll go back to my normal life. A PHP developer. :P
@PaulMoldovan: I think it's a pity if you don't use the opportunity you're being given to expand your horizons. It's always useful to see a different perspective on development. But if this is really just a means to an end as far as you're concerned, that's your call. I just think you're missing out.
@PaulMoldovan: I suppose if you heavily abused the dynamic keyword, you could pretend that C# was a dynamically typed language. However, this will have heavy performance penalties and will make debugging your program much more difficult. Also, other C# developers will come after you with torches and pitchforks.
|
4
  • or use array object[] array of objects

  • or more OOP approch:

    public class B : IHolder { }

    public class D : IHolder { }

IHolder[] arrays of IHolders

Comments

1

I am new to c#. I don't like it but I have to learn it. In PHP it's 10000 times easier;

You left out a part of the last sentence: "to make mistakes". Strong-typedness has its strengths.

You can let the classes B and D inherit from a common base class or interface like such:

interface ISomeInterface
{
    string CommonProperty { get; }
}

class B : ISomeInterface
{
    public string CommonProperty { get; }
}

class D : ISomeInterface
{
    public string CommonProperty { get; }
}

ISomeInterface[] array = new ISomeInterface[]
{
    o1,
    o2,
    o3,
    o4
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.