Embed IEnumerable in a Property

Assuming class

public class Foo 
{
    public List<Bar> Bar = new List<Bar>();
    public string Something;
    public TStatus Status;
}

A bar is a class defined as

public class Bar
{
    public string Code;
    public string Message;
    public TStatus Status;
}

I need to iterate over a list string, but I cannot use foreach because I need to implement IEnumerable. I am a little newbie and it’s hard for me to understand how to implement it, any help was appreciated.

+3
source share
8 answers

Few things:

1.) Regarding your actual question , you have two options:

You can iterate over a field Bar(or a property if you change it to a property as indicated below) in your code:

Foo foo = new Foo();

foreach(Bar bar in foo.Bar)
{
    ...
}

Or you can do Fooimplement IEnumerable<Bar>(this is harder):

public class Foo : IEnumerable<Bar>
{
    .. your existing code goes here

    public IEnumerator<Bar> GetEnumerator()
    {
        return Bar.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return Bar.GetEnumerator();
    }
}

This will allow you to do this:

Foo foo = new Foo();

foreach(Bar bar in foo)
{

}

. , Foo ( ) Bar, .

2.) , , - , . :

public class Foo
{
    public string Something
    {
        get { return ...; }
        set { ... = value; }
    }
}

, . - :

public class Foo
{
    private string something;

    public string Something
    {
        get 
        { 
            return something; 
        }
        set  
        { 
            something = value; 
        }
    }
}

, , get, - set ( , ).

-, ( ), , , . ( / - ) , , . # 3.0 auto, :

public class Foo
{
    public string Something { get; set; }
}

Something , . # ( get, set ) .

+11

:

var foo = new Foo();
foo.Bar.Add(new Bar {Code = "code1" });
foo.Bar.Add(new Bar {Code = "code2" });

foreach(var b in foo.Bar)
{
     Console.WriteLine(b.Code);
}
+2

, , , , :

private List<Bar> _bar = new List<Bar>();


public IEnumerable<Bar> Bar 
{
    get { return _bar; }
}

.

+2

Bar, foreach. IEnumerable , List<> :

var fooInstance = new Foo();
// initialize...
fooInstance.Bar.Add(new Bar {Code = "a" });
fooInstance.Bar.Add(new Bar {Code = "b" });

// iterate over members of Foo.Bar
foreach( var item in fooInstance.Bar )
{
    // access the proeprties of Bar...
    Console.WriteLine( item.Code );
}  
+1

List<T> .

, , List<T> .. - .

:

public class Foo 
{
    private List<Bar> _barList;

    public List<Bar> BarList 
    {
        get
        {
            return this._barList ?? (this._barList = new List<Bar>());
        }
    }
}

:

Foo myFoo = new Foo();

myFoo.BarList.Add(new Bar());
myFoo.BarList.Add(new Bar());

foreach(Bar bar in myFoo.BarList)
{
    //do something with bar
}
+1

, :

var enumerator = foo.Bar.GetEnumerator();
while (enumerator.MoveNext())
{
    Console.WriteLine(enumerator.Current);
}
+1

, , , Foo, IEnumerable IEnumerable<Bar>, - Bar, Foo.Bar.

EDIT: , , . , .

:

  • Foo Bar , TStatus .
  • Foo, Bar, Status, Foo object Status. , , , Status Foo, Bar.

, BarsFilteredByStatus Foo. .NET iterator function Bar, Bar.Status Foo.Status. - , , , .

// an example status enum
enum SomeStatus
{
    Open,
    Closed,
    Funky,
}

// Blarg<TStatus> is just some container class.  This isn't necessary, but it allows TStatus to be a generic parameter rather than a specific type.
class Blarg<TStatus>
{
    public class Bar
    {
        public string Code;
        public string Message;
        public TStatus Status;
    }

    public class Foo
    {
        public List<Bar> Bar = new List<Bar>();
        public string Something;
        public TStatus Status;

        public IEnumerable<Bar> BarsFilteredByStatus
        {
            get
            {
                // return a filtered collection of bars where Bar.Status equals Foo.Status
                foreach (var bar in this.Bar)
                {
                    if (this.Status.Equals(bar.Status))
                        yield return bar;
                }
            }
        }
    }
}

// Code from this point on is a usage example

class Program
{
    static void Main(string[] args)
    {
        // set up some example data
        var bars = new List<Blarg<SomeStatus>.Bar>();
        bars.Add(new Blarg<SomeStatus>.Bar { Code = "123", Status = SomeStatus.Open });
        bars.Add(new Blarg<SomeStatus>.Bar { Code = "234", Status = SomeStatus.Closed });
        bars.Add(new Blarg<SomeStatus>.Bar { Code = "345", Status = SomeStatus.Funky });
        bars.Add(new Blarg<SomeStatus>.Bar { Code = "456", Status = SomeStatus.Open });
        bars.Add(new Blarg<SomeStatus>.Bar { Code = "567", Status = SomeStatus.Funky });

        // create a Foo object
        Blarg<SomeStatus>.Foo foo = new Blarg<SomeStatus>.Foo
        {
            Bar = bars,
            Status = SomeStatus.Open,
        };

        // now iterate over the Foo.BarsFilteredByStatus property
        // This will iterate over all Bar objects contained within Foo.Bar where Bar.Status equals Foo.Status
        foreach (var bar in foo.BarsFilteredByStatus)
        {
            Console.WriteLine(bar.Code);
        }
    }
}
+1

:

List<Bar> bars = new List<Bar>( );
foreach ( Bar bar in bars ) {
    //Put your code here
}

You must name your instance of List different from Bar to avoid confusion.

0
source

Source: https://habr.com/ru/post/1770153/


All Articles