본문 바로가기

.NET/C# Basic

C# 켈렉션 콜렉션 ArrayList Hashtable SortedList Stack ArrayList 클래스 IList 인터페이스를 구현한 것으로, 객체 배열의 크기가 동적으로 조절 내부적으로는 object형 배열을 가지며, 배열의 크기가 할당된 것보다 더 필요한 경우 더 큰 배열로 대체 using System; using System.Collections; class Class { static void Main() { ArrayList list = new ArrayList(); list.AddRange(new string[] {"apple","bababobo","cat" }); list.Sort(); foreach(string s in list) Console.WriteLine(s); Console.WriteLine(list[2]); } } Hashtable 클래스 Hashtable .. 더보기
닷넷 클래스 object is 연산자 GetHashCode() 닷넷 클래스 object object도 일종의 클래스 이므로 내부에 맴버를 가진다 생성자는 아무것도 하지않는 디폴트생성자만 정의되어있으면 필드, 상수, 프로퍼티 인덱서 이벤트등은 가지지않는다.루트 클래스의 맴버는 모든 후손에게 상속되므로 메모리를 소모하는 멤버는 포항하기 어렵다 만약 object에 40바이트의 필드가 있다면 닷넷의 모든 객체는 최소 40바이트 이상으로 되어야하므로 메모리 낭비가 너무 심해질것이다 그래서 객체를 관리하는 일반적인 메서드만을 가진 object 로부터 파생된 모든클래스의 객체들은 이메서드를 가지는 셈이다 ToString public virtual 객체를 문자열 형태로 표현한다 티폴트로 클래스 이름을 리턴하는 데 필요시 재정의할수있다 GetType public 객체의 타입정보를 .. 더보기
닷넷 클래스 리플렉션 reflection 클래스 라이브러리 닷넷 기본 클래스 라이브러리를 사용하면 개발 시간을 많이 단축할 수 있다 제공된 클래스를 생성하거나 상속받아서 새로운 클래스를 정의 System 네임스페이스는 기본 클래스를 모아둔 네임스페이스 System.Object 객체를 비롯해서 기본 데이터형과 같은 기본 클래스가 정의 리플렉션 GetType 메서드는 클래스를 나타내는 Type 객체를 리턴하는 메서드 Type 객체를 이용하면 실행 타임에 클래스에 대한 다양한 정보를 구할 수 있다 Type type = pt.GetType(); FieldInfo [] finfo = type.GetFields(); Type 객체로부터 클래스 정보를 구할 수 있는 방법을 리플렉션(reflection) 이라 한다 리플렉션을 통해서 알아낼 수 있는 정보에는 .. 더보기
클래스관련 const 상수 readonly 구조체관련 GetType() , is as using System; class Sizes { public const int Medium = 100; // 상수로써 사용되는것 const 항상동일한수 public const int Large = 105;// const 라고 붙은것은 별도의 객체생성없이도 가능 public const int XLarge = 110; } class Shirt { public static void Main() { while(true) { string s; Console.WriteLine("치수 기호를 입력하세요. (M)edium (L)arge (X)Large (q)uit"); s = Console.ReadLine(); if(s == "q") break; switch(s) { case "M" : Console.WriteLin.. 더보기
프로퍼티 인덱서 ///// 프로퍼티 ///////// using System; class test { private int a; public int A { get { Console.Write("get"); this.a = a+2; return a; } set { Console.WriteLine("set"); this.a = value; } } } class jks { static void Main() { test t = new test(); t.A = 2; Console.WriteLine("{0}",t.A); } } using System; class jksPro { private string Name; public string NameP { get { string newName = this.Name + "추가문장"; .. 더보기
연산자 메서드 operator +(), operator-() , 연산자 오버로딩? 연산자 메서드를 부르려면 해당 연산자를 사용하며, 연산자를 사용하면 코드가 훨씬 직관적이기 때문에 이해하기 쉬워진다 using System; class Point { public int x; public int y; public Point( int x, int y ) { this.x = x; this.y = y; } public override string ToString() { // (x,y) 값 return String.Format("({0},{1})", x, y ); } // + 연산자 메서드 public static Point operator +(Point pt1, Point pt2) { return (new Point(pt1.x + pt2.x, pt1.y + pt2.y)); } // - 연산자 .. 더보기
static정적맴버 배열 연산자 is/as foreach 문 메서드 오버로딩 메서드 가변 인자 정적(static) 멤버 일반적인 객체 사용 방법은 클래스를 정의하고, 여러 개의 인스턴스를 생성 현재 생성된 총 인스턴스 수를 구하려면 정수형 변수를 두고 인스턴스를 생성할 때마다 하나씩 증가 인스턴스간에 공유해야 하는 값이 필요한 경우, 정적(static) 멤버를 사용 using System; class Point { public Point() { bbb++; Console.WriteLine("생성"+ bbb); } public int bbb = 0; // static int bbb = 0;v // 스택틱하면 값이 더해진다 } class jks { static void Main() { Point pt = new Point(); Point pt1 = new Point(); } } 배열////// usi.. 더보기
인터페이스(interface) 와 추상화클래스(abstract) 예제 using System; namespace test_cal { interface IAdd { int Add(int i, int j); } interface IMnus { int Minus(int i, int j); } abstract class abMethod { public int Add(int i , int j) { int tot = i +j; return tot; } public abstract int Minus(int i ,int j); } class NewabMethod : abMethod { public override int Minus(int i, int j) { int m = i -j; return m; } } class NewInter : IAdd, IMnus { public int Ad.. 더보기