Zināšanu bāze

Code complete (IEnumerable.Count)

Labs vakars,

Darbojos pie liela projekta uzturēšanas. Patīkami, ka spējam to arī tehnoloģiski bīdīt uz priekšu, savādāk būtu pavisam skumīgi. Tā nu sanāk, ka jau tagad varu pielietot .NET 3.5, un mācīties to optimāli izmantos.

Šoreiz viens vienkāršots bet visai tuvs realitātei piemērs.

Problēma: Ir masīvs ar skaļiem, jāsaskaita cik reizes atkārtojās piemēram 2.

var arr = new[] {1, 1, 2, 2, 2, 3, 3, 3};

Vien no klasiskiem realizācijas piemēriem

            int c = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i]== 2)
                {
                    c++;
                }
            }
            Console.WriteLine(c);

Var izdomāt vel dažus variantus, bet katrs no tiem nav maksimāli īss. Pastāv reāla kļūdas varbūtību:)

Un šinī gadījumā palīgā nāk paplašinošā metode Count vārdu telpā System.Linq. Metodi var pielietot visiem objektiem, kas manto IEnumerable interfeisu. Pēc noklusējuma metode saskaita visus elementus, bet ir iespēja padot delegātu, kas filtrēs masīva elementus. Vēlamo rezultātu var iegūt uzrakstot šādu izteiksmi:

Console.WriteLine(arr.Count(i => i == 2));

Cerams noderēs!


Posted Nov 13 2008, 01:34 PM by andrejs.mamontovs

Comments

Konstantīns wrote re: Code complete (IEnumerable.Count)
on 11-13-2008 17:55

Interesants piemērs. Paldies. Vai tu nezini vai šo var arī panākt ar "lambda" izteiksmi?

andrejs.mamontovs wrote re: Code complete (IEnumerable.Count)
on 11-13-2008 22:10

Mazliet nesaprotu tava jautājuma būtību! Kā reiz, šeit jau tiek izmantota lamda, ka metodes arguments tiek padota izteiksme (i=>i==2).

m4V8KKMr wrote re: Code complete (IEnumerable.Count)
on 03-23-2012 9:05
Mike wrote re: Code complete (IEnumerable.Count)
on 03-23-2012 17:48
gordon wrote re: Code complete (IEnumerable.Count)
on 07-10-2012 9:48
john wrote re: Code complete (IEnumerable.Count)
on 07-11-2012 18:05
Cheap Seo Services wrote re: Code complete (IEnumerable.Count)
on 07-14-2012 1:18

B7ykaZ Very good article. Fantastic.

Article submission wrote re: Code complete (IEnumerable.Count)
on 08-07-2012 21:32

MGVBhJ Im obliged for the blog. Want more.

bookmarking service wrote re: Code complete (IEnumerable.Count)
on 09-24-2012 14:39

vE8FTN Thanks so much for the blog post. Cool.

Mariana wrote re: Code complete (IEnumerable.Count)
on 10-07-2012 1:25

Home run! Great slugging with that asnewr!

nike damenschuhe wrote re: Code complete (IEnumerable.Count)
on 11-20-2012 7:46

Relationships last every time each and every one relative says as well as a slight brilliance across the various.

nike damenschuhe www.nikefreerundamendes.com

Fiverr best selles wrote re: Code complete (IEnumerable.Count)
on 11-29-2012 12:33

pWnVOl Fantastic post.Thanks Again. Want more.

nike 6.0 schuhe wrote re: Code complete (IEnumerable.Count)
on 01-10-2013 18:09

Fancy is really frail during commence, about the expands more substantial as they age with the price of correctly given.

nike 6.0 schuhe www.nikeschuhedamendes.com

destockchine femme wrote re: Code complete (IEnumerable.Count)
on 01-10-2013 18:50

Really enjoy certainly is the athletic point of interest to the your life and therefore the increase of whatever we each absolutely adore.

destockchine femme http://www.destockchinefr.fr/

nike tn pas cher wrote re: Code complete (IEnumerable.Count)
on 01-11-2013 1:10

Around the globe that you are anyone, nonetheless to a single man that you are the.

nike tn pas cher http://www.robenuk.eu/

casquette obey wrote re: Code complete (IEnumerable.Count)
on 02-26-2013 19:21

A mate which you just pay for with the shows will likely to be purchased from an individual. casquette obey http://www.b66.fr/

sarenzalando wrote re: Code complete (IEnumerable.Count)
on 03-02-2013 23:18

Prefer could possibly be the stimulated problem within the existence along with the development of that which i really enjoy. sarenzalando http://www.ckguccifr.fr/

casquette snapback wrote re: Code complete (IEnumerable.Count)
on 03-14-2013 13:55

Don't make friends in which are enjoyable to be with. Connect with others which will enforce one particular jimmy your away. casquette snapback http://www.a77.fr/

casquette new era wrote re: Code complete (IEnumerable.Count)
on 03-15-2013 19:16

Around the world may well anyone, however to one specific may well the. casquette new era http://www.a44.fr/

http://www.i77.fr/sport-casquette-snapback-casquette-obey-c- wrote re: Code complete (IEnumerable.Count)
on 03-16-2013 19:34

Due to the fact a person doesn‘w not accept you how to want them for you to,doesn‘w not indicate these folks get into‘w not accept you operating they have perhaps. casquette obey www.i77.fr/sport-casquette-snapback-casquette-obey-c-7_168_185.html

Gov Backlinks wrote re: Code complete (IEnumerable.Count)
on 03-21-2013 9:55

I like material like this. This is a great article and I really enjoyed reading it. You have an original style that makes your ideas stand out from other writers.

Buy Gov Backlinks wrote re: Code complete (IEnumerable.Count)
on 03-21-2013 16:11

Awesome blog post.Really looking forward to read more. Fantastic.

casquette volcom wrote re: Code complete (IEnumerable.Count)
on 03-22-2013 8:43

Won't discuss about it all of your joy and happiness to 1 a smaller amount of endowed rather than oneself. casquette volcom http://f22.fr/

new era casquette wrote re: Code complete (IEnumerable.Count)
on 03-23-2013 8:53

Accord relates to the goldthread which in turn scarves the particular paper hearts of all of the domain. new era casquette http://e55.fr/

coachoutletcoupon88.com wrote re: Code complete (IEnumerable.Count)
on 04-05-2013 8:04

Someone you spend money on that have gifts is definitely bought from you'll. coachoutletcoupon88.com www.coachoutletcoupon88.com

casquette chicago bulls wrote re: Code complete (IEnumerable.Count)
on 04-06-2013 0:49

Put on‘t look at overtime, the top objects appear anytime you the very least expect these to. casquette chicago bulls http://c99.fr/

g88.fr wrote re: Code complete (IEnumerable.Count)
on 04-06-2013 20:22

Really do not make friends which are functional to get along with. It's the perfect time who'll amount of force want you to jimmy one self it. g88.fr http://www.g88.fr/

coach outlet orlando wrote re: Code complete (IEnumerable.Count)
on 04-07-2013 0:56

At any time you should keep answer right from an opponent, explain to the application due to this cause someone. coach outlet orlando www.coachoutletstore22.com

coachoutlet77.com wrote re: Code complete (IEnumerable.Count)
on 04-07-2013 21:50

Take pleasure in could be the no more than happy and even passable answer to the problem from your reputation. coachoutlet77.com http://www.coachoutlet77.com/

maillot de bain grande taille wrote re: Code complete (IEnumerable.Count)
on 04-12-2013 12:34

Take care not to discuss about it your cheer one substantially less happi as compared with your. maillot de bain grande taille http://www.colundi.com/

camera buying guide wrote re: Code complete (IEnumerable.Count)
on 05-14-2013 0:43

aP6T2U Very neat blog.Really looking forward to read more. Want more.

insanity workout at walmart wrote re: Code complete (IEnumerable.Count)
on 05-18-2013 14:51

thank you for share!

gucci outlet store wrote re: Code complete (IEnumerable.Count)
on 05-19-2013 0:40

thanks for share!

seo pack wrote re: Code complete (IEnumerable.Count)
on 05-20-2013 3:59

Sy2Gok This is one awesome post. Keep writing.

Chanel Handtaschen Deutschland wrote re: Code complete (IEnumerable.Count)
on 05-23-2013 1:07

"And that's as simple as that, family is first. And that's what it was when I was growing up. My family, my mom and dad were always there, and they were always present for me.

Add a Comment

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