C#

2020.07.13(월) - C# review

J_Bin 2020. 7. 13. 15:23

1-1-1. 메서드란? (기능)

 

형식 : 접근속성, 반환타입, 이름 (매개변수/인자/인수)

{
본문

}

 

메서드 오버로딩 : 메서드의 이름은 같으나 인자가 다르다!

장점 : 명칭을 동일하게 사용하니깐 특정한 기능을 타입별로 구현할때 편리하다.

 

1-1-2. 메서드 오버로딩 반환값 정하는 방식 2가지

2-1-1. 배열의 매개변수의 위치찾기 2가지 방법

namespace _2020._07._13_002
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Array = { 144, 123, 232, 324, 789 };

            int index = System.Array.IndexOf(Array, 324);
            //324의 위치 찾기 1
            Console.WriteLine(index);

            for (int count = 0; count < Array.Length; ++count)
            {   //324의 위치 찾기 2
                if( 324 == Array[count])
                {
                    Console.WriteLine($"324의 위치는 : {count} 이다.");
                    break;
                }

            }
        }
    }
}

 

 

c#은 두가지 방법 다 가능하다!

3-1-1. int[]의 type을 가지는 메서드

int[]라는 type이 잘없기때문에 당황하지 말 것!

namespace _2020._07._13_003
{
    class Program
    {
        //Test라는 메서드가 int[]을 반환type으로 가진다!
        static int[] Test()
        {
            int[] Array = new int[5];

            return Array;

        }

        static void Main(string[] args)
        {
            int[] Ret = Test();
            Console.WriteLine(Ret.Length);

        }
    }
}

 

 

3-1-2. Main메서드에서 int[] 불러오기

Var라는 int[] type을 가지는 변수를 만든다.

그 Var의 길이를 알아본다.

using System;

namespace _2020._07._13_003
{
    class Program
    {
       static void Test(  int[] Var  )
        {
            Console.WriteLine(Var.Length);
        }

        static void Main(string[] args)
        {	
            int[] Array = new int[5];
            //Array배열을 Test메서드에 집어넣는다.
            Test(Array);
        }
    }
}

 

4-1-1. 메인메서드의 인자 (args)

 

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

namespace _2020._07._13_004
{
    class Program
    {
        static void Main(string[] args)
        {
           

        }
    }
}

 

args라는 변수를 갖는 string[] 을 만들어준다.