Zināšanu bāze

IEnumerable<T>

.NET Framework 1 veidot metodes, kurām tiek padots noteikta tipa masīvs, kā arguments var 2 veidots:  

public decimal Sum (decimal[] amounts)
{

decimal result = 0;

foreach (decimal a in amounts){
result += a;

}

return result;

}

vai       

public decimal Sum (params decimal[] amounts) 
{           
decimal result = 0;
foreach (decimal a in amounts) 
{               
result += a;           
} return result;       
}

 

Tas radija daudz lieku konstrukciju darbā ar sarakstiem.
Piemēram, lai izsauktu metodi „Sum” jāraksta sekojošas darbības.

ArrayList list = new ArrayList ();

// aizpilda masīvu ar vērtībam.

 

Sum ((decimal[])list.ToArray (typeof (decimal)));

 

 

Sākot ar Microsoft.NET Framework 2.0 pavērās lieliska iespēja uzlabot koda kvalitāti un lasāmību izmantojot „Generic” iespējas. Ar minimālam izmaiņām samazinās koda sarežģītības pakāpe.

 

public decimal Sum (IEnumerable<decimal> amounts){
decimal result = 0;foreach (decimal a in amounts){
result += a;
}return result;

}

 

Koda izsaukums izskatās tā 

List<decimal> list = new List<decimal> ();

// aizpilda masīvu ar vērtībam, 100% garantija, ka masīvs satur mums nepiciešamo tipu.

Sum (list);

 

 

Saglabājas iespēja padot vērtības arī kā masīvu

 

... 

Sum (new decimal[] {1.8M, 2M, 1.2M});

...

 


Posted Aug 09 2007, 12:10 PM by andrejs.mamontovs
Filed under:

Comments

mihails golubevs wrote re: IEnumerable<T>
on 08-09-2007 15:42

Andrējs, tu pat nevari iedomāties cik sēn šī fiča jau ir realizēta citās valodās. Šo rakstu vajadzēja apsaukt "Nu beidzot M$ realizēja sen gaidīto fiču!" :)

andrejs.mamontovs wrote re: IEnumerable<T>
on 08-09-2007 16:25

Piekrītu. Nevienam jau nav jaunums, ka C#’am ir daudz ierobežojumu. Bet biznesa prasībām ļoti piemērota valoda.

Cheap Seo Service wrote re: IEnumerable<T>
on 06-29-2012 22:33

iTkHDU I really like and appreciate your blog post.Really looking forward to read more. Want more.

Cheap Seo Services wrote re: IEnumerable<T>
on 08-07-2012 2:25

l6FnXP Thank you ever so for you article.Really looking forward to read more. Keep writing.

cheap bookmarking service wrote re: IEnumerable<T>
on 09-12-2012 6:16

4DTuwr Im grateful for the article.Really thank you! Want more.

crork wrote re: IEnumerable<T>
on 11-07-2012 15:57

FzPAyE Major thankies for the blog post.Really looking forward to read more. Great.

buy viagra wrote re: IEnumerable<T>
on 02-03-2013 1:36

72GmIx Really enjoyed this article post.Really thank you! Really Cool.

archeage gold wrote re: IEnumerable<T>
on 02-27-2013 15:41

Definitely, what a fantastic blog and revealing posts, I definitely will bookmark your website.All the Best! archeage gold www.cheap-archeage-gold.com

Social bookmarks wrote re: IEnumerable<T>
on 03-24-2013 21:48

l9XWjW Major thanks for the article post.Really thank you! Great.

Add a Comment

(obligāts)  
(brīvizvēles)
(obligāts)  
Remember Me?