-3

If we have written a class called MyClass and need an object of it's type, we would go

var myVar = new MyClass();

and also if our MyClass has an IEnumerable interface we again need the new keyword such as:

class myClass : IEnumerable {...}
var myVar = new MyClass();

what about a situation when our class, MyClass is not used as one but as a type to instantiate a new variable of say:

IEnumerable<MyClass> myVar;

I would say we should clearly get an error if we go

IEnumerable<MyClass> myVar = new IEnumerable<MyClass>(); 

but trying to understand the logic behind this. How this enumeration set of objects get created in this situation?

DavidG
  • 113,891
  • 12
  • 217
  • 223
Mehrad
  • 4,093
  • 4
  • 43
  • 61
  • C# *and* C++? Why not throw in Java and PHP to complete the merry round of languages we should guess? – Kerrek SB Jun 02 '14 at 23:54
  • possible duplicate of [Learning to use Interfaces effectively](http://stackoverflow.com/questions/8701345/learning-to-use-interfaces-effectively) – DavidG Jun 02 '14 at 23:59
  • @all, despite reading your recommended duplicated before and after I published the question, I still learned from the answer given for this question. My best regards for the people who value knowledge over curiosity but never given a though about which caused the other. – Mehrad Jun 03 '14 at 00:17
  • Please vote up for this comment, in the favor of me taking this question down if you think it will be no use for the future reader. I learned what I needed to and don't mind deleting it. – Mehrad Jun 03 '14 at 00:20

2 Answers2

4

There is a difference between declaring a variable and instantiating an object.

IEnumerable<MyClass> myVar; 

Just declares a variable. No instantiation has occurred. If you were to hover over this variable with the debugger, it would be null. This line is also valid:

MyClass myVar;

You still need to instantiate IEnumerable with a derived type to use it:

IEnumerable<MyClass> myVar = new List<MyClass>(); 

Note that your line would not compile, because you cannot instantiate an interface or abstract class.

IEnumerable<MyClass> myVar = new IEnumerable<MyClass>();  //Does not compile!

You also seem to be confused about the MyClass in IEnumerable<MyClass>. This is just a generic type parameter, again, nothing is instantiated.

To speak to return values, the following code is totally valid:

IEnumerable<MyClass> myVar = someFunction();
myVar = new List<MyClass>();

However, someFunction would need to instantiate (or generate via LINQ) a class that derives from IEnumerable, or else this would just assign null again. I'm not sure what you mean by "instantiated and allocated by the value in one step", so its hard to say :). Functions just modularize code, there isn't any magic. For it to return a valid value, it has to have one in the first place or otherwise make one itself.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • can you allocate a return value of a method which is of a type `IEnumerable` to `myVar` variable before you perform this line `IEnumerable myVar = new List();`? would it instantiated AND been allocated with the value in one step? – Mehrad Jun 03 '14 at 00:14
  • 1
    @Mehrad do you know what is an interface ? let's assume you instantiate the interface, what would you have ? methods without implementation ? interfaces don't provide any implementation, they are just contracts, classes implement interfaces and you instantiate them. – Selman Genç Jun 03 '14 at 00:18
  • 1
    @Mehrad, not sure what your second question was about but yes you can assign it to a return value. See my edit for more details. Selmans comment about interfaces is spot on, if you have questions about the nature of an interface, please ask! – BradleyDotNET Jun 03 '14 at 00:34
2

IEnumerable is an interface, you can't instantiate it. You'd have to use a concrete class like List.

Jeff
  • 2,701
  • 2
  • 22
  • 35