728x90
List.Add() Method
The Add method adds an item to a List. The following code snippet creates a List and adds items to it by using the Add method.
// Create a list
List<string> AuthorList = new List<string();
// Add items using Add method
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
Authㅊ#orList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");
---------------------------------------------
List<>
// C# program to add element in List<T> using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { // Creating a List of integers List<int> firstlist = new List<int>(); // adding elements in firstlist for (int i = 4; i < 10; i++) { firstlist.Add(i * 2); } // Displaying elements of firstlist // by using foreach loop foreach(int element in firstlist) { Console.WriteLine(element); } } } Output: 8 10 12 14 16 18 |
------------------
List<> : 상속, 개체
public class Store { public List<item> Inventory = new List<item>(); //스토어의 생성자 : Constructor public Store() // Inventory 안의 Food를 만듬. Food는 Item에 상속되어있음. { Inventory.Add(new Food("vega mega", "salad", "3")); Inventory.Add(new Food("makes you healty", "sandwich", "4" )); Inventory.Add(new Food("welldone", "bread", "10")); } public void ShowInventory() { int x = 0; foreach (var i in Inventory) { Console.WriteLine("{0} {1} : ${2}", Inventory[x].description, Inventory[x].name, Inventory[x].price); x++; } } Output: vega mega salad 3 makes you healty sandwich 4 welldone bread 10 |
상속 public class Item { public string description; public string name; public string price; public Item() { } public void About() { Console.WriteLine("{0} : {1}, {2}", name, price, description); } } public class Food : Item { //상속된 클래스의 개별 값 (List 에는 사용되지 않았음) int amountounces; internal Array ingredients; public Food(string description, string name, string price) { base.description = description; base.name = name; base.price = price; } public Food() { } } |
반응형
'💻 소프트웨어 Tutorial 모음 > C# 비주얼스튜디오' 카테고리의 다른 글
더블소수점, c# (0) | 2021.07.19 |
---|---|
상속, 예문, c# (0) | 2021.07.19 |
Timer/Stopwatch/타이머 (0) | 2021.07.19 |
Xaml, tool box 도구 추가하기 (0) | 2021.07.19 |
접근 한정자 Access Modifier 종류 (0) | 2021.07.19 |