IList<T> _list = new List<T>() What is _list??

G

GiJeet

IList<T> _list = new List<T>()

I'm confused as to what _list is...a List or an IList ?

The variable is IList but the new creates a List.

TIA
G
 
N

Nicholas Paldino [.NET/C# MVP]

GiJeet,

Well, the _list is a variable of type IList<T>, and will always be an
IList<T>. That reference, however, ultimately points to a List<T>. If you
want to work with the List<T>, then you will have to cast that _list
reference to another variable of type List<T>.
 
G

GiJeet

    Well, the _list is a variable of type IList<T>, and will always be an
IList<T>.  That reference, however, ultimately points to a List<T>.  If you
want to work with the List<T>, then you will have to cast that _list
reference to another variable of type List<T>.

is the only reason we have to use a List<T> reference is because we
can't create an instance of IList<T> ?
IOWs, if we could create an instance of IList<T> there would be no
need to reference List<T>, is this correct?
G
 
N

Nicholas Paldino [.NET/C# MVP]

GiJeet,

Well, you can't create an IList<T> since that's an interface, and you
can't create instances of interfaces, only of concrete (as opposed to
abstract) classes.

You don't HAVE to assign a List<T> to _list, you can assign ANYTHING
that implements IList<T>. Of course, List<T> is the most common
implementation, but you don't have to use that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GiJeet said:
On Dec 3, 11:25 am, "Nicholas Paldino [.NET/C# MVP]"
Well, the _list is a variable of type IList<T>, and will always be an
IList<T>. That reference, however, ultimately points to a List<T>. If you
want to work with the List<T>, then you will have to cast that _list
reference to another variable of type List<T>.

is the only reason we have to use a List<T> reference is because we
can't create an instance of IList<T> ?
IOWs, if we could create an instance of IList<T> there would be no
need to reference List<T>, is this correct?
G
 
G

GiJeet

To the OP: if you're unsure as to what an interface is, you should try to 
read up on them until you understand them.  The short answer is that an 
interface is a "contract"; that is, a way of declaring a specific API that  
must be implemented by a class.  When you're using an interface, you can  
use an instance of any class that implements that API, which allows for  
specific functionality to be implemented by a broad variety of classes,  
without being constrained by the single-inheritance rules of C# (that is, 
otherwise all classes implementing the desired API would have to derive  
 from a single common class).

Pete

Thanks Pete but I'm familiar with interfaces, just a little confused
as to the references assigned to interface types
like IList<T> _list = new List<T>();

I create some examples to see the differences:

IList<T> _list = new List<T>();
_list. intellisense shows a limited number of methods and properties

List<T> _list2 = new List<T>();
_list2. intellisense displays many more methods and properties now
that the type is List

IList _list3 = new ArrayList();
huh?? why can we reference ArrayList ?so I guess ArrayList implements IList ??
 
J

Jeroen Mostert

GiJeet said:
[...] I'm familiar with interfaces, just a little confused
as to the references assigned to interface types
like IList<T> _list = new List<T>();

I create some examples to see the differences:

IList<T> _list = new List<T>();
_list. intellisense shows a limited number of methods and properties

List<T> _list2 = new List<T>();
_list2. intellisense displays many more methods and properties now
that the type is List
You can't call the methods and properties of List<T> through a variable of
type IList<T>, just like you could only access the members of Object if you
assigned it to a variable of type Object.

Intellisense doesn't "know" the object the variable refers to is actually a
List<T>, and even if it did (it can't know in general, since the value is
determined at runtime) it would be of no assistance to you to see all the
members you can't use anyway.
IList _list3 = new ArrayList();
huh?? why can we reference ArrayList ?
so I guess ArrayList implements IList ??
Don't be lazy. There is such a thing as documentation.

http://msdn.microsoft.com/library/system.collections.arraylist
 
P

proxyuser

GiJeet said:
IList<T> _list = new List<T>()

I'm confused as to what _list is...a List or an IList ?

The variable is IList but the new creates a List.

It is both, depending on your point of view, and what you actually mean when
you refer to _list.

Technically, it is of IList<T> type. But you can't actually instantiate
such a thing. The actual object is of type List<T>. You declared something
that is just a reference. The thing it refers to is of type List<T>. So
when you say _list, do you mean the reference variable, or the actual list?
 
P

proxyuser

Thanks Pete but I'm familiar with interfaces, just a little confused
as to the references assigned to interface types
like IList<T> _list = new List<T>();

I create some examples to see the differences:

IList<T> _list = new List<T>();
_list. intellisense shows a limited number of methods and properties

List<T> _list2 = new List<T>();
_list2. intellisense displays many more methods and properties now
that the type is List

IList _list3 = new ArrayList();
huh?? why can we reference ArrayList ?
so I guess ArrayList implements IList ??

I think you'd be better off thinking of IList as an abstract base class.
Then the basic OO ideas related to polymorphism make more sense.

If class B inherits from class A, then you can write

A a = new B();

But you can only call the methods on "a" when you're using "a:". If you
cast it to a B, then you can call all of B's methods on "a" also.

And keep in mind that IList<T> isn't exactly the same thing as IList.
 
G

GiJeet

A a = new B();

But you can only call the methods on "a" when you're using "a:".  If you
cast it to a B, then you can call all of B's methods on "a" also.

And keep in mind that IList<T> isn't exactly the same thing as IList.- Hide quoted text -

let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?

What is bird?

Chicken bird = new Eagle();


G
 
H

Hans Kesting

After serious thinking GiJeet wrote :
let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?

What is bird?

Chicken bird = new Eagle();


G

It probably will not compile, as there is no relation between those
classes (I guess).
However, if both Chicken and Eagle inherit from a baseclass Bird (that
is probably abstract), then the following would be legal:

Bird b1 = new Eagle();
Bird b2 = new Chicken();

Now "b1" is in reality an Eagle, but for this code I am only interested
in the "Bird" aspects of it.

So if Eagle has a method GrabPrey() that is not available in Bird, then
I couldn't do "b1.GrabPrey()".
When I know that b1 really *is* an Eagle, then I could tell the
compiler by casting: ((Eagle)b1).GrabPrey().

When I try ((Eagle)b2).GrabPrey() I would get a cast error, as I can't
convert a Chicken into an Eagle.

A Fly() method probably *is* defined by Bird, which means that b1.Fly()
would be legal, as would b2.Fly() (although it would fly slower :) )


Hans Kesting
 
G

Göran Andersson

GiJeet said:
let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?

What is bird?

Chicken bird = new Eagle();


G

A chicken is hardly and eagle, or vice versa...

This makes more sense:

interface ICanFly {}
interface ICanHunt {}

class Bird : ICanFly {}

class Chicken : Bird {}
class Eagle : Bird, ICanHunt {}

Now you can create an eagle object and store the reference in different
kinds of reference variables:

ICanFly eagleFly = new Eagle();
ICanHunt eagleHunt = new Eagle();
Bird eagleBird = new Eagle();
Bird chickenBird = new Chicken();
Chicken chicken = new Chicken();
Eagle eagle = new Eagle();

The eagleFly reference can be used for anything that the ICanFly
interface defines.
The eagleHunt reference can be used for anything that the ICanHunt
interface defines.
The eagleBird reference can be used for anything that the Bird class
defines.
The chickenBird reference can be used for anything that the Bird class
defines.
The chicken reference can be used for anything that the Chicken class
defines.
The eagle reference can be used for anything that the Eagle class defines.

Casting the reference (Eagle)eagleFly means that you can use it for
anything that the Eagle class defines.
Casting the reference (ICanFly)eagle means that you can only use it for
what the ICanFly interface defines.

You can cast any of the references eagleFly, eagleHunt, eagleBird or
eagle to the type ICanHunt, but not the references chickenBird or chicken.
 
A

Anthony Jones

GiJeet said:
A a = new B();

But you can only call the methods on "a" when you're using "a:". If you
cast it to a B, then you can call all of B's methods on "a" also.
let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?

What is bird?

Chicken bird = new Eagle();

<<<<<<<<<<

You can't an Eagle is not a Chicken and this assignment will fail.

However if you had:-

class Bird { void Fly() { } }
class Chicken : Bird { void Cluck() { } }
class Eagle : Bird { void Hunt() { } }

Bird bird1 = new Eagle();
Bird bird2 = new Chicken();

You can ask both bird1 and bird2 to Fly but you can ask neither bird1 nor
bird2 to Hunt or Cluck.

You can now to this:-

Eagle bird3 = (Eagle)bird1;

Now you can ask bird3 to both Fly and Hunt but it won't Cluck.

Chicken bird4 = (Chicken)bird2;

Now you can ask bird4 to both Fly and Click but it won't Hunt.

You cannot do these:-

Eagle bird5 = bird4; // bird4 is a Chicken not an Eagle
Chicken bird6 = bird3; // bird3 is an Eagle not a chicken.

You can do this:-

bird1 = bird2; // replace reference to bird in bird1 with the reference in
bird2 both are Birds.




bird3.Hunt() // Eagle eats Chicken. ;)
 
M

Mythran

GiJeet said:
Excellent. Now I can see what’s happening. This really cleared up
much confusion.

Thanks!
G

What I want to know is why is it so hard to explain Interfaces to someone
who doesn't know them? I understand them...and can picture how and when to
use interfaces, but I can't explain them to someone who doesn't know. I
explain them word-for-word how I understand them, and people just don't
grasp it. I don't remember what it finally took for me to understand...and
they are actually really really simple to use and understand...but why is it
so difficult to explain it in a way for others that don't know them can
grasp them the first time it is explained?

<shrug> I just don't know and honestly am curious for an answer...

Thanks
Mythran
 
P

proxyuser

let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?
What is bird?
Chicken bird = new Eagle();

That is a poor example. The class should be Bird, not the variable name.

class Bird

class Chicken : Bird

class Eagle : Bird

Bird chicken = new Chicken();
Bird eagle = new Eagle();
f(chicken);
f(eagle);

void f(Bird bird)
{
}
 
P

proxyuser

Mythran said:
What I want to know is why is it so hard to explain Interfaces to someone
who doesn't know them? I understand them...and can picture how and when
to use interfaces, but I can't explain them to someone who doesn't know.
I explain them word-for-word how I understand them, and people just don't
grasp it.

There are 2 kinds of people you'd talk to
a) people who already know about inheritance
b) people who don't

For a), they probably don't get it because "interface" is too different from
what they understand. Just tell them an interface is an abstract base class
with no member variables.

For b), the probably don't get it because they don't get inheritance to
begin with.
 
P

proxyuser

Peter Duniho said:
But in this case, the person started out by claiming to be "familiar with
interfaces". By claiming knowledge he doesn't have, it leads to a
discussion that is at the wrong level.

The leap from understanding the generic term "interface" in computer
programming, and "interface" as implemented in C#, is a pretty big one.

The computer field in general does not do any favors by overloading these
terms. Think of "static" from C++, for example. Another I was just
recently reminded of is the Finalize method in C# and how you override it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top