Introduction to C# part 1 : namespace

วันนี้เราจะเริ่มทำการเขียน C# กันละนะคับ โดยเริ่มตั้งแต่พื้นฐานเรื่อง namespace
ผมจะเทียบการเขียน C# กับการเขียน JAVA เป็นหลักนะคับ เพื่อที่จะได้เข้าใจกันได้ง่ายขึ้น

namespace เปรียบได้กับ package ใน JAVA คับ ซึ่งจะเป็นเหมือนตู้ที่เก็บคลาสอยู่นั่นเอง

1.สร้าง project ที่เป็น Console Application ขั้นมา

2.สร้างไฟล์แรกชื่อ file1.cs

using System;
namespace First
{
    public class A
    {
       public A()
       {
           Console.WriteLine("Namespace:First.Class:A");
       }
    }
    public class B
    {
       public B()
       {
           Console.WriteLine("Namespace:First.Class:B");
       }
    }
}

3.สร้างไฟล์ที่สองชื่อ file2.cs

using System;
namespace Second
{
    public class A
    {
        public A()
        {
            Console.WriteLine("Namespace:Second.Class:A");
        }
    }
    public class C
    {
        public C()
        {
            Console.WriteLine("Namespace:Second.Class:C");
        }
    }
}

คราวนี้เรามาดูในไฟล์หลักที่มี main อยู่กันครับ

using System;
using First;
using Second;

namespace NamespaceTester
{
    class Program
    {
        static void Main(string[] args)
        {
            C c = new C();
            /*
             * A a = new A(); // ถ้าไม่คอมเมนส่วนนี้จะ error คับ
             * เนื่องจากคลาส A มีอยู่ทั้งใน namespace First, Second ทั้งคู่
             * เราจึงต้องระบุ  namespaceให้จัดเจนคับจึงจะไม่มีปัญหา
            */
            First.A aA = new First.A();
            Second.A aB = new Second.A();
            //Console.ReadLine(); /*ถ้ารันแล้วเรายังไม่ทันเหนผลแล้วจอดับไปให้ลบcommentหน้าบรรทัดนี้ออกนะคับ*/
        }
    }
}

เห็นหรือยังคับกับวิธีการใช้ namespaceอย่างง่ายๆ
ส่วนใครที่งงกับ Console.WriteLine() ไม่ต้องตกใจไปคับ มันเหมือนกับการ print ออกมาอย่าง System.out.println() คับ

การใช้namespaceก็จบลงเท่านี้ละนะค้าบ แล้วคราวหน้าเรามาต่อกันกับ Data types กันนะคร๊าบ ^^

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (13) | RSS


Comments

June 20. 2009 21:57

hara

ขอบคุณคร้าบ...เห็ยภาพดีอะ

hara

August 10. 2009 05:23

payday loans

I think your blog need a new wordpress template. Downalod it from http://templates.wordpressguru.in, The site has nice and unique wordpress templates.

payday loans

August 12. 2009 05:22

cash loans

I don�t usually reply to posts but I will in this case. I�ve been getting this very same problem with a new WordPress installation of mine. I�ve spent weeks calibrating and getting it ready when all of a sudden� I cannot delete any content. It�s a workaround that, although isn�t perfect, does the trick so thanks! I really hope this problem gets solved properly asap.

cash loans

August 27. 2009 20:08

bad credit loans

Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.

bad credit loans

August 27. 2009 23:18

bad credit loans

I'm searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up.

bad credit loans

September 17. 2009 01:14

payday loan

A SUPPORTED BY THE DEVELOPER TOOLS? It was interesting. You seem very educated in your field.

payday loan

October 1. 2009 07:57

Make Money Online

Great overview. Your style of writing is really a joy to read.

Make Money Online

October 31. 2009 03:44

easy cash loans

Do you have any more info on this?

easy cash loans

November 5. 2009 01:54

faxless payday loans

Interesting ... as always - is your blog making any cash advance ? ;)

faxless payday loans

November 6. 2009 23:58

cash advance

Nothing more to say ... just perfect

cash advance

November 9. 2009 01:49

personal loans

I guess there's always an easier way ...

personal loans

November 24. 2009 18:36

paydayloans

Interesting post

paydayloans

January 2. 2010 00:39

fast payday loans

Do you make money out of this blog? just curious

fast payday loans

Add comment




biuquote
  • Comment
  • Preview
Loading



 

Tag Cloud