C#,Keywords မ်ား(၂)

Thursday, July 1, 2010

(၉) sbyte
sbyte keyword ကလည္း integral type ေတြကို store လုပ္မဲ႔ value types အမ်ိဳးစားပါပဲ ။
Type = sbyte
Range = -128 to 127
Size = Signed 8-bit integer
.NET Framework type = System.SByte
Example
sbyte sByte1 = 127;
sbyte မွ short, int ,long ,float , double , decimal တို႔ ကို တုိက္ရိုက္ေျပာင္းလဲနိုင္ပါတယ္ ။
numeric(int,float,double) type ေတြကေနၿပီး subyte ကိုလည္း တိုက္ရိုက္ေျပာင္းလဲနိုင္ပါတယ္ ။
ဒါေပမဲ႔ sbyte ရဲ့လက္ခံနိင္တဲ႔ပမာဏအထိပဲျဖစ္ပါတယ္ အဲဒီထက္ပိုႀကီးသြားရင္ေတာ႔ လက္ခံနိုင္မွာမဟုတ္ပါဘူး ။ ေနာက္ၿပိးေတာ႔ ေအာ္ပေရတာေတြနဲ႔ မသိကိ္န္းတန္ဖိုးေတြကုိလည္းတန္းၿပီးေျပာင္းလဲလို႔မရပါဘူး ။
sbyte z = x + y; // Error: conversion from int to sbyte

ေျပာင္းခ်င္တယ္ဆိုရင္ သြယ္ဝိုက္နည္းသံုးရပါမယ္ ။
sbyte z = (sbyte)(x + y); // OK: explicit conversion

(၁၀) short
short keyword ကလည္း integral data type ေတြကို store လုပ္ေပးမဲ႔ value type တစ္မ်ိဳးပါပဲ။
Type = short
Range = -32,768 to 32,767
Size = Signed 16-bit integer
.NET Framework type = System.Int16
Short ကေန implicit အေနနဲ႔ေျပာင္းလဲလို႔ရတ႔ဲ type ေတြကိုေတာ႔ေအာက္ကဇယားမွာၾကည့္ပါ ။
Implicit အေနနဲ႔ေျပာင္းလဲလို႔မရဘူးဆိုရင္ေတာ႔ casting လုပ္ၿပီး explicit အေနနဲ႔ေျပာင္းရမွာျဖစ္ပါတယ္။

(၁၁) struct
struct keyword ဆိုတာလည္း value type အမ်ိဳးစားထဲမွာပါပါတယ္ ။ သူ႔မွာထူးျခားတာေလးကေတာ႔ variable အုပ္စုေလးကို အထုပ္ပိုးေလးအေနနဲ႔ သိမ္းဆည္းထားတာပါပဲ ။
ဥပမာေလး ကိုၾကည့္လိုက္ရင္ရွင္းမွာပါ ။
public struct Book
{
public decimal price;
public string title;
public string author;
}

structထဲမွာဆိုရင္constructors,constants,fields,methods,properties,indexers,operators,events, nested types ေတြလည္းပါဝင္နိုင္ပါတယ္ ။
struct ကinterface တစ္ခုအေနနဲ႔ သံုးနိုင္ပါတယ္ ။ ဒါေပမဲ႔ struct တစ္ခုကေန struct တစ္ကို အေမြဆက္ခံ(inherit) လို႔မရပါဘူး ။ ဒါေၾကာင္႔ struct ထဲမွာပါတဲ႔ members ေတြကို protected အေနနဲ႔ေၾကျငာလို႔မရနိုင္ပါဘူး ။

public struct CoOrds
{
public int x, y;

public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}

(၁၂) uint
uint keyword ကလည္း integral data type ေတြကို store လုပ္ေပးမဲ႔ value type တစ္မ်ိဳးပါပဲ။
Type = uint
Range = 0 to 4,294,967,295
Size = Unsigned 32-bit integer
.NET Framework type = System.UInt32

uint အတြက္ suffix U ေနာက္ကထည့္ေပးၿပီး သံုးနိုင္သလို မထည့္ပဲသံုးလို႔လည္းရပါတယ္ ။
uint uInt2 = 123U;
uint ကေန ေျပာင္းလဲလို႔ရတဲ႔ value type ေတြကိုေတာ႔ ေအာက္ကဇယားမွာပဲၾကည့္လိုက္ပါ ။
(၁၃) ulong
ulong keyword ကလည္း integral data type ေတြကို store လုပ္ေပးမဲ႔ value type တစ္မ်ိဳးပါပဲ။
Type = ulong
Range = 0 to 18,446,744,073,709,551,615
Size = Unsigned 64-bit integer
.NET Framework type = System.UInt64
example
ulong uLong = 9223372036854775808;
suffix UL ကိုထည့္ေပးၿပီးလည္းအသံုးျပဳနိုင္ပါတယ္ ။ U တစ္လံုးတည္းထည္ေပးရင္ေတာ႔ အဲဒီတန္ဖိုးဟာ uint သို႔ ulong တစ္ခုခုပါပဲ ။ထည့္တဲ႔တန္ဖိုးရဲ့အရြယ္စားေပၚမွာမူတည္သြားမွာပါ ။
SampleMethod(5); // Calling the method with the int parameter
SampleMethod(5UL); // Calling the method with the ulong parameter
Ulong ကေနေျပာင္းလဲလို႔ရတဲ႔ valu type ေတြကိုေတာ႔ေအာက္ကဇယားမွာေလ႔လာၾကည့္ပါ ။

(၁၄)ushort
ushort keyword ကလည္း integral data type ေတြကို store လုပ္ေပးမဲ႔ value type တစ္မ်ိဳးပါပဲ။
Type = ushort
Range = 0 to 65,535
Size = Unsigned 16-bit integer
.NET Framework type = System.UInt16
Example
ushort myShort = 65535;
ushort ကေနေျပာင္းလဲလို႔ရတဲ႔ value types ေတြကိုေတာ႔ဇယားမွာၾကည့္ၾကည့္ပါ ။

Implicit conversions table (တိုက္ရိုက္ေျပာင္းလဲနိုင္ေသာ ပံုစံ)




အေပၚမွာျပထားတဲ႔ from ကေန to ဆိုတာ တိုက္ရိုက္ေျပာင္းလဲနိုင္တဲ႔ ပံုစံေတြပါ ။
ဥပမာ
sbyte a = 5; short b = a;
int c = a; long d = a; စသည္ျဖင္႔အားလံုးအိုေခပါတယ္ ။


explicit conversions table(သြယ္ဝိုက္ၿပီးေျပာင္းလဲရေသာပံုစံ)



ဒီဇယားကေတာ႔ explicit ေျပာင္းလဲမွဳပံုစံပါ ။ from ကေန to ကို casting နဲ႔ေျပာင္းလဲေပးနိုင္ပါတယ္။
ဥပမာ
sbyte a = 34 ; byte b =(byte) a ; ushort c = (ushort) a ;
စသျဖင္႔ ေပါ႔ ..။




Range ေတြကို သိပ္မမွတ္မိေသးရင္ထပ္ၾကည့္လို႔ရေအာင္ေအာက္မွာဇယားနဲ႔ျပထားပါေသးတယ္ ။

0 comments:

Post a Comment

ဒီPost ရဲ႕ေရးသားခ်က္နဲ႕ပက္သက္ျပီး ေ၀ဖန္အၾကံျပဳႏုိင္ပါတယ္။