insanepenguin's Profile
Reputation: 7
Worker
- Group:
- Active Members
- Active Posts:
- 235 (0.22 per day)
- Joined:
- 08-July 10
- Profile Views:
- 2,049
- Last Active:
Nov 10 2012 11:50 PM- Currently:
- Offline
Previous Fields
- Country:
- GB
- OS Preference:
- Windows
- Favorite Browser:
- Chrome
- Favorite Processor:
- AMD
- Favorite Gaming Platform:
- PC
- Your Car:
- Who Cares
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: yield return functionality
Posted 14 Jul 2012
thanks CodingSup3rnatur@l-360,
That explanation helped a lot, so the yield result in GetEnumerator() is basically generating an iterator object in the background specifying to the calling code (foreach etc) how to iterate the collection<T>.
-
In Topic: Learning to do more with a language
Posted 14 Jul 2012
I can vouch for the guided learning approach, I've been learning C# for a couple of years and can create my own functional/useful(debatable!) applications that connect and use databases etc - I'm by no means advanced yet and find the best way is to keep building projects where you are guided by someone far more experienced and try to understand how they are building the application and why they make the choices they make.
I've been frustrated in the past by getting this great idea in my head and discovering I do not yet have the skill set to implement it, step by step is best! -
In Topic: yield return functionality
Posted 14 Jul 2012
I've written a simple collection class to help me understand IEnumerable<T>.GetEnumerator();
Because my class is generic, implementing an iterator using IEnumerable<T>.GetEnumerator() allows the compiler to figure out behind the scenes how to iterate through different types using the BasicCollection<T> class? e.g:
namespace BasicEnumerator { class Program { static void Main(string[] args) { BasicCollection<string> myCollection = new BasicCollection<string>(); myCollection.AddData("Lets", "Try", "This", "Out"); foreach (string item in myCollection) { Console.WriteLine(item); } } } class BasicCollection<T> : IEnumerable<T> { List<T> list = new List<T>(); public void AddData(params T[] dataParam) { foreach (var data in dataParam) { list.Add(data); } } IEnumerator<T> IEnumerable<T>.GetEnumerator() { foreach (var data in list) { yield return data; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } }
Thanks -
In Topic: yield return functionality
Posted 13 Jul 2012
Thanks guys,
So using yield return can generate the objects required on the fly, for example the alternative to the FilterByCategory method below would have to populate a List<Products> (or something IEnumerable<Products>) then look for the filter?
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum, string categoryParam) { foreach (Product prod in productEnum) { if (prod.Category == categoryParam) { yield return prod; } } } } } public class ShoppingCart : IEnumerable<Product> { public List<Product> products{ get; set;} public IEnumerator<Product> GetEnumerator() { return products.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public class Product { public int ProductID { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string Category { get; set; } } } -
In Topic: yield return functionality
Posted 13 Jul 2012
Maybe a better way to phrase the question would be: What is the difference between return and yield return?
Thanks
My Information
- Member Title:
- D.I.C Head
- Age:
- 29 years old
- Birthday:
- March 11, 1984
- Gender:
-
- Location:
- Wrexham, Wales
- Years Programming:
- 0
- Programming Languages:
- C#
Contact Information
- E-mail:
- Private
- Facebook:
- http://www.facebook.com/insanealcoholicpenguin
- Twitter:
- mikejharper
Friends
insanepenguin hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
insanepenguin has no profile comments yet. Why not say hello?