Program.cs using System; public class 제네릭클래스 { public static void Main() { //기본클래스 메서드 호출 //Hello h = new Hello(); h.SayInt(1234); h.SayStr("안녕"); //제네릭 클래스 호출 Hello hi = new Hello(); hi.Say(1234); Hello hs = new Hello(); hs.Say("안녕"); //생성자 호출 Hello say = new Hello("반갑습니다."); say.Say(); say.SayType(); } } //제내릭 클래스 설계 public class Hello //제내릭 메서드로 설정 { //public void SayInt(int msg) { Console.Wr..
Program.cs using System; public class 연산자오버로드 { public static void Main() { //선언과 동시에 초기화 변환연산자 사용 (int 키워드를 Integer로 모방) //Integer i = new Integer(10); Integer i = 10; //위 라인 대체 Integer j = 20; //Integer j = new Integer(20); Integer k = 0; //Integer k = new Integer(0); //i.Value++; i++; //단항 연산자 오버로드 --j; //Integer k = i + j; //k.Value = i.Value + j.Value; k = i + j; //이항 연산자 오버로드 k = k - i; Co..
네임스페이스인 System.Collections.Generic; 를 선언하게 되면 리스트 제네릭 을 사용할 수 있습니다. 리스트 제네릭 클래스는 어떠한 자료 형태도 다 받아 드릴 수 있어 코드의 간결화로 개발자 입장에서는 유용한 클래스 입니다.사용-예)ArrayList를 대신해서 사용 Program.cs using System; using System.Collections.Generic; public class 리스트제네릭클래스 { static void Main() { //정수형 List su = new List(); su.Add(10); su.Add(20); su.Add(30); for (int i = 0; i < su.Count; i++) { Console.WriteLine("{0}",su[i]); ..
예외처리 Exception : 오류(Error)컴파일(문법) 오류 : 잘못된 타이핑, 잘못된 문법이면 visual stdio가 바로 잡아줌 - 오류 줄일려면 많은 타이핑, 많은 학습런다임(실행) 오류 : 실행시 발생 -많은 테스트논리(잘못된분석) 오류(예외처리) : 잘못된 분석/설계/구현(오탈자) -많은 프로그램 작성 경험 Program.cs using System; public class 예외처리 { static void Main(string[] args) { //int c = 5 / 0; //1. 문법 오류 //int a = 5; int b = 0; int c = a / b; //빌드하면 문법 오류없음 2. 실행시 애러(런타임 오류) //int c = args+bool; //논리상 나누기가 필요한데 ..
선언과 동시에 초기화 하는 예제입니다. Program.cs 01 using System; 02 public class 변환연산자 03 { 04 public static void Main() 05 { 06 //Car car; 07 //car = new Car("에쿠스"); 08 09 //Car car = new Car("에쿠스"); 10 11 Car car = "에쿠스"; 12 13 Console.WriteLine("{0}",car.Name); 14 } 15 } 7+8 =11번줄이 되고 11번줄을 좀더 줄여서 12번 라인으로표시하기 원칙적 선언과 동시에 초기화 위해 변환연산자 구현한다. //11번 라인 Car car = new Car("에쿠스"); 을 변환연산자라 한다. Car.cs using System..
반복기는 for문/foreach문이 필요할때 호출해서 사용하는 방법입니다. Program.cs //Iterator : 내가 만든 객체를 foreach문으로 //돌리고자할 때 반복기가 구현되어있어야 함... using System; class 반복기 { static void Main(string[] args) { int[] data = { 1, 2, 3 }; foreach (var item in data) //var 대신 int로 써도 됨 { Console.WriteLine(item); } Car hyundai;//Class hyundai= new Car(3); //Constructor hyundai[0] = "에쿠스"; //Indexer hyundai[1] = "제네시스"; hyundai[2] = "그랜..
Program.cs using System; namespace 암시적으로형식화된로컬변수 { class Program { static void Main(string[] args) { int a = 10; //변수 선언 int? b = null; //Nullable 형식 //암시적으로 형식화된 로컬 변수 var i = 1234; //알아서 초기화되는 값으로 선언 var s = "1234"; //타입 출력 Console.WriteLine("{0}", i.GetType()); //Int32 Console.WriteLine("{0}", s.GetType()); //String } } } 결과화면
불할클래스는 동일한 클래스를 여러개 파일에 걸쳐 나눠서 설계를 하지만 실제 빌드(컴파일)했을 때에는 단일 클래스로 합쳐집니다. Program.cs using System; public partial class Car { private string name; //Field } public partial class Car { public void Run() //Method { Console.WriteLine("{0}가 달리다", this.name); } } Car.cs using System; class 분할클래스 { static void Main(string[] args) { Car s = new Car("소"); s.Run(); } } public partial class Car { public Car(..
참조타입 관련 연산 예제 입니다. Program.cs using System; class 추가연산자 { static void Main(string[] args) { int i = 10; //값 형식은 사용불가 (기본) string s = "안녕"; object o = "Hi"; int? num = null; // ??연산자는 Nullalbe 가능 Console.WriteLine("{0}", o is string); //True Console.WriteLine("{0}", o as string); //(o is string) ? o :null //변환 그렇지 않으면 null Console.WriteLine("{0}", (num ?? 1234) ); //(num is null)?1234 : num; } //..
.Net FrameWork영역 운영체제와는 별계로 값이 스택에 붙으면 value이고 힙에 붙으면참조형 언박싱 -> 스택 (힙영역의 값을 푸는것) / 박싱 -> 힙 (형 변환) Program.cs using System; class 값형식과참조형식 { static void Main(string[] args) { //값 형식 : Value Type : 닷넷이 관리하는 메모리의 스택에 보관 int i = 1234; //참조 형식 : Reference Type : 닷넷의 힙 메모리에 보관 string s = "안녕\0하세요"; //리터널(Literal) Console.WriteLine(s); //박싱(Boxing)과 언박싱(UnBoxing) string su = "1234"; int num = Convert.T..
정수형 등의 변수는 반드시 초기화해서 사용해야 한다.변수 선언시 ?를 붙이면 null 값으로 초기화할 수 있다. Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Nulilable { class Program { static void Main(string[] args) { int i = 100; //double nd = null; // ? 붙이지 않으면 null값으로 처리안됨 int? ni = null; double? nd = 12.34; if (ni.HasValue) { //널값이면 false, 값이 있으면 참 Console.WriteLine("{0}", ni.V..
Program.cs using System; namespace MyCalc { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("명령줄인수는 3개 이상 필요합니다."); return; } int first = Convert.ToInt32(args[0]); int second = Convert.ToInt32(args[2]); string calc = Convert.ToString(args[1]); int r = 0; if (args[1] == "+") { r = first + second; } else if (args[1] == "-") { r = first - second; } else if..
Copyright © 2016 by WaaNee. All Rights Reserved.