Monday, 30 January 2012

history of C#

အရင္ဆံုး C# ဆိုတာ ဘယ္လိုျဖစ္ေပၚလာလဲ စေျပာရေအာင္။ ကြၽန္ေတာ္သိတာကို ေျပာမွာေနာ္။ အားလံုးမွန္ခ်င္မွလည္း မွန္လိမ့္မယ္။
ကဲ… စျပီ….။
C# ဆိုတာ Microsoft က ထုတ္လိုက္တဲ႔ programming language တစ္ခုေပါ့။ C# ျဖစ္ေပၚလာရတဲ႔ history ကို အရင္ ေျပာၾကည့္မယ္ဗ်ာ။ programming language ေတြ အမ်ားၾကီး ရွိတဲ႔ အထဲမွာ ေတာ္ေတာ္မ်ားမ်ား စလုပ္ဖူးတာက C , C++ ထင္ပါတယ္။ C နဲ႔ ေရးရင္ ကြၽန္ေတာ္တို႔ Graphic User Interface ေတြ ေရးရတာ အဆင္မေျပဘူးဗ်။ နည္းနည္းပင္ပန္းတယ္။ ဒါနဲ႔ ပိုလြယ္တဲ႔ language ေတြ ရွာလာတဲ႔ အခါ Microsoft ကထုတ္တဲ႔ Visual Basic(VB) ကိုသံုးျဖစ္လာၾကတယ္။ VBက microsoft က ထုတ္တဲ႔ Visual Studio လို႔ေခၚတဲ႔ programming language အစုအေ၀းၾကီးထဲက Language တစ္ခုေပါ့ဗ်ာ။ အဲ့ဒီ Visual Studio ထဲမွာ VB, VC++, Visual Foxpro, စသည္ျဖင့္ဗ်ာ ေရြးခ်ယ္လို႔ ရတဲ႔ language ေတြပါပါတယ္။ VB ရဲ့ အားသာခ်က္က Design အပိုင္းမွာ ေတာ္ေတာ္ေလးကို လြယ္တာပါပဲ။ ဒါေပမယ့္ အားနည္းခ်က္ကေတာ့ Object-Oriented Technology ကို သံုးလို႔ မရဘူး။ OOP မဟုတ္ဘူးေပါ့ဗ်ာ။ ဒီေတာ့ တကယ့္ Enterprise Application ၾကီးေတြ ေရးလို႔ အဆင္မေျပဘူးလို႔ ေျပာၾကတယ္။ VC++ နဲ႔ ေရးဖို႔ကလည္း ေတာ္ေတာ္ေလးကို ခက္တယ္လို႔ ေျပာၾကတယ္ဗ်။ (ကြၽန္ေတာ္ေတာ့ မေရးဖူးပါ)။ ဒါေၾကာင့္ ေနာက္ပိုင္းမွာ programmer ေတြက Java လိုင္းကို ေျပာင္းၾကတယ္။ ဒီေတာ့ Microsoft က java ကို fightဖို႔ အၾကံထုတ္ရျပီေပါ့။ တျခားprogramming language ေတြရဲ့ အားသာခ်က္ေတြကို ၅ႏွစ္ေလာက္ေလ့လာျပီးမွ ၂၀၀၁၊၂၀၀၂ မွာ Visual Studio .NET ဆိုတာၾကီးကို ထုတ္လိုက္တယ္ဗ်။ အဲ့ဒီ Visual Studio .NET ထဲမွာ နဂို VS ေတြထဲက ပါေနၾကျဖစ္တဲ႔ VB, VC++ စတာေတြ အျပင္ Visual J# ဆိုတာနဲ႔ Visual C# ဆိုတဲ႔ language အသစ္ ၂ ခုကို မိတ္ဆက္ေပးလိုက္တယ္ဗ်။ java သမားေတြအတြက္ J# နဲ႔ C သမားေတြ အတြက္ C# ဆိုပါေတာ့ဗ်ာ။ အဲ့ဒီ language ၂ ခုမွာ C# ကေတာ့ ေတာ္ေတာ္ေလးကို ေအာင္ျမင္သြားတယ္။(ေပါက္သြားတယ္ေပါ့ဗ်ာ)။ Microsoft ကလည္း C#ကို ေတာ္ေတာ္ညႊန္းတယ္ဗ်။ တျခားlanguage ေတြ အားလံုးရဲ့ အားသာခ်က္ေတြကို ယူထားတဲ႔ language ဆိုပဲဗ်။ VB လိုမ်ိဳး design ကို အလြယ္တကူေရးနိုင္တယ္။ Java လိုမ်ိဳး OO ရဲ့ feature ေတြကို သံုးနိုင္တယ္။(java ထက္ေတာင္မွ OO ပိုစစ္ေသးတယ္ဆိုပဲဗ်)(ဥပမာဗ်ာ java မွာဆိုရင္ data type ေတြက keyword အဆင္မွာပဲ ရွိတယ္၊ C# မွာေတာ့ datatype ေတြကိုယ္တိုင္က classေတြ အျဖစ္ရွိတယ္ဗ်ာ၊ class မဟုတ္တာဆိုလို႔ operator ေတြပဲ က်န္ေတာ့တယ္တဲ႔)။
ကဲ ဒါကေတာ့ C# language ေမြးဖြားလာပံုေပါ့ဗ်ာ။

.NET Framework

C# language စျပီးပါလာတဲ႔ Visual Studio .NET 2002 မွာပဲ Microsoft က .NET Framework ဆိုတာၾကီးကိုပါ မိတ္ဆက္ေပးခဲ႔တယ္ဗ်။ အဲ့ဒီ .NET Frameworkၾကီးထဲမွာ အဓိကကေတာ့ အပိုင္း ၂ပိုင္းပါတယ္ဗ်။ Common Language Runtime (CLR) နဲ႔ Framework Class Library(FCL) ဆိုျပီးေတာ့။
CLR က ကြၽန္ေတာ္တို႔ .NET Platform မွာ ေရးတဲ႔ ဘယ္language codeကိုမဆို Microsoft Intermediate Language အျဖစ္ေျပာင္းေပးျပီး Run လို႔ ရေအာင္လုပ္ေပးတယ္ေပါ့ဗ်ာ။ FCL ကေတာ့ predefined class library အစုအေ၀းၾကီးလို႔ပဲ နားလည္ထားတယ္ဗ်။ .NET Framework အလုပ္လုပ္ပံု ေလးၾကည့္ၾကည့္ရေအာင္ဗ်ာ။
အရင္ဆံုးကြၽန္ေတာ္တို႔ Programတစ္ခုေရးလိုက္တယ္ဗ်ာ။ အဲ့ဒါကို source code လို႔ ေခၚတယ္။ source code ကို ကြၽန္ေတာ္တို႔ ကြန္ပ်ဴတာၾကီးက နားမလည္ပါဘူး။ ဒီေတာ့ ကြန္ပ်ဴတာနားလည္မယ့္ machine code အျဖစ္ကို ေျပာင္းဖို႔လိုပါတယ္။ ဒီအလုပ္ကို .NET Framework အတြင္းမွာ ရွိတဲ႔ သက္ဆိုင္ရာ Language အလိုက္ compiler ေတြက ဘာသာျပန္အလုပ္လုပ္ပါတယ္။ ဒီေနရာမွာ သိထားရမွာက .NET Framework ကိုသံုးျပီးေရးထားတဲ႔ code ေတြကို compile လုပ္ရင္ machine code တန္းမထြက္ပါဘူး။ .NET Framework ကပဲနားလည္နိုင္တဲ႔ MSIL လို႔ေခၚတဲ႔ Microsoft Intermediate Language Code အျဖစ္ပဲထြက္ပါတယ္။ အဲ့ဒီ code ကိုမွတဖန္ CLR ထဲမွာ ရွိတဲ႔ Jet In-time Compilier (JIT) ေလးက ထပ္ျပီး Compile လုပ္ပါတယ္။ ဒီလိုလုပ္လိုက္မွ Operating System ကနားလည္တဲ႔ code အျဖစ္ကိုေရာက္ရွိပါတယ္။ ဆိုလိုတာကေတာ့ဗ်ာ .NET Framework မရွိရင္ .NET program ေတြ run လိုေတာင္မရဘူးေပါ့။ ေနာက္ပိုင္း Microsoft က ထုတ္တဲ႔ OS ေတြမွာ .NET Framework ကို build-in ထည့္ေပးပါတယ္။ ဒါမွမဟုတ္လည္း internet ကေန framework ကို အလကားေပးပါတယ္။ Visual Studio .NET ၾကီးတစ္ခုလံုး ၀ယ္မယ္ဆိုရင္ေတာ့ ေစ်းေတာ္ေတာ္ၾကီးတယ္ၾကားတယ္ဗ်။ (ကြၽန္ေတာ္တို႔ ျမန္မာနိုင္ငံမွာေတာ့ ေပါပါတယ္ ) FCL အေၾကာင္းေတာင္ ေမ႔ေတာ့မလို႔။ ဒီလိုဗ် Framework Class Library ဆိုတာ ကြၽန္ေတာ္တို႔ program ထဲမွာ user monitor ေပၚ 'hello' ဆိုျပီး ရိုက္ထုတ္ျပခ်င္တယ္ဆိုပါေတာ့။ အဲ့ဒါကို C နဲ႔ ေရးရင္
printf("hello");
ဆိုျပီး ရိုက္တယ္ဗ်ာ။ အဲ့ဒီကုဒ္ထဲမွာ ေရးလိုက္တဲ႔ printf ဆိုတဲ႔ဟာေလးကို function လို႔ေခၚတယ္ဗ်။ အဲ့ဒီ function က ကြၽန္ေတာ္တို႔ကိုယ္တိုင္ေရးတာမဟုတ္ဘူးဗ်ာ၊ library ထဲကေနေခၚသံုးတာေပါ့။ အဲ့ဒီလို predefined function ေတြေရးျပီး သိမ္းထားတဲ႔ class အစုအေ၀းၾကီးကို Framework Class Library လို႔ ေခၚေၾကာင္းပါဗ်ား။
ကဲ ဒီဇယားေလးကေတာ့ .NET Framework version history ပါဗ်ာ။
Version .... Release ... Visual Studio ................... Default in Window
1.0 ............ 2002 ....... Visual Studio .NET .............................-
1.1 ............ 2003 ....... Visual Studio .NET 2003 .......... Window Server 2003
2.0 ............ 2005 ....... Visual Studio 2005 ............................-
3.0 ............ 2006 ..................- .............................. Window Vista, Window Server 2008
3.5 ............ 2007 ....... Visual Studio 2008 .................. Window 7, Window Server 2008 RC2
4.0 Beta1 .... 2009 ....... Visual Studio 2010

hello world from C#

ကဲ အခုကြၽန္ေတာ္တို႔ C# programming language နဲ႔ program တစ္ခုေလာက္ စေရးၾကရေအာင္ဗ်ာ။ programming language တိုင္းရဲ့ first time program ေလး helloworld နဲ႔ပဲ စလိုက္ရေအာင္။ C# program စေရးဖို႔အတြက္ ခင္ဗ်ားတို႔ စက္မွာ .NET Framework install လုပ္ထားျပီးသားျဖစ္ဖို႔ေတာ့ လိုတယ္ဗ်။ Visual Studio install လုပ္ထားျပီးသားဆိုရင္ framework ထပ္သြင္းစရာမလိုေတာ့ဘူးေနာ္။ မသြင္းရေသးဘူးဆိုရင္ေတာ့ ဒီမွာသြားျပီး download လုပ္ျပီးသြင္းလိုက္ဗ်ာ။ framework သြင္းျပီးရင္ေတာ့ ကြၽန္ေတာ္တို႔ program စေရးလို႔ ရျပီ။ java လိုပဲဗ် C# program ကိုလည္း Visual Studio IDE မသံုးပဲေရးလို႔ ရတယ္။ အခု အဲ့ဒီလိုေရးၾကရေအာင္။ program ေရးဖို႔အတြက္ Text Editor တစ္ခုေတာ့လိုတယ္ဗ်။ ခင္ဗ်ားတို႔စက္မွာ Notepad ေတာ့ ရွိျပီးသား ျဖစ္မွာပါ။ ကဲ Notepad ဖြင့္ဗ်ာ။
window key + R ႏွိပ္ျပီး Run Box ကိုေခၚလိုက္ဗ်ာ ျပီးရင္ အဲ့ဒီ Box ထဲမွာ notepad လို႔သာရိုက္ထည့္လိုက္၊ ဒါဆို Notepad ပြင့္လာလိမ့္မယ္။ ဟုတ္ျပီ… program စေရးရေအာင္။ ဒီအတိုင္းေရးလိုက္ဗ်ာ ျပီးမွ ျပန္ရွင္းမယ္။
class Hello
{
static void Main()
{
System.Console.Write("Hello World");
}
}
ေရးျပီးျပီေနာ္… အိုေက ျပန္ရွင္းရေအာင္။ C တို႔ java တို႔လိုပဲ C# program ေတြကလည္း Main function ကေနစျပီး အလုပ္လုပ္တယ္ဗ်။ ဒါေပမယ့္ C# မွာေတာ့ Main ကို M အၾကီးနဲ႔ ေရးရတယ္ေနာ္။ ျပီးေတာ့ static ဆိုတဲ႔ keyword လည္းထည့္ရတယ္။ အိုေက ကြၽန္ေတာ္တို႔ ဒီအတိုင္းေရးလိုက္ျပီ static void Main() ေပါ့။
အဲ့ဒီ Main function ရဲ့ scope ကိုသက္မွတ္ဖို႔ { နဲ႔ } ကို သံုးတယ္။ ဒါလည္း C တို႔ java တို႔နဲ႔ တူတူပဲ။ အဲ့ဒီ scope ထဲမွာမွ ကြၽန္ေတာ္တို႔ computer ကို ခိုင္းေစခ်င္တဲ႔ statement ေတြကို ေရးရမယ္။ statement တစ္ေၾကာင္း ျပီးျပီဆိုတိုင္းမွာ semicomar( ပိတ္ဖို႔လည္း မေမ႔နဲ႔ဦးေနာ္။
monitor မွာ စာရိုက္ခုိုင္းဖို႔ အတြက္ C# မွာ Write or WriteLine function ကို သံုးရတယ္ဗ်။ အဲ့ဒီ Write function ကိုလည္း ဒီတိုင္းေခၚခြင့္မရွိဘူးဗ်၊ function ရွိေနတဲ႔ class name နဲ႔ တဆင့္ေခၚရတယ္။ Write function က Console class ထဲမွာ ရွိတယ္ေလ၊ ဒါေၾကာင့္ ေရးတဲ႔အခါ Console.Write လို႔ေရးမွမွန္မယ္။ ဟုတ္ျပီ ဒါဆို ေရွမွာ ဘာလို႔ System ဆိုတာၾကီးပါရတာလဲ။ ဒါက ဒီလိုရွိတယ္ဗ် အဲ့ဒီ System ဆိုတာကို Namespace လို႔ ေခၚတယ္။ Namespace ဆိုတာ class ေတြ စုထားတဲ႔ class အစုအေ၀းၾကီး class library ေပါ့ဗ်ာ။ Console class က System namespace ထဲမွာ ရွိတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔က System.Console.Write လို႔ ေရးရတာဗ်။ ဒီလိုမ်ိဳး class name ရဲ့ ေရွ႕မွာ ထည့္မေရးခ်င္ဘူး ဆိုလည္း ေနာက္တစ္နည္းရွိေသးတယ္။ class မေဆာက္ခင္မွာ library file အေနနဲ႔ ၾကိဳျပီး ေၾကျငာေပးထားလို႔ ရတယ္။ C မွာ #include၊ java မွာ import ဆိုျပီး ေၾကျငာသလို C# မွာလည္း using ဆိုျပီး ေၾကျငာရတယ္။ ဒီလိုမ်ိဳးဗ်ာ…
using System;
class Hello
{
static void Main()
{
Console.Write("Hello World");
}
}
ခုလို႔ class အေပၚမွာ System namespace ကို using လုပ္လိုက္ရင္။ ဒီ class အတြင္းမွာ အဲ့ဒီ System namespace ထဲက class ေတြကို စိတ္ၾကိဳက္ေခၚသံုးလို႔ ရျပီ။ ေနာက္ထပ္ထပ္ေရးေနစရာ မလိုေတာ့ဘူး။ မဟုတ္ရင္ Console.Write တစ္ခါရိုက္ခ်င္တိုင္း ေရွ႕မွာ System ဆိုတာၾကီးကို ျပန္ျပန္ထည့္ေပးေနရလိမ့္မယ္။
ကဲ Main function ေရးျပီေတာ့ ျပီးေရာလားဆိုေတာ့ မျပီးေသးဘူးဗ်။ C# က OOP language ေလ။ ဒီေတာ့ ဒီfunction ကိုလည္း class တစ္ခုခုအထဲမွာပဲ ထည့္ေရးခြင့္ရွိတယ္ဗ်။ function ၾကီးပဲ ဒီတိုင္းေရးလို႔ မရဘူး။ ဒါေၾကာင့္ ကြၽန္ေတာ္က Hello ဆိုတဲ႔ class ထဲမွာ ထည့္ေရးလိုက္တာ။ class name က ကိုယ္ၾကိဳက္တာေပးခြင့္ရွိပါတယ္။ java မွာလိုမ်ိဳး file name နဲ႔ class name နဲ႔ တူစရာ မလိုဘူး။
ဒီေလာက္ဆိုရင္ အေပၚက program ထဲမွာ ေရးထားတဲ႔ coding ေတြကို နားလည္ျပီထင္တယ္ေနာ္။ အဓိကနားလည္ေစခ်င္တာကေတာ့ ဘာလုပ္ခ်င္လို႔ ဘယ္ကုဒ္ကိုေရးရတယ္ ဆိုတာကို နားလည္ေစခ်င္တာ။ ကဲ ကြၽန္ေတာ္တို႔ ေရးလို႔ ျပီးျပီဆိုေတာ့ ဒီ source code ေတြကို သိမ္းရမယ္။ Ctrl + S ႏွိပ္ျပီး C: Drive ေအာက္မွာ ခင္ဗ်ားတို႔ ၾကိဳက္တဲ႔ နာမည္နဲ႔ သိမ္းဗ်ာ၊ အဲ file extension ကေတာ့ (.cs) ျဖစ္ရမယ္ေနာ္။ ဥပမာ ကြၽန္ေတာ္က filename ကို Test1 လို႔ ေပးခ်င္တယ္ဆိုရင္ Test1.cs ဆိုျပီးေပးရမယ္။ ရတယ္ေနာ္… အိုေက အခုကြၽန္ေတာ္တို႔ ဒီ C# source file ေလးကို compile လုပ္ျပီး run ၾကည့္ရေအာင္။ compile လုပ္ဖို႔ အတြက္ command prompt ထဲသြားဖို႔လိုတယ္။ ကဲ႔ Run Box ကိုေခၚျပီး cmd လို႔ ရိုက္လိုက္ဗ်ာ ဒါဆို command prompt ေပၚလာမယ္။ command prompt ထဲမွာ လက္ရွိ cursor location က C: ေအာက္က document and setting ေအာက္က login ၀င္ထားတဲ႔ username ေအာက္ကို ေရာက္ေနလိမ့္မယ္ထင္တယ္။ ဒါဆိုအဆင္မေျပဘူး ကြၽန္ေတာ္တို႔ Root Directory ေအာက္ကို ျပန္သြားရေအာင္။ CD\ ရိုက္ျပီး Enter ေခါက္လိုက္။ ဒါဆို C: ေအာက္ကို ျပန္ေရာက္သြားျပီ။ အဲ့ဒီမွာမွ ကြၽန္ေတာ္က .NET Framework သြင္းထားတဲ႔ location ကို ရွာရေအာင္။ ဒီေနရာမွာ ကြၽန္ေတာ္ တစ္ခုေျပာဖို႔က်န္သြားတယ္။ အေပၚပိုစ့္မွာ ေျပာခဲ႔တဲ႔ အတိုင္း ကြၽန္ေတာ္တို႔ ေရးထားတဲ႔ source file ေတြကို သက္ဆိုင္ရာ language ရဲ့ compilier နဲ႔ compile လုပ္ရတယ္။ C# အတြက္ေတာ့ C Sharp Compiler (csc.exe) ေပါ့ဗ်ာ။ ဒါေၾကာင့္ csc.exe file ေလးကို ရွာမယ္ဗ်ာ။ ကဲ command prompt ထဲမွာ ဒီလိုေလးရိုက္လိုက္ပါဦး။
dir csc.exe /s
ခဏေတာ့ ေစာင့္ရမယ့္ထင္တယ္။ ရွာလိုေတြ႔ျပီဆိုရင္ အဲ့ဒီ compiler ေလးရွိတဲ႔ ေနရာကို path ေပးရမယ္ဗ်။ ခက္တာက ကြၽန္ေတာ္က path ေပးတာကို ေမ႔ေနျပီ။ ဒါေၾကာင့္ အဲ့ဒီ compiler ရွိတဲ႔ location ကိုပဲ CD command သံုးျပီး သြားလိုက္မယ္ဗ်ာ။ ေၾသာ္ဒါနဲ႔ ခင္ဗ်ားစက္မွာ framework version အမ်ားၾကီးရွိေနတာလဲ ျဖစ္နိုင္တယ္ေနာ္။ ၾကိဳက္တဲ႔ version ေအာက္ကို ၀င္လိုက္ဗ်ာ။ ကဲ compile လုပ္ရေအာင္။
csc.exe C:\Test1.cs
ခုလို compile လုပ္လုိက္ေတာ့ compiler က exe file ထုတ္ေပးတယ္ဗ်။ ရွာရင္ ရွာၾကည့္လိုက္ဦး။
dir Test1.exe
ေတြ႔တယ္မလားဗ် ကြၽန္ေတာ္တို႔ program ရဲ့ executable file ေလး။ အဲ့ဒီ file ေလးကို run ရင္ ရျပီေပါ့။ တခုေတာ့ မွတ္ထားဦးေနာ္၊ ဒီ exe file က .NET Framework ရွိမွ run လို႔ ရမွာေနာ္။ အခု တခါတည္း run ၾကည့္ရေအာင္ဗ်ာ။ exe file ဆိုေတာ့ ဒီတိုင္း filename ရိုက္လိုက္ရံုပါပဲ။
Test1.exe
or
Test1
ကဲ႔ ဒါဆိုရင္ ခင္ဗ်ားတို႔ရဲ့ ပထမဆံုး C# program ရဲ့ output ကို ေတြ႔ရျပီေပါ့။
***********
ကြၽန္ေတာ္ video training ေလးပါ တင္ေပးလိုက္တယ္။ ဒီမွာ ယူဗ်ာ။
Compile လုပ္ရင္ program မွာမွားနိုင္တာေလးေတြလည္း ရွိတယ္ဗ်။
၁ - using, class, static, void အဲ့ဒါေတြကို စာလံုး အေသးနဲ႔ေရးရမယ္။
၂ - System, Console, Write, Main အဲ့ဒါေတြကိုေတာ့ အစစာလံုးကို အၾကီးနဲ႔ ေရးရမယ္။
၃ - write function အဆံုးမွာ ( ; ) ထည့္ရမယ္။
၄ - Hello World ဆိုတဲ႔ စာသားကို " နဲ႔ " ထဲထည့္ေရးရမယ္။
ဒီေလာက္ဆို အားလံုး run လို႔ ရၾကမွာပါ။

data types of C#

C# data type ေတြ အေၾကာင္းဆက္ေျပာရေအာင္ဗ်ာ။ Data type ဆိုတာဘာလဲ။ ကြၽန္ေတာ္တို႔ user ေတြဆီကေန data အခ်က္အလက္ေတြကို program ထဲမွာ သိမ္းထားဖို႔အတြက္ ကြၽန္ေတာ္တို႔ computer ရဲ့ memory ေပၚမွာ ေနရာသြားယူေပးရတယ္ဗ်။ အဲ့ဒီလို ေနရာယူတာကို Variable Declaration လုပ္တယ္လို႔ ေျပာတာေပါ့။ Variable declaration လုပ္တဲ႔ အခါ memory ေပၚမွာ ကိုယ္သိမ္းခ်င္တဲ႔ data ရဲ့ အမ်ိဳးအစား(data type)ကို ေၾကျငာေပးရတယ္။ Number သိမ္းမွာလား၊ ဒါမွမဟုတ္ စာသားေတြသိမ္းမွာလား စသည္ျဖင့္ေပါ့။
C# မွာ အဓိက data type ၂ မ်ိဳးရွိတယ္ဗ်။ Value Type နဲ႔ Reference Type ဆိုျပီးေခၚတယ္ဗ်ာ။ Value Type ဆိုတာကေတာ့ memory ေပၚက ကြၽန္ေတာ္တို႔ ေၾကျငာထားတဲ႔ ေနရာမွာ value ကိုတိုက္ရိုက္သိမ္းထားတာဗ်ာ။ Reference Type ဆိုရင္ေတာ့ value ကို မသိမ္းထားပဲ အဲ့ဒီ value ရွိေနတဲ႔ memory address ကိုပဲ ကြၽန္ေတာ္တို႔ရဲ့ variable ထဲမွာ သိမ္းထားတာကို ဆိုလိုတာေပါ့။
နမူနာက ဒီလိုဗ်ာ…
int num = 3;
number value data တခုသိမ္းဖို႔အတြက္ num ဆိုတဲ႔ variable တစ္ခုကို memory ေပၚမွာ ေနရာယူလိုက္ပါတယ္။ ျပီး တခါတည္း အဲ့ဒီ ေနရာမွာ 3 ဆိုတဲ႔ value တစ္ခုကို သိမ္းလုိက္ပါတယ္။ ေၾကျငာထားတဲ႔ ေနရာထဲမွာ value သိမ္းသြားတဲ႔ အတြက္ int ဟာ value type ျဖစ္ပါတယ္။
string st = “abc”;
ဒီလိုပါပဲ abc ဆိုတဲ႔ စာသားကို သိမ္းဖို႔အတြက္ st ဆိုတဲ႔ variable ကို ေၾကျငာလိုက္ပါတယ္။ ဒါေပမယ့္ st ထဲကိုထည့္လိုက္တဲ႔ value ျဖစ္တဲ႔ abc ဟာ st ထဲမွာ မရွိပါဘူး။ memory ရဲ့ တျခားတေနရာမွာ ရွိသြားပါတယ္။ အဲ့ဒီ abc ေရာက္သြားတဲ႔ ေနရာရဲ့ address ကသာလွ်င္ st ထဲကိုေရာက္သြားပါတယ္။ ဒါေၾကာင့္ string ဟာဆိုရင္ျဖင့္ reference type ထဲမွာ ပါ၀င္ပါတယ္။

ဒါဆိုရင္ value type နဲ႔ reference type ကို ခြဲျပီး နားလည္မယ္ထင္ပါတယ္။ ဟုတ္ျပီ ဒါဆိုရင္ value type မွာ ဘာေတြပါလဲ ဆက္ၾကည့္ရေအာင္။
value type ထဲမွာ integral type ေတြ၊
Type .................................... Range ......................................... Size
sbyte ................................ -128 to 127 ...................................... Signed 8-bit integer
byte ..................................... 0 to 255 ........................................Unsigned 8-bit integer
char ..................................... U+0000 to U+ffff ........................... Unicode 16-bit character
short .....................................-32,768 to 32,767 ........................ Signed 16-bit integer
ushort .................................0 to 65,535 ................................... Unsigned 16-bit integer
int ........................-2,147,483,648 to 2,147,483,647 .................... Signed 32-bit integer
uint .............................0 to 4,294,967,295 .................................. Unsigned 32-bit integer
long ......... -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807..... Signed 64-bit integer
ulong ..............0 to 18,446,744,073,709,551,615 ..............................Unsigned 64-bit integer
floating point typeေတြ၊
Type .........................Approximate range ..............Precision
float ..................В±1.5e−45 to В±3.4e38 ............................7 digits
double ...............В±5.0e−324 to В±1.7e308 ....................15-16 digits
decimal type၊
Type .........................Approximate Range .................Precision
decimal ................В±1.0 Г— 10−28 to В±7.9 Г— 1028 ..................28-29 significant digits
boolean data type၊ enumeration data type နဲ႔ structure data type ေတြပါ၀င္ပါတယ္။
reference type ထဲမွာေတာ့ string type၊ object type၊ array data type၊ class data type၊ interface နဲ႔ delegate type ေတြပါ၀င္ပါတယ္။
variable declaration အပိုင္းမွာ ကြၽန္ေတာ္တို႔ သိဖို႔လိုတာတခုရွိတယ္ဗ်။ ကြၽန္ေတာ္တို႔ variable name ေတြေၾကျငာတဲ႔ေနရာမွာ C# keywords ေတြနဲ႔ သြားျပီးတူလို႔မရဘူးဗ်။ Keywords ဆိုတာကေတာ့ဗ်ာ compiler က နားလည္ျပီးသား စကားလံုးေတြေပါ့။ သူတို႔ကို reserved words လို႔လဲေခၚေသးတယ္ဗ်။
Code:
abstract  event  new  struct  as  explicit  null  switch  base  extern  object
this bool false operator throw break finally out true byte fixed
override try case float params typeof catch for private uint char

foreach protected ulong checked goto public unchecked class if
readonly unsafe const implicit ref ushort continue in return using

decimal int sbyte virtual default interface sealed volatile delegate
internal short void do is sizeof while double lock stackalloc else

long static enum namespace string
ဒီဇယားထဲက keyword ေတာ္ေတာ္မ်ားမ်ားကို ကြၽန္ေတာ္တို႔ သိျပီးသားျဖစ္မွာပါ။ ဒါနဲ႔ ခင္ဗ်ားတို႔ စက္မွာ Visual Studio 2008 ျဖစ္ျဖစ္တင္ထားဦးဗ်ာ။ ေနာက္program ေတြကို VS နဲ႔ run ရေအာင္။


data types conversation

Data Type ေတြနဲ႔ ပတ္သက္ျပီး ေနာက္ထပ္သိထားစရာေလးေတြ ေျပာခ်င္ေသးတယ္ဗ်။
ကြၽန္ေတာ္တို႔ C# ထဲမွာ 3 လို႔ ရိုက္လိုက္ရင္ အဲ့ဒီ 3 ရဲ့ datatype က default အေနနဲ႔ integer type ဗ်။ ကြၽန္ေတာ္က long အျဖစ္ေရးခ်င္တယ္ဆိုရင္ 3L လို႔ ေရးရမယ္။ က်န္တာေတြကို ေအာက္မွာၾကည့္လိုက္ပါဦး။
3 => int
3L => long
3.0 or 3.0D => double
3.0F => float
3.0M => decimal
'3' => char
"3" => string
အေပၚမွာ ျပထားတာကိုၾကည့္မယ္ဆိုရင္ တကယ့္ value က 3 ျဖစ္ေပမယ့္ သူ႔ကို data type အမ်ိဳးမ်ိဳးမွာ ေရးတဲ႔ပံုစံေလးပါ။
အဲ့ဒီထဲမွာ string type ကလြဲျပီး က်န္တဲ႔ type ေတြက numeric type ေတြပါ။ ဆိုလိုတာက သူတို႔ကို ေပါင္းႏႈတ္ေျမွာက္စား လုပ္လို႔ရတယ္။ string type ကိုေတာ့ မရဘူး။ ဥပမာ
Code:
string v1 = "3";
string v2 = "4";
Console.Write(v1+v2);
ဆိုရင္ output က
Code:
34
ဆိုျပီး ျပေနလိမ့္မယ္ဗ်ာ။ ဆိုလိုတာကေတာ့ဗ်ာ 3 နဲ႔ 4 ကို number အေနနဲ႔ မေပါင္းပဲ string အေနနဲ႔ ေပါင္းသြားတာေပါ့ဗ်ာ။ ဒီေတာ့ ကြၽန္ေတာ္တို႔က string type အေနနဲ႔ ရွိေနတဲ႔ value ေတြကို ေပါင္းခ်င္တဲ႔ အခါမွာ numeric type အျဖစ္ကို ေျပာင္းဖို႔လိုလာျပီေပါ့။ ဒီလိုမ်ိဳး datatype တစ္ခုကေန တစ္ခုကို ေျပာင္းဖို႔အတြက္ဆိုရင္ Data Type Conversation အေၾကာင္းကို သိဖို႔လိုလာပါျပီ။ အဲ့ဒီလို ေျပာင္းတဲ႔ ေနရာမွာ numeric type အခ်င္းခ်င္း ေျပာင္းတာနဲ႔ numeric type နဲ႔ string type ေျပာင္းတာ ဆိုျပီး ခြဲေျပာပါမယ္။

numeric type အခ်င္းခ်င္းေျပာင္းမယ္ဆိုရင္ type conversation မွာ ၂ မ်ိဳးရွိပါတယ္။ အလြယ္မွတ္မယ္ဆိုရင္ေတာ့ ငယ္တဲ႔ type ကေန ၾကီးတဲ႔ type ကို ေျပာင္းတာ (Widening) နဲ႔ ၾကီးတဲ႔ type ကေန ငယ္တဲ႔ type ကို ေျပာင္းတာ (Narrowing) ဆိုျပီး ရွိတယ္ဗ်။ ဥပမာ int data type ကေန long ကို ေျပာင္းခ်င္ရင္ widening , long ကေန int ကို ေျပာင္းခ်င္ရင္ေတာ့ narrowing ေပါ့။
int int_var1 = 123;
long long_var1 = int_var1; // implicit conversation (widening)
long long_var2 = (long) int_var1; // explicit conversation (widening)
int int_var2 = (int) long_var1; // explicit conversation (narrowing)

*** narrowing မွာ implicit မရွိပါဘူး ***
object data type ကေန numeric type ကိုေျပာင္းမယ္ဆိုရင္....
int i = 123;
object o = i; // implicit boxing
object o = (object) i; // explicit boxing
int i = (int) o; // unboxing
string type ကေနေျပာင္းခ်င္တယ္ဆိုရင္ေတာ့ အဲ့ဒီလို type casts လုပ္လို႔ မရေတာ့ပါဘူး။ ဒါဆိုရင္ ဘယ္လိုေျပာင္းမလဲ? Convert Function ေတြကိုသံုးျပီးေတာ့ ေျပာင္းရပါမယ္။ FCL ထဲက Convert class ထဲမွာ data type တစ္ခုကေန တစ္ခုကို ေျပာင္းဖို႔ function ေတြရွိပါတယ္။ သက္ဆိုင္ရာ Data type class ေတြထဲမွာလဲ ရွိပါတယ္။ အဲ့ဒီ function ေတြနဲ႔ ေျပာင္းရပါမယ္။ ေအာက္မွာ နမူနာၾကည့္ပါ။
string st = "123";
int i = Convert.ToInt32(st);
int i = Int32.Parse(st);
string st = Convert.ToString(i);
string st = i.ToString();
ကဲ နားၾကဦးစို႔....

sample program 1.01

အခု ကြၽန္ေတာ္တို႔ program ေလးတခုေလာက္ ေရးၾကည့္ရေအာင္။ ကြၽန္ေတာ္က ဘာလုပ္ခ်င္လဲဆိုရင္ user ဆီကေန number value တစ္ခုေတာင္းမယ္ဗ်ာ။ ျပီးေတာ့ အဲ့ဒီnumber ရဲ့ square တန္ဖိုးကို ျပန္ျပီး ရိုက္ထုတ္ခ်င္တယ္။ ဟုတ္ျပီေနာ္ ဒါဆို ေရးၾကည့္ရေအာင္… အရင္ဆံုး C# မွာ Main function ကေန စေရးရမယ္ဆိုေတာ့…
Code:
static void Main()
{
}

ဆိုျပီး ေရးလိုက္မယ္ဗ်ာ။ ျပီးရင္ user ဆီကေန ေတာင္းမယ့္ number ကို memory ေပၚမွာ ခဏသိမ္းထားဖို႔ အတြက္ ေနရာယူ(variable declare) ရမယ္။ ဒါေၾကာင့္ Main function ရဲ့ အတြင္းထဲမွာ….
Code:
int num;

ဟုတ္ျပီး ဒါဆိုရင္ integer တန္ဖိုးေတြ သိမ္းဖို႔အတြက္ ေနရာေလးရျပီး။ အခု user ကို တန္ဖိုးတစ္ခုထည့္ေပးပါလို႔ ေျပာခ်င္တယ္။ user ကို ေျပာတယ္ဆိုတာက monitor မွာ output ထုတ္ျပတာပဲေလ။ ဒါေၾကာင့္ ဒီလိုေလးေျပာလိုက္မယ္….
Code:
Console.Write("Enter a number : ");

ဒါဆိုရင္ user က number တစ္ခုရိုက္ထည့္ေတ့မယ္။ အဲ့ဒီ number ကို လက္ခံဖို႔ အတြက္ C# console application မွာ ReadLine() function ကိုသံုးရမယ္။ အဲ့ဒီ function ကလည္း Console class ထဲမွာရွိတယ္။ ဒါေပမယ့္ အဲ့ဒီ ReadLine function က user ဘာရိုက္ထည့္ထည့္ string data type ပဲ return ျပန္တယ္။ ဒီေတာ့ user က 3 ရိုက္ထည့္ရင္ “3” ဆိုျပီး string type ျဖစ္သြားမယ္။ ဒါဆုိရင္ ကြၽန္ေတာ္တို႔ memory ေပၚမွာေၾကျငာထားတဲ႔ integer variable ထဲကို ဒီတိုင္းတန္းျပီး ထည့္လို႔မရေတာ့ဘူး။ string type ကေန integer type ကို convert လုပ္ေပးရေတာ့မယ္….
Code:
string temp = Console.ReadLine();num = Convert.ToInt32(temp);

ဒီလိုေရးလည္း ရပါတယ္။
Code:
num = Convert.ToInt32(Console.ReadLine());

ကဲ… ကြၽန္ေတာ္တို႔ရဲ့ num variable ထဲမွာ user ထည့္လိုက္တဲ႔ တန္ဖိုးေလး ေရာက္သြားပါျပီ။ အခု user ကို square result ထုတ္ျပဖို႔ပဲက်န္ပါေတာ့တယ္…
Code:
Console.WriteLine(“Square of {0} is {1}”, num, num*num);

Result ထုတ္ျပလိုက္တဲ႔ code ေလးကို နည္းနည္းေလး ရွင္းျပပါမယ္။ Write or WriteLine function အတြင္းမွာ parameter ၃ ခု ကြၽန္ေတာ္ေပးလိုက္ပါတယ္။ ပထမဆံုးတစ္ခု “Square of {0} is {1}” ဆိုတာမွာ {} အတြင္းထဲမွာ ေရးတဲ႔ ဟာ၂ခုကလြဲျပီး က်န္တာေတြအားလံုးက ဒီအတိုင္းေပၚပါမယ္။ အဲ့ဒါကို format string လုိ႔ ေခၚပါတယ္။ ေနာက္က Parameter ၂ ခုကေတာ့ format string ထဲမွာ ထည့္ေပးလိုက္ခ်င္တဲ႔ value ေတြပါ။ num က index 0 ၊ num*num က index 1 ေနရာမွာ ရွိပါတယ္။ အဲ့ဒီ value ေတြကို format string ထဲမွာ ျပန္ေခၚဖို႔ အတြက္ {} ကိုသံုးပါတယ္။ index 0 ေနရာက value ကို ေခၚခ်င္ရင္ {0} လို႔ေခၚရပါမယ္။ ဒီအတိုင္းပဲ index 1 ေနရာက ဟာကိုေခၚဖို႔အတြက္ေတာ့ {1} ေပါ့။ ဒါဆိုရင္ နားလည္မယ္ထင္တယ္ေနာ္။ အဲ့ဒီလို မေရးပဲ ေနာက္တစ္မ်ိဳးလည္း ေရးလို႔ရပါေသးတယ္…
Code:
Console.WriteLine(“Square of “+num+” is “+num*num);

အခု ေနာက္ဆံုးအေနနဲ႔ user ကို program ပိတ္ဖို႔အတြက္ ဒီလိုေလး ေရးေပးလိုက္ပါမယ္…
Code:
Console.Write(“\npress enter to close…”);Console.Read();

\n ကို ေနာက္တစ္လိုင္းဆင္းဖို႔ အတြက္သံုးျပီး။ Console.Read() ကေတာ့ user enter ေခါက္တာကို ေစာင့္ေနေပးပါတယ္။ ဒီ ပုစာၦေလးထဲမွာ ကြၽန္ေတာ္ Convert class နဲ႔ Console class ၂ ခုကို သံုးထားပါတယ္။ ဒါေၾကာင့္ အေပၚဆံုးမွာ ဒီလိုေလးေရးေပးလိုက္တယ္….
Code:
using System;

Code:
using System;class Program

{ static void Main(string[] args)

{ string temp; int num;

Console.Write("Enter a number : "); temp = Console.ReadLine();

num = Convert.ToInt32(temp); Console.WriteLine("Square of {0} is {1}", num, num * num);

Console.Write("\npress enter to close..."); Console.Read();

}}

Output
Code:
Enter a number : 4 Square of 4 is 16
Press enter to close…

4 က user input ပါ။
ဒီ program ေလးကို ေရးဖို႔အတြက္ Visual Studio ထဲမွာ File->New->Project->C# language->Console Application ကိုေရြးပါမယ္။ ဒါဆိုရင္ class ေတြ Main function ေနရာေတြ IDE က auto လုပ္ေပးသြားလိမ့္မယ္။ အဲ့ဒီ ထဲမွာ ရိုက္ရံုပါပဲ။ ျပီးလို႔ run မယ္ဆိုရင္ေတာ့ F5 ပါ။ အေသးစိတ္ေျပာဖို႔လိုရင္ ျပန္ေျပာပါခင္ဗ်ာ။