Here is the java code i incompletely tried to translate
static Enumeration enumerate()
{
Vector list = new Vector();
Enumeration e = cache.keys();
while (e.hasMoreElements())
{
Vector v = (Vector) cache.get(e.nextElement());
for (int i = 0; i < v.size(); i++)
{
list.addElement(v.elementAt(i));
}
}
return list.elements();
}
This is the C# translation but not complete
public static IEnumerable<Http> enumurate()
{
List<Http> list = new List<Http>();
IEnumerator e = cache.Keys.GetEnumerator();
while (e.MoveNext())/*While e has more element*/
{
var vector = (List<Http>)cache[e.Current];
for (int i = 0; i < vector.Count; i++)
{
list.Add(vector.ElementAt<Http>(i));
}
}
return //Something missing!!
}
Any help please !
In C# List<Http> implements IEnumerable<Http> so you can simply return your list:
return list;
To convert the code to C# even more, you could just skip the adding of elements to the list and yield results directly:
public static IEnumerable<Http> enumerate()
{
IEnumerator e = cache.Keys.GetEnumerator();
while (e.MoveNext())/*While e has more element*/
{
var vector = (List<Http>)cache[e.Current];
for (int i = 0; i < vector.Count; i++)
{
yield return vector.ElementAt<Http>(i);
}
}
}
Also, you can avoid using enumerators directly and make the code even more readable:
public static IEnumerable<Http> enumerate()
{
foreach (var key in cache.Keys)
{
foreach (var http in (List<Http>)cache[key])
{
yield return http;
}
}
}
Just return the local list. List<T> implements IEnumerable<T>.
In C# you can use LINQ to simplify your code:
public static IEnumerable<Http> enumerate()
{ return cache.Keys.SelectMany(key => (List<Http>)cache[key]); }
Assuming cache is declared as Dictionary<..., List<Http>> you can avoid key lookup by using the Values property:
public static IEnumerable<Http> enumerate()
{ return cache.Values.SelectMany(list => list); }
One important difference between the above and the code that you have now is that the above is not evaluated until you actually try to iterate through the returned collection. If you want to evaluate results immediately, you can add a call to .ToList(). This is equivalent to your original code:
public static IEnumerable<Http> enumerate()
{ return cache.Keys.SelectMany(key => (List<Http>)cache[key]).ToList(); }
Related
When Repeat(item, nr)) is added to res with res.Add(Repeat(item, nr)), the Repeat method is not executed.
Why? I used a debugger and it does not execute it.
public class Program
{
public static IEnumerable<IEnumerable<T>> Echo<T>(IEnumerable<T> src, int nr)
{
List<IEnumerable<T>> res = new List<IEnumerable<T>>();
foreach (T item in src)
{
Console.Write(item);
res.Add(Repeat(item, nr));
}
return res;
}
static IEnumerable<T> Repeat<T>(T item, int nr)
{
for (int i = 0; i < nr; i++)
{
Console.Write(item);
yield return item;
}
}
public static void Main()
{
string[] src = { "a", "b", "c" };
Echo(src, 2);
}
}
Enumerators built with yield return aren't executed until they are iterated over. So you'd need to have some code that iterates over the enumerator that's added to the list. Since you don't ever do anything with the enumerator returned by Echo (let alone the items inside it), the enumerator is never executed.
I have been looking for a way of splitting a foreach loop into multiple parts and came across the following code:
foreach(var item in items.Skip(currentPage * itemsPerPage).Take(itemsPerPage))
{
//Do stuff
}
Would items.Skip(currentPage * itemsPerPage).Take(itemsPerPage) be processed in every iteration, or would it be processed once, and have a temporary result used with the foreach loop automatically by the compiler?
No, it would be processed once.
It's the same like:
public IEnumerable<Something> GetData() {
return someData;
}
foreach(var d in GetData()) {
//do something with [d]
}
The foreach construction is equivalent to:
IEnumerator enumerator = myCollection.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object current = enumerator.Current;
Console.WriteLine(current);
}
}
finally
{
IDisposable e = enumerator as IDisposable;
if (e != null)
{
e.Dispose();
}
}
So, no, myCollection would be processed only once.
Update:
Please note that this depends on the implementation of the IEnumerator that the IEnumerable uses.
In this (evil) example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
namespace TestStack
{
class EvilEnumerator<T> : IEnumerator<T> {
private IEnumerable<T> enumerable;
private int index = -1;
public EvilEnumerator(IEnumerable<T> e)
{
enumerable = e;
}
#region IEnumerator<T> Membres
public T Current
{
get { return enumerable.ElementAt(index); }
}
#endregion
#region IDisposable Membres
public void Dispose()
{
}
#endregion
#region IEnumerator Membres
object IEnumerator.Current
{
get { return enumerable.ElementAt(index); }
}
public bool MoveNext()
{
index++;
if (index >= enumerable.Count())
return false;
return true;
}
public void Reset()
{
}
#endregion
}
class DemoEnumerable<T> : IEnumerable<T>
{
private IEnumerable<T> enumerable;
public DemoEnumerable(IEnumerable<T> e)
{
enumerable = e;
}
#region IEnumerable<T> Membres
public IEnumerator<T> GetEnumerator()
{
return new EvilEnumerator<T>(enumerable);
}
#endregion
#region IEnumerable Membres
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
class Program
{
static void Main(string[] args)
{
IEnumerable<int> numbers = Enumerable.Range(0,100);
DemoEnumerable<int> enumerable = new DemoEnumerable<int>(numbers);
foreach (var item in enumerable)
{
Console.WriteLine(item);
}
}
}
}
Each iteration over enumerable would evaluate numbers two times.
Question:
Would items.Skip(currentPage * itemsPerPage).Take(itemsPerPage) be
processed every iteration, or would it be processed once, and have a
temporary result used with the foreach loop automatically by the
compiler?
Answer:
It would be processed once, not every iteration. You can put the collection into a variable to make the foreach more readable. Illustrated below.
foreach(var item in items.Skip(currentPage * itemsPerPage).Take(itemsPerPage))
{
//Do stuff
}
vs.
List<MyClass> query = items.Skip(currentPage * itemsPerPage).Take(itemsPerPage).ToList();
foreach(var item in query)
{
//Do stuff
}
vs.
IEnumerable<MyClass> query = items.Skip(currentPage * itemsPerPage).Take(itemsPerPage);
foreach(var item in query)
{
//Do stuff
}
The code that you present will only iterate the items in the list once, as others have pointed out.
However, that only gives you the items for one page. If you are handling multiple pages, you must be calling that code once for each page (because somewhere you must be incrementing currentPage, right?).
What I mean is that you must be doing something like this:
for (int currentPage = 0; currentPage < numPages; ++currentPage)
{
foreach (var item in items.Skip(currentPage*itemsPerPage).Take(itemsPerPage))
{
//Do stuff
}
}
Now if you do that, then you will be iterating the sequence multiple times - once for each page. The first iteration will only go as far as the end of the first page, but the next will iterate from the beginning to the end of the second page (via the Skip() and the Take()) - and the next will iterate from the beginning to the end of the third page. And so on.
To avoid that you can write an extension method for IEnumerable<T> which partitions the data into batches (which you could also describe as "paginating" the data into "pages").
Rather than just presenting an IEnumerable of IEnumerables, it can be more useful to wrap each batch in a class to supply the batch index along with the items in the batch, like so:
public sealed class Batch<T>
{
public readonly int Index;
public readonly IEnumerable<T> Items;
public Batch(int index, IEnumerable<T> items)
{
Index = index;
Items = items;
}
}
public static class EnumerableExt
{
// Note: Not threadsafe, so not suitable for use with Parallel.Foreach() or IEnumerable.AsParallel()
public static IEnumerable<Batch<T>> Partition<T>(this IEnumerable<T> input, int batchSize)
{
var enumerator = input.GetEnumerator();
int index = 0;
while (enumerator.MoveNext())
yield return new Batch<T>(index++, nextBatch(enumerator, batchSize));
}
private static IEnumerable<T> nextBatch<T>(IEnumerator<T> enumerator, int blockSize)
{
do { yield return enumerator.Current; }
while (--blockSize > 0 && enumerator.MoveNext());
}
}
This extension method does not buffer the data, and it only iterates through it once.
Given this extension method, it becomes more readable to batch up the items. Note that this example enumerates through ALL items for all pages, unlike the OP's example which only iterates through the items for one page:
var items = Enumerable.Range(10, 50); // Pretend we have 50 items.
int itemsPerPage = 20;
foreach (var page in items.Partition(itemsPerPage))
{
Console.Write("Page " + page.Index + " items: ");
foreach (var i in page.Items)
Console.Write(i + " ");
Console.WriteLine();
}
How to create dynamic incrementing variable using "for" loop in C#? like this:
track_1, track_2, track_3, track_4. so on.
You can't create dynamically-named variables. All you can do - it to create some collection or array, and operate with it.
I think the best class for you is generic List<>:
List<String> listWithDynamic = new List<String>();
for (int i = 1; i < limit; i +=1)
{
listWithDynamic.Add(string.Format("track_{0}", i));
...
}
Assuming you want strings:
for (int i = 1; i < limit; i +=1)
{
string track = string.Format("track_{0}", i);
...
}
But when you already have variables called track_1, track_2, track_3, track_4 you will need an array or List:
var tracks = new TrackType[] { track_1, track_2, track_3, track_4 } ;
for (int i = 0; i < tracks.length; i++)
{
var track = tracks[i]; // tracks[0] == track_1
...
}
Obvious Solution
for (var i = 0; i < 10; i++)
{
var track = string.Format("track_{0}", i);
}
Linq-Based Solution
foreach (var track in Enumerable.Range(0, 100).Select(x => string.Format("track_{0}", x)))
{
}
Operator-Based Solution This is somewhat hacky, but fun none-the-less.
for (var i = new Frob(0, "track_{0}"); i < 100; i++)
{
Console.WriteLine(i.ValueDescription);
}
struct Frob
{
public int Value { get; private set; }
public string ValueDescription { get; private set; }
private string _format;
public Frob(int value, string format)
: this()
{
Value = value;
ValueDescription = string.Format(format, value);
_format = format;
}
public static Frob operator ++(Frob value)
{
return new Frob(value.Value + 1, value._format);
}
public static Frob operator --(Frob value)
{
return new Frob(value.Value - 1, value._format);
}
public static implicit operator int(Frob value)
{
return value.Value;
}
public static implicit operator string(Frob value)
{
return value.ValueDescription;
}
public override bool Equals(object obj)
{
if (obj is Frob)
{
return ((Frob)obj).Value == Value;
}
else if (obj is string)
{
return ((string)obj) == ValueDescription;
}
else if (obj is int)
{
return ((int)obj) == Value;
}
else
{
return base.Equals(obj);
}
}
public override int GetHashCode()
{
return Value;
}
public override string ToString()
{
return ValueDescription;
}
}
don't know if I get your question, but I will try:
for(var i = 1; i < yourExclusiveUpperbound; i++)
{
var track = String.Format("$track_{0}", i);
// use track
}
or with some LINQ-Magic:
foreach(var track in Enumerate.Range(1, count)
.Select(i => String.Format("$track_{0}", i)))
{
// use track
}
Do as follow:
for (int i = 0; i < lenght; i ++)
{
any work do in loop
}
No, we can't create dynamically named variables in a loop. But, there are other elegant ways to address the problem instead of creating dynamically named variables.
One could be, create an array or list before the loop and store values in array / list items in the loop. You can access the array / list later anywhere in your code. If you know which variable you want to use (track_1, track_2, ...), you can simply access it from the array / list (tracks[1], tracks[2], ...).
List<String> tracks = new List<String>();
for (int i = 1; i < limit; i++)
{
Track track = new Track();
tracks.Add(track);
...
}
Is there anyway to foreach through a list from the end to the beginning rather than the beginning to then end (preferably without reordering the list).
using System.Linq;
foreach(var item in source.Reverse())
{
...
}
Edit: There is one more step if you are dealing specifically with a List<T>. That class defines its own Reverse method whose signature is not the same as the Enumerable.Reverse extension method. In that case, you need to "lift" the variable reference to IEnumerable<T>:
using System.Linq;
foreach(var item in list.AsEnumerable().Reverse())
{
...
}
you could use a regular for loop, start at the end and decrement, instead of starting at the top and incrementing.
something like:
for(int i=foo.lenth; i != 0; i--)
{
do stuff
}
You probably don't want to do anything complicated, so I would suggest just using a for loop.
However, if it were somehow a requirement, you can certainly implement your own iterators for custom list iteration behavior.
It depends on what you mean by list.
List<T> ? No, unless you use Linq and it's Reverse() function.
Your custom collection? Easily, just implement IEnumerator like you
want.
Error checking ommitted for clarity. Use a custom implementation of IEnumerable and IEnumerator. This will avoid unnecessary copying.
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class ReversedEnumerator : IEnumerator<int>
{
List<int> v;
int index;
public ReversedEnumerator(List<int> v) {
this.v = v;
this.index = v.Count;
}
public int Current
{
get { return v[index]; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return v[index]; }
}
public bool MoveNext()
{
return --index >= 0;
}
public void Reset()
{
index = this.v.Count;
}
}
class EnumeratorStub : IEnumerable<int>
{
List<int> v;
public EnumeratorStub(List<int> v)
{
this.v = v;
}
public IEnumerator<int> GetEnumerator()
{
return new ReversedEnumerator(v);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new ReversedEnumerator(v);
}
}
class Program
{
static EnumeratorStub Reverse(List<int> v)
{
return new EnumeratorStub(v);
}
static void Main(string[] args)
{
List<int> v = new List<int>();
v.Add(1);
v.Add(2);
v.Add(3);
foreach (int item in Reverse(v))
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
I would recommend to refactor the code sample to use generics. That way you could use this for any container type.
IList<String> strList = new IList<String>();
strList.Add("A");
strList.Add("B");
strList.Add("C");
for (int i = strList.Count-1; i>=0;i--)
{
Console.WriteLine(strList[i]);
}
not tried but should work.
not c# but you can do it too :-)
Dim a As New List(Of Integer)
a.Add(1)
a.Add(2)
a.Add(3)
For Each i In a.AsEnumerable.Reverse
Debug.Print(i)
Next
You can construct your list as a stack and then iterate over the stack:
Stack<char> stack = new Stack<char>();
//Add items...
foreach(var item in stack)
{
...
}
Is it possible in C# to something like the following
foreach (ref string var in arr) {
var = "new value";
}
so that var variable was treated as reference and assigning to var would change an array element?
There is no such construct for updating a loop; an iterator is read-only. For example, the following provides a perfectly valid iterator:
public IEnumerable<int> Get1Thru5() {
yield return 1; yield return 2; yield return 3;
yield return 4; yield return 5;
}
How would it update? What would it update?
If the data is an array/list/etc, then something like:
for(int i = 0 ; i < arr.Length ; i++) {
arr[i] = "new value";
}
Or other options depending on the specific container.
Update; at a push, an extension method:
public static void UpdateAll<T>(this IList<T> list, Func<T, T> operation) {
for (int i = 0; i < list.Count; i++) {
list[i] = operation(list[i]);
}
}
static void Main() {
string[] arr = { "abc", "def", "ghi" };
arr.UpdateAll(s => "new value");
foreach (string s in arr) Console.WriteLine(s);
}
No. The foreach statement is simply syntax sugar on top of the IEnumerable interface. This interface defines a method to get en IEnumerator which in turn has methods to do read-only enumeration:
Current : object
MoveNext() : bool
Reset() : void
foreach(string s in strings)
{
Console.WriteLine(s);
}
is compiler shortcut for:
IEnumerator e = strings.GetEnumerator();
string s;
while(e.MoveNext())
{
s = e.Current;
Console.WriteLine(s);
}
Since IEnumerator.Current is a get-only property you can't set the value.
// Non-generic IEnumerator shown.
interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}
If you want to support an updatable enumerator you will need to create it yourself -- but you won't be able to use "foreach" with it, and you'll have to implement wrappers around all the common IEnumerable classes.
You'll have to analyze your current situation and figure out how to update. If you're using an IList interface you can do:
for(int i = 0; i < strings.Count; ++i)
{
string s = strings[i];
//do work
s = s.ToUpperInvariant();
strings[i] = s;
}
In the case of a string, no; C#
strings are immutable (cannot be
changed). If you were enumerating over
objects of a different, mutable type,
you can change the properties of those
objects.
Just to illustrate what Jacob is talking about. Consider the code snippet below:
class MyInt
{
public int Val { get; set; }
public MyInt(int val) { this.Val = val; }
}
class Program
{
static void Main(string[] args)
{
MyInt[] array = new MyInt[] { new MyInt(1), new MyInt(2) };
foreach (var obj in array) Console.Write("{0}\t", obj.Val);
foreach (var obj in array)
{
obj = new MyInt(100); // This doesn't compile! the reference is read only
obj.Val *= 10; // This works just fine!
}
foreach (var obj in array) Console.Write("{0}\t", obj.Val);
}
}
Indeed, if you try to assign to the "obj" as above you'll get a compile time error. But nothing prevents you from modifying MyInt's properties through the "obj" reference
In the case of a string, no; C# strings are immutable (cannot be changed). If you were enumerating over objects of a different, mutable type, you can change the properties of those objects.