수업 011 (04.20 2020) 복습 - array

2020. 4. 20. 10:11
728x90

1. App.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._012
{
    class App
    {
        public App()
        {
            // 길이가 3인 int형 배열을 선언하고 초기화 (price)
            int[] arrPrices = new int[3];
            // 인덱스 0~3 배열의 마지막 인덱스까지 값을 할당
            arrPrices[0= 500;
            arrPrices[1= 300;
            arrPrices[2= 400;
            // for, foreach 문을 사용해서 배열의 요소의 값을 출력
            for (int i = 0; i < arrPrices.Length; i++)
            {
                Console.WriteLine("Price: " + arrPrices[i]);
            }
            Console.WriteLine();
 
            foreach (int productPrice in arrPrices)
            {
                Console.WriteLine("Price: " + productPrice);
            }
            Console.WriteLine();
 
            // 길이가 3인 string 형 배열을 선언하고 초기화 (product name)
            string[] arrProductNames = new string[3];
            // 인덱스 0~3 배열의 마지막 인덱스까지 값을 할당
            arrProductNames[0= "과자";
            arrProductNames[1= "젤리";
            arrProductNames[2= "초코";
            // for, foreach 문을 사용해서 배열의 요소의 값을 출력
            for (int i = 0; i < arrProductNames.Length; i++)
            {
                Console.WriteLine("Product Name: " + arrProductNames[i]);
            }
            Console.WriteLine();
 
            foreach (string productName in arrProductNames)
            {
                Console.WriteLine("Product Name: " + productName);
            }
            Console.WriteLine();
 
 
            // 길이가 3인 Ptodcut 배열을 선언하고 초기화
            Product[] arrProducts = new Product[3];
            // Product 클래스 멤버 (name, price)
            // 인덱스 0~ 배열의 마지막 인덱스 까지 값 (Product객체)을 할당
            for (int i = 0; i < arrProductNames.Length; i++)
            {
                int price = arrPrices[i];
                string name = arrProductNames[i];
 
                ProductData data = new ProductData(name, price);
 
                arrProducts[i] = new Product(data);
            }
            //객체로부터 분리
 
            // for, foreach 문을 사용해서 배열의 요소의 값을 출력
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

2. Product.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._012
{
    class Product
    {
        public ProductData data;
 
        //생성자
        public Product(ProductData data)
        {
            this.data = data;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

3. ProductData.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._012
{
    class ProductData
    {
        public int price;
        public string name;
 
        public ProductData(string name, int price)
        {
            this.name = name;
            this.price = price;
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
728x90

'C# > Study' 카테고리의 다른 글

수업 012 (04.21 2020) List<> 복습  (0) 2020.04.21
수업 011 (04.20 2020) List<>  (0) 2020.04.21
수업 010 (04.17 2020) Array  (0) 2020.04.17
수업 010 (04.17 2020) Array - Food  (0) 2020.04.17
수업 010 (04.17 2020) Array - Burger  (0) 2020.04.17

BELATED ARTICLES

more