csharp 使用列表和代理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用列表和代理相关的知识,希望对你有一定的参考价值。

<span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>            
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">// Example list </span>
        <span style="color: #008080; font-style: italic;">//</span>
        List&lt;Person&gt; Persons = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Jonas&quot;</span>, <span style="color: #FF0000;">22</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Peter&quot;</span>, <span style="color: #FF0000;">19</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Daniel&quot;</span>, <span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Yvonne&quot;</span>, <span style="color: #FF0000;">18</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Jana&quot;</span>, <span style="color: #FF0000;">26</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Removes all persons younger than 20 years</span>
        Persons.<span style="color: #0000FF;">RemoveAll</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Person P<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>P.<span style="color: #0000FF;">Age</span> &lt; <span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Searches for specific entries and returns a list of</span>
        <span style="color: #008080; font-style: italic;">// them</span>
        List&lt;Person&gt; NewList = Persons.<span style="color: #0000FF;">FindAll</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Person P<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>P.<span style="color: #0000FF;">Age</span> &gt; <span style="color: #FF0000;">25</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Loops trough every item and modifies the</span>
        <span style="color: #008080; font-style: italic;">// entries</span>
        Persons.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Person P<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            P.<span style="color: #0000FF;">Name</span> = <span style="color: #808080;">&quot;&lt;&quot;</span> + P.<span style="color: #0000FF;">Name</span> + <span style="color: #808080;">&quot;&gt;&quot;</span>;
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Returns true or false if a specific entry is found</span>
        <span style="color: #FF0000;">bool</span> Contains = Persons.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Person P<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>P.<span style="color: #0000FF;">Age</span> &gt; <span style="color: #FF0000;">25</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Print the new list</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Person PersonObject <span style="color: #0600FF;">in</span> NewList<span style="color: #000000;">&#41;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>PersonObject<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Example class</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> name = <span style="color: #808080;">&quot;&quot;</span>;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> name;<span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> name = value;<span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> age = <span style="color: #FF0000;">0</span>;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Age
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> age; <span style="color: #000000;">&#125;</span>
        set <span style="color: #000000;">&#123;</span> age = value; <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>	
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Constructor</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;Name&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;Age&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF;">public</span> Person<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Name, <span style="color: #FF0000;">int</span> Age<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">name</span>   = Name;
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">age</span>    = Age;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Overrides the default ToString method with</span>
    <span style="color: #008080; font-style: italic;">/// a custom one</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0} ({1})&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Name</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Age</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>

以上是关于csharp 使用列表和代理的主要内容,如果未能解决你的问题,请参考以下文章

csharp 使用简单代理的示例服务类

csharp WebApi ActionFilter与Service Fabric反向代理或ServiceFabric.AutoRest结合使用,以指示RESTfull 404响应。

csharp C#使用Lambda表达式为动态类型和动态属性排序通用列表

csharp 设计图案结构代理

csharp tcp代理

csharp 从AD #exchange下载所有代理地址