Jump to content

IEbumerable i = {1,2,3}

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Nixon

Nixon

    Newbie

  • Members
  • Pip
  • 8 posts
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?

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
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:


 [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
Nixon

Nixon

    Newbie

  • Members
  • Pip
  • 8 posts
hm...so "i" just keep reference?
does interfaces inferits from system.object to keep references?

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
Basically i and x are to variables that point to the same location in memory.

Like this diagram:
Attached File  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
Nixon

Nixon

    Newbie

  • Members
  • Pip
  • 8 posts
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:


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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme

[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
Nixon

Nixon

    Newbie

  • Members
  • Pip
  • 8 posts
thanks for explanation)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users