The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Enumerators and boxing..

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Enumerators and boxing.. Posted: Mar 28, 2004 9:12 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Enumerators and boxing..
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

Jeroen asked:

While we're on the subject of boxing, why doesn't foreach do the same optimization as using? foreach always seems to box the enumerator struct.

If you use a struct as an enumerator, you will always box when you go to IEnumerator. Interfaces are reference types, and you have to box to get an interface reference to a struct.

The fix is to implement the strongly-typed enumerator pattern. Start with the IEnumerable/IEnumerator version and:

1) Change the type of the Current property in the IEnumerator struct from object to the strong type.
2) Remove IEnumerator from the implementation list on the struct type.
3) Remove IEnumerable from the implementation list
4) Change GetEnumerator() so that it returns the struct type rather than IEnumerator

The compiler will then deal with the strongly-typed version. If you want to also allow the interface versions, you can implement them specifically.

Here's some code (sorry about the formatting):

public class IntegerListExplicit: IEnumerable
{
int count = 0;
int allocated = 10;
int[] elements = new int[10];
public IntegerListExplicit()
{
}
void Expand()
{
if (count == allocated)
{
int[] newElements = new int[allocated * 2];
for (int i = 0; i < count; i++)
newElements[i] = elements[i];
allocated = allocated * 2;
elements = newElements;
}
}
public int Add (int item)
{
lock(this)
{
Expand();
elements[count] = item;
count++;
}
return count - 1;
}
public int Count
{
get
{
return(count);
}
}
void CheckIndex(int index)
{
if (index < 0 || index > count - 1)
throw(new IndexOutOfRangeException(String.Format("Index {0} out of range", index)));
}
public int this[int index]
{
get
{
CheckIndex(index);
return(elements[index]);
}
set
{
CheckIndex(index);
elements[index] = value;
}
}
public override string ToString()
{
string[] s = new string[count];
for (int i = 0; i < count; i++)
s[i] = elements[i].ToString();
return(String.Join("\n", s));
}
IEnumerator IEnumerable.GetEnumerator()
{
return((IEnumerator) GetEnumerator());
}
public IntegerListEnumerator GetEnumerator()
{
return(new IntegerListEnumerator(this));
}
public class IntegerListEnumerator: IEnumerator
{
IntegerListExplicit list;
int index = -1;
public IntegerListEnumerator(IntegerListExplicit list)
{
this.list = list;
}
public bool MoveNext()
{
index++;
if (index == list.Count)
return(false);
else
return(true);
}
object IEnumerator.Current
{
get
{
return(Current);
}
}
public int Current
{
get
{
return(list[index]);
}
}
public void Reset()
{
index = -1;
}
}
}

 

Read: Enumerators and boxing..

Topic: Kevin Schofield Replies Previous Topic   Next Topic Topic: MSDN releases our next .NETCF White Paper on Performance

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use