Home
Forums
New posts
Search forums
Articles
Latest reviews
Search resources
Members
Current visitors
Newsgroups
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
foreach with generics and without generics
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Tony Johansson, post: 13632079"] Hello! The first block of code below is just using an ArrayList. The ArrayList is implementing IEnumerable that look like this. public interface IEnumerable { IEnumerator GetEnumerator(); } IEnumerator look like this public interface IEnumerator { bool MoveNext(); void Reset(); object Current { get; } } Now to my question as you can see property Current is returning an object but in the foreach below I don't have to use any kind of cast. Can somebody explain that ? In the second block of code example I use generics which mean I use IEnumerable<T> but as you can see the code is identical to the code when non generics was used Normally the code you write is different when you use generics compared to when no generics is used. For example if you compare between implementing IComparable and IComparable<T> we have different signature in method CompareTo So can somebody give an example of a piece of code that a need for a cast is required becuse of propery Current is returning an object. first block of code ************* { ArrayList list = new ArrayList(); list.Add(4); list.Add(1); list.Add(2); list.Add(3); list.Add(9); foreach (int number in list) Console.WriteLine(number); } second block of code **************** { List<int> myList = new List<int>(); myList.Add(4); myList.Add(1); myList.Add(2); myList.Add(3); myList.Add(9); foreach (int number in myList) Console.WriteLine(number); } //Tony [/QUOTE]
Verification
Post reply
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
foreach with generics and without generics
Top