more correct with allocation:
1. int[] x = {1,2,3}
2. IEnumerable i = x
how does it calls? (the 2 line)
Is it an implicit creation of a class that implemens IEnum?
6 replies to this topic
#1
Posted 26 August 2011 - 10:34 AM
|
|
|
#2
Posted 26 August 2011 - 01:09 PM
x is an array. All arrays implement IEnumerable so you can always assign an array to an IEnumerable object. So i is just a reference to x.
Try this:
Try this:
[COLOR=blue]int[/COLOR][] x = {1, 2, 3};
[COLOR=#2b91af]IEnumerable[/COLOR] i = x;
[COLOR=blue]foreach[/COLOR] ([COLOR=blue]var[/COLOR] v [COLOR=blue]in[/COLOR] x)
[COLOR=#2b91af]Console[/COLOR].WriteLine(v);
x[0]++;
[COLOR=#2b91af]Console[/COLOR].WriteLine([COLOR=#a31515]"Contents of i:"[/COLOR]);
[COLOR=blue]foreach[/COLOR] ([COLOR=blue]var[/COLOR] v [COLOR=blue]in[/COLOR] i)
[COLOR=#2b91af]Console[/COLOR].WriteLine(v);
#3
Posted 26 August 2011 - 10:45 PM
hm...so "i" just keep reference?
does interfaces inferits from system.object to keep references?
does interfaces inferits from system.object to keep references?
#4
Posted 29 August 2011 - 06:15 AM
Basically i and x are to variables that point to the same location in memory.
Like this diagram:
reference.png 5.02K
20 downloads
[ATTACH=CONFIG]4211[/ATTACH]
The arrows mean that x is a reference to the array and when you do i = x you are making i hold the same reference as x which is why i also points to the array. This is why changing one changes both.
Don't know what you mean by this?
Like this diagram:
reference.png 5.02K
20 downloads[ATTACH=CONFIG]4211[/ATTACH]
The arrows mean that x is a reference to the array and when you do i = x you are making i hold the same reference as x which is why i also points to the array. This is why changing one changes both.
Quote
does interfaces inferits from system.object to keep references?
Don't know what you mean by this?
Edited by chili5, 30 August 2011 - 06:51 AM.
#5
Posted 29 August 2011 - 10:38 AM
explanation of reference type in .NET is priceless :) Many will be grateful to you.
Actually, what I really didn't know, that interfaces can keep references. In fact any of interfaces, that class implements can hold the reference to the object.
So i can do this:
and even this:
it's pretty cool... I thought that interfaces is something very abstract.
Actually, what I really didn't know, that interfaces can keep references. In fact any of interfaces, that class implements can hold the reference to the object.
So i can do this:
int[] x = new int[5]{ 1, 2, 3, 4, 5 };
IEnumerable ie = x;
ICollection ic = x;
IList il = x;
IStructuralComparable isc = x;
IStructuralEquatable ise = x;
and even this:
ie = isc as IEnumerable;can't do this:
foreach (int v in isc) Console.WriteLine(v);but can do this:
foreach (int v in (IEnumerable)isc) Console.WriteLine(v);
it's pretty cool... I thought that interfaces is something very abstract.
#6
Posted 30 August 2011 - 06:47 AM
[COLOR=blue]int[/COLOR][] x = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][5] { 1, 2, 3, 4, 5 };
[COLOR=#2b91af]IEnumerable[/COLOR] ie = x;
[COLOR=#2b91af]ICollection[/COLOR] ic = x;
[COLOR=#2b91af]IList[/COLOR] il = x;
[COLOR=#2b91af]IStructuralComparable[/COLOR] isc = x;
[COLOR=#2b91af]IStructuralEquatable[/COLOR] ise = x;
Those all work because Array implements all those interfaces. If an object implements a certain interface then you can declare an object of that type to be the same type as the interface.
Like this:
[COLOR=blue]class[/COLOR] [COLOR=#2b91af]Person[/COLOR] : [COLOR=#2b91af]IComparable[/COLOR], [COLOR=#2b91af]ICloneable[/COLOR] {
[COLOR=blue]public[/COLOR] [COLOR=blue]int[/COLOR] CompareTo([COLOR=blue]object[/COLOR] obj) {
[COLOR=blue]throw[/COLOR] [COLOR=blue]new[/COLOR] [COLOR=darkblue][B]NotImplementedException[/B][/COLOR]
}
[COLOR=blue]public[/COLOR] [COLOR=blue]object[/COLOR] Clone() {
[COLOR=blue]throw[/COLOR] [COLOR=blue]new[/COLOR] [COLOR=darkblue][B]NotImplementedException[/B][/COLOR]();
}
}
I can do this:
IComparable x = new Person(); ICloneable x2 = new Person();
but not this:
IList x3 = new Person();
This fails because Person doesn't implement IList.
foreach (int v in isc) Console.WriteLine(v);
This fails because isc is of type IStructuralComparable which is used to compare structures. In this context VS cannot guaranteer that isc
is a collection so you cannot iterate over it.
However,
foreach (int v in (IEnumerable)isc) Console.WriteLine(v);
This works because you are assuming isc can be converted to object of type IEnumerable (something you can enumerate over). If it turns out
isc is not an object that implements IEnumerable then this will fail.
Interfaces ARE very abstract. They can be used in a lot of different ways! For instance, LINQ was designed so that it works on anything that implements IEnumerable. So you can very easily swap data structures and not break your entire program.
#7
Posted 30 August 2011 - 07:50 AM
thanks for explanation)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









