C # Extension Method

Source: Internet
Author: User

I saw a question on the forum today, which is described as follows:

 

I have a student. CS model.

class student{public string Sname{get;set;}public int Money{get;set;}} 

Existing collection

     Ilist <student> List = new list <student> (); Student S1 = new student {sname = "Stephen Chow", 60 }; student S2 = new student {sname = "Andy Lau", 80 };

--------------------------------------------------------
My intention is as follows:
When I add another person to the List, such as adding {"Wu MengDa", 50}, the set is added normally.
But when I add another Stephen Chow {"Stephen Chow", 10}, the final result is {"Stephen Chow", 70} because the list already has Stephen Chow}

In general, when the same person is met, the money is added, and the number of sets remains unchanged.

 

I can use the C # Extension Method for implementation.

 

 

 

Now the code is recorded here for later viewing

 

 

Static class extensions <br/>{< br/> internal static void addstudent (This list <student> list, student item) <br/> {<br/> Student temp = List. singleordefault (S => S. sname = item. sname); <br/> If (temp = NULL) list. add (item); <br/> else temp. money + = item. money; <br/>}< br/> class student <br/>{< br/> Public String sname {Get; set ;} <br/> Public int money {Get; Set ;}< br/>}< br/> Public partial class csstest: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> var list = new list <student> (); <br/> list. addstudent (New Student {sname = "Stephen Chow", money = 60}); <br/> list. addstudent (New Student {sname = "Andy Lau", money = 80}); <br/> list. addstudent (New Student {sname = "Stephen Chow", money = 10}); <br/> list. foreach (L => response. write (L. sname + "" + L. money); <br/>/* <br/> * result: <br/> * Stephen Chow 70 <br/> * Andy Lau 80 <br/> */<br/>} 

 

 

Use extension methods

My personal experience is convenient because vs smart sensing can also perceive

Note that the small icons of this extension method are different from those of others.

 

 

Msdn reference: http://msdn.microsoft.com/zh-cn/library/vstudio/bb383977.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.