csharp 使用列表和代理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用列表和代理相关的知识,希望对你有一定的参考价值。
<span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">(</span><span style="color: #FF0000;">string</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span> args<span style="color: #000000;">)</span>
<span style="color: #000000;">{</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<Person> Persons = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<Person><span style="color: #000000;">(</span><span style="color: #000000;">)</span>;
Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</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;">(</span><span style="color: #808080;">"Jonas"</span>, <span style="color: #FF0000;">22</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>;
Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</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;">(</span><span style="color: #808080;">"Peter"</span>, <span style="color: #FF0000;">19</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>;
Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</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;">(</span><span style="color: #808080;">"Daniel"</span>, <span style="color: #FF0000;">20</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>;
Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</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;">(</span><span style="color: #808080;">"Yvonne"</span>, <span style="color: #FF0000;">18</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>;
Persons.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</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;">(</span><span style="color: #808080;">"Jana"</span>, <span style="color: #FF0000;">26</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>;
<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;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span>Person P<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">(</span>P.<span style="color: #0000FF;">Age</span> < <span style="color: #FF0000;">20</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span><span style="color: #000000;">)</span>;
<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<Person> NewList = Persons.<span style="color: #0000FF;">FindAll</span><span style="color: #000000;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span>Person P<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">(</span>P.<span style="color: #0000FF;">Age</span> > <span style="color: #FF0000;">25</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span><span style="color: #000000;">)</span>;
<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;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span>Person P<span style="color: #000000;">)</span><span style="color: #000000;">{</span>
P.<span style="color: #0000FF;">Name</span> = <span style="color: #808080;">"<"</span> + P.<span style="color: #0000FF;">Name</span> + <span style="color: #808080;">">"</span>;
<span style="color: #000000;">}</span><span style="color: #000000;">)</span>;
<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;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span>Person P<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #000000;">(</span>P.<span style="color: #0000FF;">Age</span> > <span style="color: #FF0000;">25</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span><span style="color: #000000;">)</span>;
<span style="color: #008080; font-style: italic;">// Print the new list</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">(</span>Person PersonObject <span style="color: #0600FF;">in</span> NewList<span style="color: #000000;">)</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">(</span>PersonObject<span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>
<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;">{</span>
<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> name = <span style="color: #808080;">""</span>;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name
<span style="color: #000000;">{</span>
get <span style="color: #000000;">{</span> <span style="color: #0600FF;">return</span> name;<span style="color: #000000;">}</span>
set <span style="color: #000000;">{</span> name = value;<span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>
<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;">{</span>
get <span style="color: #000000;">{</span> <span style="color: #0600FF;">return</span> age; <span style="color: #000000;">}</span>
set <span style="color: #000000;">{</span> age = value; <span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">/// <summary></span>
<span style="color: #008080; font-style: italic;">/// Constructor</span>
<span style="color: #008080; font-style: italic;">/// </summary></span>
<span style="color: #008080; font-style: italic;">/// <param name="Name"></param></span>
<span style="color: #008080; font-style: italic;">/// <param name="Age"></param></span>
<span style="color: #0600FF;">public</span> Person<span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> Name, <span style="color: #FF0000;">int</span> Age<span style="color: #000000;">)</span>
<span style="color: #000000;">{</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;">}</span>
<span style="color: #008080; font-style: italic;">/// <summary></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;">/// </summary></span>
<span style="color: #008080; font-style: italic;">/// <returns></returns></span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">(</span><span style="color: #808080;">"{0} ({1})"</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;">)</span>;
<span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>
以上是关于csharp 使用列表和代理的主要内容,如果未能解决你的问题,请参考以下文章
csharp WebApi ActionFilter与Service Fabric反向代理或ServiceFabric.AutoRest结合使用,以指示RESTfull 404响应。