C#/C# Concept
[C#] Out 키워드
군우
2018. 3. 19. 13:00
out
메소드 호출해서 여러개의 반환을 하고 싶을때 사용함.
TryParse() 메서드
int.TryParse() 기본적인 숫자 자료형에 소속되어 있는 클래스 메서드 이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; namespace CSStudy_out { class Program { static void Main(string[] args) { Console.Write("input : "); int output; bool result = int.TryParse(Console.ReadLine(), out output); // 키워드 out을 매개변수 앞에 붙여준다. out output if (result) { Console.WriteLine("show your input: " + output); } else { Console.WriteLine("err, "); } } } } | cs |
이 밑에거는 별 거 없네
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSStudy_outMehtod { class Program { static void Main(string[] args) { int x=0, y = 0; int vx =1, vy = 1; Console.WriteLine("now coordynates : " + x +","+y); NextPositon(x, y, vx, vy, out x, out y); Console.WriteLine("next coordynates : "+ x+","+y); } private static void NextPositon(int x1, int y1, int vx, int vy, out int rx, out int ry) { rx = x1 + vx; ry = y1 + vy; } } } | cs |
메인에서 nextposition 호출할때 매개변수 out x, out y 를 추가함
새 매개변수처럼 사용함.