I am pretty new in C# (I came from Java) and I have a doubt about the use of:
I have this class:
namespace DataModel.MaliciousCode
{
public class PagedMalicious : Shared.Paged
{
public IEnumerable<MaliciousSmall> MaliciousCode { get; set; }
}
}
As you can see this class contains only a IEnumerable<MaliciousSmall> MaliciousCode
Reading the online documentation it seems to me to understand that IEnumerable is an interface that give me an iterator on a non generic collection.
What exactly means the previous assertion?
I have MaliciousSmall that is the type of a model object in my application (an object that contains some properties that map the fields of a table on the DB)
So my doubt are:
IEnumerable<MaliciousSmall> MaliciousCode: MaliciousCode is an iterable collection of MaliciousSmall objects? So it means that it represent a collection and some provided methods to iterate on it?
If the previous assertion is true, ok MaliciousCode object is an iterable collection but IEnumerable is an interface so who implement the method to iterate on this collection (coming from Java I think that an interface is not provided of implemented methods)
Some one can help me to understand these things?
Tnx
Andrea
IEnumerable<MaliciousSmall> MaliciousCode:MaliciousCodeis an iterable collection ofMaliciousSmallobjects? So it means that it represent a collection and some provided methods to iterate on it?
Sort of - IEnumerable<T> provides one method - GetEnumerator - which returns an IEnumerator<T>. THAT interface allows you to iterate over the collection. Pre-Linq all IEnumerable allowed you to do was use the collection in a foreach loop (or use the provided IEnumerator directly, which is rare). Linq has since defined extension methods on IEumerable<T> that allow more sophisticated queries like Select, Where, Count, etc.
If the previous assertion is true, ok
MaliciousCodeobject is an iterable collection butIEnumerableis an interface so who implement the method to iterate on this collection (coming from Java I think that an interface is not provided of implemented methods)
Typically the implementation is provided by using an underlying collection type like List<MaliciousSmall> or MaliciousSmall[]. So the IEnumerable implementation is provided by that class. The yield keyword introduced in C# 2.0 allows you to "return" an IEnumerable<T> and let the compiler provide the actual implementation.
So in your class, you might internally implement the collection as a List<T>:
public class PagedMalicious : Shared.Paged
{
public IEnumerable<MaliciousSmall> MaliciousCode { get; set; }
public PagedMalicious()
{
MaliciousCode = new List<MaliciousSmall>();
}
// other private methods that add to MaliciousCode
}
The use of IEnumerable<T> allows you to change the internal implementation without changing the public interface.
Your property MaliciousCode, represents an object of a class that implements IEnumerable<T> interface. On it's own, IEnumerable does not really mean anything. It just provides a structure. It is user's responsibility to implement the methods that are provided with interface whatever way the see it suitable.
Edit: Here is a simple example to demonstrate:
private void Form3_Load(object sender, EventArgs e)
{
Parent parent = new Parent();
parent.Child = new List<Child>(); // -> this is where implementer is decided.
//Before this line, Child property is not instantiated and is not referring to any object.
}
public class Parent
{
public IEnumerable<Child> Child { get; set; }
}
public class Child
{
public int MyProperty { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With