05 1 using System using System Collections Generic
- Slides: 43
05장. 메소드와 인자 1. 메소드의 정의와 호출 메인 함수의 해석 using System; using System. Collections. Generic; using System. Text; namespace Console. Application 1 { class Program { static void Main(string[] args) { Console. Write. Line("Hello. . ? "); } } static : 접근 지정자 } void : 리턴형(리턴값 없음) • static Main()함수가 정적 메소드란 의미 c# Main : 함수명 (string[] args) : 인자목록 Console. Write. Line(“Hello…? ”) : 명령문 6 / 29
05장. 메소드와 인자 1. 메소드의 정의와 호출 메소드 실행하기 1. using System; 2. namespace Console. Application 3. { 4. public class Example 5. { 6. static void Method. Test() 7. { 8. Console. Write. Line("여기는 Method. Test 입니다. "); 9. } 10. static void Main(string[] args) 11. { 12. Console. Write. Line("여기는 Main 메소드입니다. "); 13. Method. Test(); 14. System. Console. Write. Line("Method. Test 메소드를 호출하였습니다. "); 15. } 16. } 17. } c# 7 / 29
05장. 메소드와 인자 1. 메소드의 정의와 호출 메소드 호출 1. using System; 2. namespace Method. Example 3. { 4. public class A 5. { 6. public static void Method. A() 7. { 8. System. Console. Write. Line("Method. A() in class A"); 9. } 10. } 11. public class B 12. { 13. public static void Main(string[] args) 14. { 15. A. Method. A(); 클래스 A의 Method. A() 호출 16. } 17. } 18. } c# 9 / 29
05장. 메소드와 인자 1. 메소드의 정의와 호출 중첩 메소드 사용 1. using System; 2. namespace Nested. Method. Example 3. { 4. public class Nested. Method 5. { 6. public static void Method. A() 7. { 8. System. Console. Write. Line("메소드 A입니다. "); 9. } 10. public static void Method. B() 11. { 12. Method. A(); 13. System. Console. Write. Line("메소드 B입니다. "); 14. Method. A(); 15. } 16. public static void Main(string[] args) 17. { 18. Method. B(); 19. Method. A(); 20. } 21. } 22. } c# 11 / 29
05장. 메소드와 인자 1. 메소드의 정의와 호출 재귀메소드 사용(팩토리얼 구하기) using System; namespace Recursion. Method { public class Recursion. Method { public static ulong Factorial(ulong number) { if (number <= 1) return 1; else return number * Factorial(number - 1); } public static void Main(string[] args) { ulong nfact = Factorial(5); Console. Write. Line("5 * 4 * 3 * 2 * 1 = " + nfact); } } } c# 14 / 29
05장. 메소드와 인자 2. 메소드와 변수 지역변수 예제 1. namespace Var 1 2. { 3. using System; 4. public class Local. Var 5. { 6. public static void Main(string[] args) 7. { 8. int local. Var = 10; 9. int un. Init. Var; 10. Console. Write. Line(local. Var); 11. Console. Write. Line(un. Init. Var); //에러 발생! 12. } 13. } 14. } 지역변수를 초기화 하지 않음 메소드 내에서 사용하는 모든 지 역 변수는 초기화 하여야 함. c# 16 / 29
05장. 메소드와 인자 2. 메소드와 변수 클래스 변수 사용 예제 1. using System; 2. public class Example 3. { 4. public static int classvari = 0; 5. 6. 7. 8. public Example() { classvari++; } 9. public static void Main(string[] args) 10. { 11. Example e 1 = new Example(); 12. Example e 2 = new Example(); 13. Console. Write. Line(Example. classvari); 14. } 15. } c# 18 / 29
05장. 메소드와 인자 2. 메소드와 변수 인스턴스 변수 클래스 멤버로 선언되어 객체가 생성될 때마다 메모리가 배정되는 변수 using System; public class Example { public int vari = 0; public Example() { vari++; } public static void Main(string[] args) { Example e 1 = new Example(); Example e 2 = new Example(); Console. Write. Line(e 1. vari); Console. Write. Line(e 2. vari); } } c# 19 / 29
05장. 메소드와 인자 3. 메소드 인자 전달하기 인자 전달 방법 1 값에 의한 전달(인자가 값형 변수일 때) namespace Param 1 { using System; public class Param. Value { public void Increase(int n) { n++; Console. Write. Line("호출 후 n : {0} ", n); } public static void Main(string[] args) { int i = 10; Param. Value pv = new Param. Value(); Console. Write. Line("호출 전 i : {0} ", i); pv. Increase(i); Console. Write. Line("호출 후 i : {0}", i); } } c# } 22 / 29
05장. 메소드와 인자 인자 전달 방법 2 3. 메소드 인자 전달하기 값에 의한 전달(인자가 참조형 변수일 때) namespace Param 2 1. var. Ref ==> (매개변수) Param. Ref. var. Ref <==풍선 도움말 2. my. Val ==> int Param. Ref. my. Val <== 풍선 도움말 { using System; 3. Increase() 메소드를 통해 인자로 넘어온 Param. Ref의 public class Param. Ref 클래스형 변수 var. Ref의 값을 1 만큼 증가 시킨다. { public int my. Val = 10; } 4. 참조형 변수인 var. Ref는 인자인 pr 이 저장된 메모리를 참조한다. public class Param. Test 5. var. Ref. my. Val이 1만큼 증가하면 pr. my. Val도 1 증가 { public static void Increase(Param. Ref var. Ref) 인자 : Param. Ref 클래스형 변수 var. Ref { var. Ref. my. Val++; Console. Write. Line("호출 후 var. Ref. my. Val : {0}", var. Ref. my. Val); } Param. Ref 클래스형 변수 pr를 생성 public static void Main(string[] args) pr은 특정 값을 포함한 변수가 아닌 { Param. Ref 클래스를 참조하는 참조형 변수 Param. Ref pr = new Param. Ref(); Console. Write. Line("호출 전 pr. my. Val : {0}", pr. my. Val); Param. Test. Increase(pr); //pr을 인자로 Increase() 메소드 호출 Console. Write. Line("호출 후 pr. my. Val : {0}", pr. my. Val); } } } c# 23 / 29
05장. 메소드와 인자 3. 메소드 인자 전달하기 인자 전달 방법 3(방법 2와 같은 원리) 참조에 의한 전달(인자가 값형 변수일 때) 전달하는 인자의 변수명 앞에 ref 키워드를 사용 namespace Param 3 { using System; public class Param. Value { public void Increase(ref int n) { n++; } public static void Main(string[] args) { int i = 10; Param. Value pv = new Param. Value(); Console. Write. Line("호출 전 : {0}", i); pv. Increase(ref i); Console. Write. Line("호출 후 : {0}", i); } } c#} 25 / 29
05장. 메소드와 인자 3. 메소드 인자 전달하기 인자 전달 방법 4 참조에 의한 전달(인자가 참조형 변수일 때) 인자로 사용할 변수가 참조형 변수이고, 전달 방식도 참조에 의한 전달방식 namespace Param 4 { using System; public class Param. Ref { public int my. Val = 10; } public class Param. Test { public static void Increase(ref Param. Ref var. Ref) { var. Ref. my. Val++; } public static void Main(string[] args) { Param. Ref pr = new Param. Ref(); Console. Write. Line("호출 전 : {0}", pr. my. Val); Param. Test. Increase(ref pr); Console. Write. Line("호출 후 : {0}", pr. my. Val); } } } c# 26 / 29
05장. 메소드와 인자 3. 메소드 인자 전달하기 출력인자 방식의 인자 전달 namespace Param 5 { using System; public class Param. Value { public void Increase(out int n) { n = 11; } public static void Main(string[] args) { int i; // n 이 항상 11인 것을 호출할 경우에 초기화 필요없다. Param. Value pv = new Param. Value(); pv. Increase(out i); Console. Write. Line("호출후 값 : {0}", i); } } } c# 29 / 29
05장. 메소드와 인자 3. 메소드 인자 전달하기 가변길이 인자 방식 인자 전달 namespace Param 6 { using System; public class Variable. Length { public int Var. Method(params int[] arr) { int sum = 0; for (int i = 0; i < arr. Length; i++) { sum = sum + arr[i]; } return sum; } public static void Main(string[] args) { int result; Variable. Length vl = new Variable. Length(); result = vl. Var. Method(10, 20); // 인자 2개 Console. Write. Line(result); result = vl. Var. Method(1, 2, 3, 4); // 인자 4개 Console. Write. Line(result); } } c#} 31 / 29
05장. 메소드와 인자 4. 메소드 오버로딩 클래스 하나 안에서 같은 이름의 메소드를 여러 개 선언 메소드 구분 규칙 static void test. Method{. . . } static void test. Method(int n) {. . . } static void test. Method(int if, int j) {. . . } public static void Main(string[] args) { test. Method(3); } c# 32 / 29
05장. 메소드와 인자 4. 메소드 오버로딩 namespace Overloading { using System; public class Overloading { public void Print. Value(int i) { Console. Write. Line("하나의 정수가 인자인 메소드"); } public void Print. Value(int i, int j) { Console. Write. Line("두개의 정수가 인자인 메소드"); } public void Print. Value(double f) { Console. Write. Line("하나의 실수가 인자인 메소드"); } public static void Main(string[] args) { Overloading mo = new Overloading(); mo. Print. Value(3); mo. Print. Value(2. 1); mo. Print. Value(3, 4); } } } c# 33 / 29
05장. 메소드와 인자 값과 참조(보충 자료) • 값 타입과 참조 타입의 대입 ▫ 값 타입의 대입 : 복사가 됨 ▫ 참조 타입의 대입 : 참조자가 하나 더 늘어남(메모리가 추가로 할당되는 것이 아님) using System; class CSTest { static void Main() { int value = 3, vcopy; vcopy = value; vcopy = 4; Console. Write. Line("value={0}, vcopy={1}", value, vcopy); int[] ar, arcopy; ar = new int[] { 1, 2, 3, 4, 5 }; arcopy = ar; arcopy[1] = 1234; Console. Write. Line("ar[1]={0}, arcopy[1]={1}", ar[1], arcopy[1]); } } c# 39 / 29
05장. 메소드와 인자 int[] ar, arcopy; ar = new int[] { 1, 2, 3, 4, 5 }; 값과 참조(보충 자료) ar arcopy = ar; arcopy[1] = 1234; c# ar 1 2 3 4 5 1 1234 3 4 5 null arcopy 40 / 29
- Using system.collections
- Using system collections generic
- Generic rectangle solver
- Pseg credit and collections
- Minimax java
- Collections trust spectrum
- Cert 3 in pathology collection
- Huge collections of stars
- Java collections overview
- Are all the methods in the collections class static?
- Chapter 20 patient collections and financial management
- Object model and collections in dhtml
- C.tech collections
- Crf sap
- Java collections framework diagram
- Collection hierarchy java
- Financial data systems debt collector
- Fungsi movie task pane
- Abstract data type (adt)
- Collection of specialized cells and cell products
- Musnad collections of hadith
- Jisc collections manager
- Ucla special collections
- Aba therapy billing and collections
- Shared services alaska
- Import java.util.* class test collection 15
- Google guava collections
- Fms collections
- Collections
- Herbivore
- Sruthika collections
- Williams fudge collections
- Digbio
- Collections
- Observable collections
- Brown bin tonbridge and malling
- Revenue cycle sales to cash collections
- Arm industry collections
- Bookmapweb
- Benchmarks in collections care
- Neuronal processes
- Dda ucsd
- Collections iterable
- Revenue cycle sales to cash collections