Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1841 | Besucher online: 126
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Enumerator as a parameter of a method


Autor: erik
Sprache: C#
Bewertung: 8,2
(1 Bewertung)
Anzahl der Aufrufe: 5096
  
Kick it on dotnet-kicks.de  

Beschreibung:

The use of custom enumerators is an elegant way to filter a list.
Sometimes a function has to be called on different filtered sets with some changes to the parameters.
This sniplet will offer you an elegant way of doing so.
It can for instance be used to write different tags in an XML file (implement the non-provided DoWriteTag method; or any other)

private void DoWrite(IEnumerable enm, string tagName) is the actual example, the other code was added to illustrate its use.


Abgelegt unter: IEnumerable, example, Enumerator, parameter.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// both Capability and OperationMode are held in the List<Group> this.capabilities
// useEnumerate.DoWrite shows how th build a generic call to handle subsets of this.capabilities.
// It offers an elegant and very readable call because the Enumerator is a parameter of the method
using System.Collections;
using System.Collections.Generic;

namespace Sniplet
{
    #region Help Classes
    // example base class
    public class Group
    {
        public bool Included
        {
            get
            {
                return true;
            }
        }
    }

    // example Capability
    public class Capability : Group
    {
        public Capability()
            :base()
        {
        }
    }
    // Example OperationMode
    public class OperationMode : Group
    {
        public OperationMode()
            : base()
        {
        }
    }
    #endregion;

    public class useEnumerate
    {
        private List<Group> capabilities;
        public useEnumerate()
        {
            this.capabilities = new List<Group>();
            // todo: fill this.capabilities
        }

        /// <summary>
        /// since the enumerator is a part of the method this call automatically filters on this.capabilities
        /// It enables you to handle each subset differently, for instance by providing a different tagName
        /// </summary>
        /// <param name="enm">The filter that will provide you with a subset of this.capabilities </param>
        /// <param name="tagName">example of an additional parameter for the call</param>
        /// <returns></returns>
        private void DoWrite(IEnumerable enm, string tagName)
        {
            foreach (Group g in enm)
            {
                // Do something on the chosen subset of capabilities
                // e.g.
                // DoWriteTag(tagname);
            }
        }

        /// <summary>
        /// Example of a call to DoWrite
        /// </summary>
        /// <returns></returns>
        protected void WriteTagNames()
        {
            DoWrite(this.ContainedCapabilities, "capability");
            DoWrite(this.ContainedOperationModes, "mode");
        }



        #region Iterators
        // enumerate Capabilities
        internal IEnumerable<Capability> ContainedCapabilities
        {
            get
            {
                for (int i = 0; i < this.capabilities.Count; i++)
                {
                    if ((this.capabilities[i] is Capability) && (this.capabilities[i].Included == true))
                    {
                        yield return this.capabilities[i] as Capability;
                    }
                }
            }
        }
        // enumerate modes
        internal IEnumerable<OperationMode> ContainedOperationModes
        {
            get
            {
                for (int i = 0; i < this.capabilities.Count; i++)
                {
                    if ((this.capabilities[i] is OperationMode) && (this.capabilities[i].Included == true))
                    {
                        yield return this.capabilities[i] as OperationMode;
                    }
                }
            }
        }
        #endregion;
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.