Lista part.3 – Dodawanie Elementów.

Lista part.3 – Dodawanie Elementów

Powiedzmy, że chcemy przechowywać na liście miesiące, więc musimy do niej dodać elementy i robimy to po prostu wywołując metodę o nazwie add().
Zróbmy to teraz.

List<string> months = new List<string>();
months.Add("January");        	     
months.Add("February");
months.Add("March"); 			
months.Add("April");
months.Add("May");			
months.Add("June");
months.Add("July");			
months.Add("August");
months.Add("September");		
months.Add("October");
months.Add("November"); 		
months.Add("December");

Podczas korzystania z listy dodawanie to najczęstszy scenariusz. Wspomnę jednak, że jeśli przed utworzeniem instancji listy wiesz, jakie elementy musi ona zawierać, możesz nadal używać inicjalizatora kolekcji.

Inicjalizator kolekcji sprawia, że Twój kod jest bardziej zwięzły. Pozwala on też połączyć tworzenie listy z dodaniem początkowego zestawu elementów. Zmodyfikuje teraz kod, aby to pokazać.

List<string> months = new List<string>
{
   "January", "February", "March",
   "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December",
};

Jest to prawie identyczne z inicjalizatorem tablicy, który widzieliście na początku przy omawianiu tablic, z elementami które mają znaleźć się na liście w nawiasach klamrowych.

Istnieje niewielka różnica w porównaniu z listami, dla wszystkich innych kolekcji oprócz tablic, musisz wyraźnie wpisać „new” po prawej stronie deklaracji, co jak pamiętacie było opcjonalna dla tablic w przypadku nawiasów klamrowych.

string[] monts =
{
   "January", "Febrry", "March",
   "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December",
};

Nawiasem mówiąc, jestem pewien, że zdaliście sobie sprawę, że kolekcja o stałym rozmiarze, taka jak miesiące, jest znacznie bardziej odpowiednia dla tablicy niż listy.
Ten kod ma po prostu pokazać, jak pracować z listami.

Ponadto ten inicjalizator kolekcji sprawia, że wygląda na to, że tworzysz listę z danymi w niej zawartymi, ale nie daj się zwieść. To tylko skrót syntaktyczny.

Pod maską kompilator po prostu zamieni inicjalizator w kod, który tworzy instancję pustej listy, a następnie wielokrotnie wywołuje metodę add() w celu dodania określonych elementów.

Całościowe omówienie tematu kolekcji (tablica, lista, kolejka, stos, słownik).
Wiele przydatnych wskazówek i dobrych praktyk dostępne jest na kursie:

https://dev-hobby.pl/kursy/c-wprowadzenie-do-kolekcji/

33 comments

  1. Right away I am going away too do myy breakfast, afterward having my breakfast coming yet again to read further news. Sofia Orbadiah Jaela

  2. I like this blog very much so much great information. Athene Ricky Shaff

  3. I really liked your blog post. Much thanks again. Really Cool. Faunie Gonzalo Davita

  4. Some really nice and useful information on this site, as well I believe the design and style contains great features. Viviana Sterne Gannes

  5. Really appreciate you sharing this article. Really thank you! Cool. Blithe Demetre Morris

  6. Thanks for fantastic info I was looking for this info for my mission. Vivianne Tore Bensky

  7. Great delivery. Great arguments. Keep up the great effort. Linette Shae Chappell

  8. Good day! I simply would like to give an enormous thumbs up for the great information you will have right here on this post. I will probably be coming back to your blog for extra soon. Peggi Drud Giorgia

  9. Its fantastic as your other articles :D, thanks for putting up. Aubree Irving Croft

  10. This is my first time pay a visit at here and i am truly pleassant to read all at one place. Neille Man Esmaria

  11. Really appreciate you sharing this blog post. Will read on Adrea Niel Lovmilla

  12. I blog quite often and I really thank you for your information. Your article has truly peaked my interest. I will take a note of your website and keep checking for new information about once a week. I opted in for your Feed as well. Halley Rodrique Hynda

  13. Hi there friends, its great post concerning teachingand completely explained, keep it up all the time. Kai Gibb Muir

  14. I like a very useful article, I like our page and follow it Ysabel Sollie Sosthena

  15. Well I definitely liked reading it. This subject procured by you is very constructive for accurate planning. Nessy Stearne Lidda

  16. Thanks for your own work on this website. My mum takes pleasure in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very important tips and hints via your website and therefore strongly encourage response from visitors on this subject matter while our own simple princess is without a doubt understanding so much. Take pleasure in the rest of the year. Your performing a useful job. Sophey Weber Harrus

  17. Every weekend i used to pay a visit this web site, for the reason that i want enjoyment, since this this site conations actually nice funny information too. Bidget Beltran Emarie

  18. I like reading through a post that can make people think. Also, thank you for permitting me to comment! Mellisa Jan Hilaria

  19. I like the helpful information you supply on your articles. Gretel Leonid Rosenbaum

  20. Amazing! Its actually amazing piece of writing, I have got much clear idea on the topic of from this post. Esther Fin Blancha

  21. Hello to all, how is all, I think every one is getting more from this web site, and your views are pleasant in support of new people. Lind Valle Polivy Cordelie Ingrim Kucik

  22. But wanna remark on few general things, The website design and style is perfect, the subject matter is very superb : D. Xena Granthem Aurora

  23. thanks for providing such helpful data. Jordana Pietrek Stahl Karlen Arlen Elberta

  24. Attractive component of content material. I merely stumbled upon your internet internet site and in accession capital to assert that I acquire in fact appreciated account your blog website posts. Any way I will end up being subscribing in your feeds or even I achievement you get right of entry to continuously quickly. Netti Sid Steve

  25. Pretty! This was an incredibly wonderful article. Thank you for supplying this info. Clarice Isidoro Nobell

  26. wow, awesome article post. Really looking forward to read more. Cool. Gaye Bord Cormier

  27. Looking forward to reading more. Great post. Cool. Ilka James Ger

  28. Hi there, You have performed a great job. I will certainly digg it and for my part suggest to my friends. I am sure they will be benefited from this web site.| Valentia Gilburt Hadwyn

  29. Wonderful article! We will be linking to this particularly great content on our site. Keep up the great writing. Carlotta Truman Ramin

  30. I really like it whenever people come together and share thoughts. Great site, keep it up! Jannelle Templeton Iow

  31. Youre so cool! I dont suppose Ive learn anything like this before. So nice to seek out someone with some authentic ideas on this subject. realy thank you for beginning this up. this web site is something that is needed on the web, someone with a little originality. useful job for bringing one thing new to the internet! Ursula Sylas Franciska

  32. Appreciate it for helping out, fantastic information. Natassia Mendel Ari

  33. Hello mates, nice piece of writing and good urging commented here, I am in fact enjoying by these. Lucina Nat Zalucki

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *