Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.12.수업내용 본문

C#/수업내용

C# 21.03.12.수업내용

HappyFrog 2021. 3. 12. 10:55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study001
{
    class App
    {
        public App()
        {
            Console.WriteLine("App생성자 호출됨");

            int x = 10;
            PassByValue(ref x);
            Console.WriteLine(x); //20 (PassByValue메서드에서 실행된 a에 대입된 결과값이 다시 호출하는 x로 반환됨)
                        
            PassByReference(ref x);
            Console.WriteLine(x); //23

            int b; // 초기화 필요X
            OutParameter(out b);
            Console.WriteLine(b); //100

            ParamsMethod1(1, 2, 3, 4, 5);
            ParamsMethod2(1, 2, 3, 4, 5);
            Paramsmethod3(1, 2, "홍길동", "임꺽정", new Unit());
            Paramsmethod4(1, 2, "홍길동", "임꺽정", new Unit());
        }

        public void PassByValue (ref int a)
        {
            a += a;            
        }

        public void PassByReference(ref int a)
        {
            a += 3;            
        }

        public void OutParameter(out int a)
        {
            a = 10;
            a *= a;
        }

        public void ParamsMethod1(int a, int b, int c, int d, int e)
        {
            Console.WriteLine(a);
        }

        public void ParamsMethod2(params int[] arr)     //new선언을 통해 Array클래스의 인스턴스가 생성됨                                                    
        {
            Console.WriteLine(arr[0]);                  //ParamsMethod와 비교했을때 Array클래스의 인스턴스중 가장 앞에있는 문자인 a를 불러오려면 a의 인덱스값 0 을 입력하여 호출한다.
        }

        public void Paramsmethod3(params object[] arr)  //arr[0] = 1, arr[2] = 2, arr[3] = "홍길동"...arr[5] = "장길산
        {
            object obj = arr[4];
            Unit unit = (Unit)obj;                      //사용하고싶다면 이처럼 언박싱해야함.
        }
        public void Paramsmethod4(params object[] arr)
        {
            Console.WriteLine(arr[3]);                  //임꺽정
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study001
{
    class App
    {
        //생성자
        public App()
        {
            //정수 배열 변수 선언            
            //정수배열 선언후 값 할당
            //int[] arr = { 1, 2, 3, 4, 5 }; // [1,2,3,4,5] 각 값은 알맞은 인덱스로 호출
            //int[] arr = new int[5]; // [0,0,0,0,0]
            //배열 인스턴스의 0번째 인덱스 변수에 값 할당
            //arr[0] = 2; //[0, 0, 0, 0, 0]에서 [2, 0, 0 , 0, 0]으로 바뀐
            //배열 인스턴스의 0번째 인덱스 변수의 값 출력
            //int a = arr[0];
            //Console.WriteLine(arr[0]); //이하 둘 모두 같은값을 출력.
            //Console.WriteLine(a);
            //배열의 길이 출력
            //Console.WriteLine(arr.Length); //5

            //배열의 마지막 인덱스
            //int lastIndex = arr.Length - 1; //4

            //배열의 마지막 인덱스의 값
            //int b = arr[lastIndex];

            //arr[10] = 1; //(인덱스가 배열 범위를 벗어났습니다.) 에러를 출력함 따라서 배열의 크기 선언하는 순간 고정됨


            //문자열 배열 변수 선언 여기서 반환타입은 string
            //string[] arr2 = new string[5]; //[null, null,..., null] string은 참조형식이기 때문
            //string[] arr2 = { "홍길동", "임꺽정", "장길산" };
            //string[] arr2 = new string[3];

            //Unit 배열 변수 선언 여기서 반환타입은 Unit
            //Unit[] arr3;
            //Unit 배열 변수 선언
            //Unit[] arr3 = { new Unit(), new Unit(), new Unit() };   
            //Unit[] arr3 = new Unit[3]; //참조형식이기 때문에 null


            //bool 배열 변수 선언 여기서 반환타입은 bool
            //bool[] arr4;
            //bool[] arr4 = new bool[2];
            //bool[] arr4 = { true, false }; //bool타입은 기본값 false

            //char 배열 변수 선언 여기서 반환타입은 char
            //char[] arr5;
            //char[] arr5 = new char[3];
            //char[] arr5 = { 'a', 'b', 'c' }; //char값으로 nul이 나와야함 null과는 다른 값. docs 기본값 참고.


            //정수 배열 변수 선언
            int[] arr = new int[5];
            arr[1] = 100; // [0, 100, 0, 0, 0]

            int length = arr.Length;
            //for문을 사용해 배열의 요소의 값 출력, for문은 인덱스를 알기 편함
            for(int i = 0; i < arr.Length; i++) // 오류가 나거나 중간에 끝나지 않게 범위를 arr.Length로 설정
            {
                int num = arr[i];
                Console.WriteLine(num);
            }

            int index = 0;              //foreach로 인덱스값을 알기위한 식1
            foreach (int num in arr)    // 인덱스를 알기 복잡함
            {
                Console.WriteLine(num);
                index += 1;             //foreach로 인덱스값을 알기위한 식1
            }


        }


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study001
{
    class App
    {
        //생성자
        public App()
        {
            //Unit타입 배열 변수 선언
            Unit[] units = new Unit[5]; //전부 null
            for (int i = 0; i < 4; i++)
            {
                //Unit타입 배열 요소에 값 할당
                Unit unit = new Unit(); //Unit클래스 인스턴스 생성
                units[i] = unit;

                //units[i] = new Unit();
            }

            //Unit타입 배열의 길이 출력
            Console.WriteLine(units.Length);      //5

            //Unit 배열의 요소 출력
            //for 문
            for (int i = 0; i < units.Length; i++)
            {
                Unit unit = units[i];
                Console.WriteLine(unit);
            }

            //foreach 문
            foreach (Unit unit in units)
            {
                Console.WriteLine(unit);
            }
        }


    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study02
{
    public class App
    {
        //생성자 
        public App() {
            //int 형식 배열 변수 선언하고 
            //배열의 인스턴스를 생성하고 초기화 
            int[] arr = { 1, 4, 2, 3, 5 };

            //정렬되기 전상태 출력 
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.WriteLine("-------------------------");
            //정렬
            Array.Sort(arr);
            //정렬후 
            //for문을 사용해 배열의 요소를 출력 
            for (int i = 0; i < arr.Length; i++) {
                Console.WriteLine(arr[i]);
            }
            Console.WriteLine("-------------------------");
            //뒤집기 
            Array.Reverse(arr);
            //for문을 사용해 배열의 요소를 출력 
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }



        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study001
{
    
    class App
    {
        public App()
        {
            //Unit타입일뿐 unit변수의 값은 SCV인스턴스이다.
            Unit unit = new SCV();
        }
    }
}

 

'C# > 수업내용' 카테고리의 다른 글

C#21.03.16.수업내용  (0) 2021.03.16
C#21.03.15.수업내용  (0) 2021.03.15
C# 21.03.11.수업내용  (0) 2021.03.11
C# 21.03.10.수업내용  (0) 2021.03.10
C# 0309 하템 스톰사용  (0) 2021.03.09