What is the best way to convert an IEnumerator to a generic IEnumerator?

I don’t believe there’s anything in the framework, but you could easily write one: IEnumerator<T> Cast<T>(IEnumerator iterator) { while (iterator.MoveNext()) { yield return (T) iterator.Current; } } It’s tempting to just call Enumerable.Cast<T> from LINQ and then call GetEnumerator() on the result – but if your class already implements IEnumerable<T> and T is a value … Read more

Overriding Scala Enumeration Value

The Enumeration values are instance of the Val class. This class can be extended and a factory method can be added. object My extends Enumeration { val A = Value(“name”, “x”) val B = Value(“other”, “y”) class MyVal(name: String, val x : String) extends Val(nextId, name) protected final def Value(name: String, x : String): MyVal … Read more

How to get Get a C# Enumeration in Javascript

You can serialize all enum values to JSON: private void ExportEnum<T>() { var type = typeof(T); var values = Enum.GetValues(type).Cast<T>(); var dict = values.ToDictionary(e => e.ToString(), e => Convert.ToInt32(e)); var json = new JavaScriptSerializer().Serialize(dict); var script = string.Format(“{0}={1};”, type.Name, json); System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), “CloseLightbox”, script, true); } ExportEnum<MyEnum>(); This registers a script like: MyEnum={“Red”:1,”Green”:2,”Blue”:3};

Can Swift enums have multiple raw values?

You have a couple options. But neither of them involve raw values. Raw values are just not the right tool for the task. Option 1 (so-so): Associated Values I personally highly recommend against there being more than one associated value per enum case. Associated values should be dead obvious (since they don’t have arguments/names), and … Read more

Dictionary enumeration in C#

To enumerate a dictionary you either enumerate the values within it: Dictionary<int, string> dic; foreach(string s in dic.Values) { Console.WriteLine(s); } or the KeyValuePairs foreach(KeyValuePair<int, string> kvp in dic) { Console.WriteLine(“Key : ” + kvp.Key.ToString() + “, Value : ” + kvp.Value); } or the keys foreach(int key in dic.Keys) { Console.WriteLine(key.ToString()); } If you … Read more

Changing objects value in foreach loop?

You cannot change the iteration variable of a foreach-loop, but you can change members of the iteration variable. Therefore change the ChangeName method to private void ChangeName(StudentDTO studentDTO) { studentDTO.name = SomeName; } Note that studentDTO is a reference type. Therefore there is no need to return the changed student. What the ChangeName method gets, … Read more