'c#'에 해당되는 글 4건

  1. 2007/08/13 8.11(토) C# - 기초문법4
  2. 2007/08/13 8.11(토) C# - 기초문법3
  3. 2007/08/13 8.11(토) C# - 기초문법2
  4. 2007/08/13 8.11(토) C# - 기초문법1

8.11(토) C# - 기초문법4

from Study/C# 2007/08/13 17:18 view 19646

using System;

 

// 1.언제 수집되는가 ? Context스위칭시간 10ms정도를 가비지시스템이 작동한다.

// 메모리를 일단 쓰게 한후에 어느정도 찼을 메모리를 비워준다.
// (
메모리의 이동을 줄인다. )

// 2.세대 개념 0, 1, 2세대(관리 ) 차례대로 진행된다.

// 3.소멸자 호출

 

// C# Java 소멸자를 만들지 않는다.

// 대신 IDisposable 인터페이스를 구현해라.

// WINDOW API 가져다 HANDLE 같은것을 해제 시켜 줄 때만 소멸자의 역할을 만든다.

 

 

class Point

{

    public int x;

 

    public Point()

    {

        Console.WriteLine("Point.Point()");

    }

 

    public void Dispose()

    {

        Console.WriteLine("여기서 소멸자의 역할을 하게 해라.");

    }

 

    // c# 소멸자를 만들 있다.( finalize라고 부른다. )

    // 하지만 정확히 언제 호출 될지를 없다.

    //~Point()

    //{

    //    Console.WriteLine("Point.~Point()");

    //}

}

 

class Test

{

    public static void Main()

    {

        {

            Point p1 = new Point();

 

            p1.Dispose();   // 소멸 작업이 필요한 경우..호출

        }

        //GC.Collect(0);   // 강제로 가비지 Collection 한다.

        //GC.Collect(0);

        //Console.WriteLine("{0}", GC.CollectionCount(0));

 

        Console.WriteLine("AAA");

    }

}

Tag |

8.11(토) C# - 기초문법3

from Study/C# 2007/08/13 17:16 view 19858

using System;

using System.Reflection;    // c# reflect 서비스

 

// Reflection 메타데이타 에서 정보를 얻어온다.

class Point

{

    public int x;

    public int y;

 

    public void foo() { }

}

 

class Test

{

    public static void Main()

    {

        Point p = new Point();

 

        Type t = p.GetType();   // p type 정보를 담고 있는 객체를 얻는다.

                                // RTTI, RTCI(MFC)

        Console.WriteLine("{0}", t.FullName);

        Console.WriteLine("{0}", t.BaseType.FullName);

 

        // t 모든 메소드 정보를 배열로 얻어 온다.

        MethodInfo[] mis = t.GetMethods();

 

        // 배열의 모든 요소를 열거하면서 출력한다.

        foreach (MethodInfo m in mis)

        {

            Console.WriteLine("{0}, {1}", m.Name, m.ReturnType);

        }

    }

}

 

////////////////////////////////////////////////////////////////////////
// Monostate : static
멤버 data static 멤버 변수만 가진 클래스 객체를
              만들 필요가 없다.

class Math

{

    //private Math() { }

    public static int plus(int a, int b) { return a + b; }

}

Math.plus(1, 2);

 


///////////////////////////////////////////////////////////////////////////
//
델리게이트 선언

delegate void CrashHandler( int a );

// 선언을 보고 C#컴파일러가 CrashHandler라는 클래스를 생성해 준다.

// 내부적으로 void 리턴하고 int 인자가 1개인 함수 포인터를 관리해주는 클래스.

 

class Car

{

    public int speed = 0;

    public CrashHandler handler;    // 델리게이터의 참조

 

    public void SetSpeed(int n)

    {

        speed = n;

        if (handler != null)

            handler.Invoke(n);  //handler(n);  // 외부에 알린다.

    }

}

 

class Test

{

    public static void foo(int a)

    {

        Console.WriteLine("Test::foo - {0}", a);

    }

    public static void Main()

    {

        Car c = new Car();

 

        c.handler = new CrashHandler(Test.foo);

        c.handler += new CrashHandler(Test.foo);

 
        c.SetSpeed(100);

    }

}

 

Tag |

8.11(토) C# - 기초문법2

from Study/C# 2007/08/13 17:12 view 26679

using System;

 

// C++ 다른 C# 핵심 문법들

 

// 1. Property : 구현자 입장에서는 함수(제어문 가능)

//               사용자 입장에서는 Data처럼 보인다.

// 2. indexer : C++ 배열 연산자 재정의..

 

class Bike

{

    private int gear;

    private int[] array = new int[10];  // C# 배열 만드는 .

 

    public int Gear

    {

        get { return gear; }

        set {  if( value < 100 ) gear = value; }

    }

 

    public int this[int name]   // set_Item( name, value ) 함수로 제공된다.

    {

        get { return array[name]; }

        set { array[name] = value; }

    }

}

 

class Test

{

    public static void Main()

    {

        Bike p = new Bike();

        p.Gear = 10;

 

        Console.WriteLine("{0}", p.Gear);

    }

}


//////////////////////////////////////////////////////
// out
파라미터와 Ref 파라미터

// 결국 항상 C처럼 메모리 그림을 그려서 해결하면 됩니다.

 

class Point

{

    public int x;

}

 

class Test

{

    public static void foo(out Point p)

    {

        //p.x = 20;

 

        p = new Point();

 

        p.x = 100;

    }

 

    public static void Main()

    {

        Point p1 = new Point();

        p1.x = 10;

 

        foo(out p1);

 

        Console.WriteLine("{0}", p1.x);

    }

}

 

class Test

{

    public static void foo(out int a)

    {

        a = 20;

    }

    public static void Main()

    {

        int x = 10;

        foo(out x);

 

        Console.WriteLine("{0}", x);

    }

}

 

//////////////////////////////////////////////////////////
//
결국 C++ : type 사용자가 메모리의 위치 결정

//      C#  : type 제작자가 메모리의 위치 결정

 

// 무조건 참조 type이다.

class Point1

{

    public int x;

    public int y;

}

 

// 구조체는 무조건 type이다. stack 객체가 만들어 진다.

struct Point2

{

    public int x;

    public int y;

}

 

class Test

{

    public static void Main()

    {

        Point1 p1;  // 참조 뿐이다.

        Point2 p2;  // 객체이다.

 

        Point1 p3 = new Point1();   // 힙에 만든다.

        Point2 p4 = new Point2();   // stack 만든다.

 

        // int 구조체이다.

        int a;

 

        Point1 pp;  // 실제 Point1 아니고 C++ 포인터개념이다.
                    // (C#
에서는 레퍼런스라고 한다.)

 

        //Point1 p = new Point1;  // C++

        Point1 p = new Point1();  // C#

 

        int a = 0;

        int b = new int();

    }

}

Tag |

8.11(토) C# - 기초문법1

from Study/C# 2007/08/13 17:06 view 21478

using System;   // using namespace System 의미


// 1. Main 함수의 기본 모양

// 2. Console 클래스의 static 함수들을 사용해서 표준 입출력을 처리한다.

// 3. %d 대신 {0}, {1} 사용.

// 4. 모든 것은 객체이다. 결국 Object 자식이다.
class Test

{

    public void foo()

    {

    }

 

    public static void Main()

    {

        int a = 10;

        int b = 20;

 

        Console.WriteLine("a= {0} {1} {0}", a, b);

    }

}

 

///////////////////////////////////////////////////////////////
//
주제 2. 모든 것은 객체이다. 그리고 모든 것은 Object 자식이다.

class Point

{

    public int x = 0; // 필드 초기화 가능

    public int y = 0; // 필드 초기화 가능

    public Point( int a, int b )

    {

        x = a;

        y = b;

    }

 

    // 부모인 Object ToString() 멤버 함수를 재정의 한다.

    // C# 가상함수 재정의시 override 키워드를 사용한다.

    public override string ToString()

    {

        return "[Point]";

    }

}

 

class Test

{

    public static void Main()

    {

        int a = 10; // int 별명에 불과하다.

 

        System.Int32 b = 0;

 

        Console.WriteLine("{0}", a);

 

        Point p = new Point(1, 1); // 주의 C++ new Point;

 

        Console.WriteLine("{0}", p.ToString());

    }

}

Tag |