ุนูุฏูุง ููุนูู ู ู ุงู Casting:
- Implicit Castingย (automatically) - converting a smaller type
to a larger type size
char
ย โยint
ย โยlong
ย โยfloat
ย โยdouble
- Explicit Castingย (manually) - converting a larger type to a smaller size type
double
ย โยfloat
ย โยlong
ย โยint
ย โยchar
Implicit Casting
No need of Casting
int myInt = 9;
double myDouble = myInt //Automatic casting: int to double
//Safe Casting , No RunTime Errors
Explicit Casting
Memory
- ุจูุญุตู ู ุน ุงู unsafe castingุ ุงู ู ู ูู ุงู data type ูุดูู ุงูููู ุฉ ุงููู ูุฏููุงูู ูู ู ูู ูุง
- ูุนูู ุฃุญุงูู ุฃุญุท ุญุงุฌุฉ ูุจูุฑุฉ ูู ุญุงุฌุฉ ุตุบูุฑุฉ ูู ุงู memory ูู ุด ูููุจู
int x = 5;
double y = 50;
x = (int) y;
Y = long.MaxValue;
x = (int) y;
// overflow, but no error here, just garbage value
- ูู ุญุงูุฉ ุงูู ุงุฏูุชูู ููู ุฉ ู ุชูุฏุฑุด ุชุดููู ูู ุงูุญุงูุฉ ุฏู ุงู CLR ู ุด ุจูุฑู ู exception ููููู ุงูุจุฑูุงู ุฌ ููู ุงูุญุงูุฉ ุฏู ุงู exception unhandled ูุจููุถู ูุฏุง ูุญุฏ ุงู ุง ุจุนู ูู Cs Handle Exception
- ุจูู ูู ุงูู ูู ูุฑู ูุจุนุฏูู ูู ุฎูุตุช ูุนุงูุฒ ุฃูู ู ุจุฑู ู ุงููู ูููุง ูุฃุจุฏุฃ ุฃู ูู ุชุงูู
int x = 300;
byte y = (byte) x; // byte -> 255
// 300 = 255 + 44
Checked
- ุจูุทูุน Exception ูู ุญุตู Overflow ูู ุงูุญุงุฌุฉ ุงููู ุจุฎุฒู ูููุง
- ููู ุงูู ุณุชูุจู ูู ุนุงูุฒ ุชุนู ู handling ุนุงุฏู
int x = 300;
checked
{
byte y = (byte) x; // Unhandled exception. System.OverflowException
}
Unchecked
- ูู ูุงูุฏุชูุง ุงู ูู ุญุตู overflow ู ูุฑู ูุด exception ูู ุด ุจูุชู ุงุณุชุฎุฏุงู ูุง ุบูุฑ ู ุน ุงูoverflow errors
- ุจุณุชุฎุฏู ูุง ุฌูุง ุงู checked ุจููู ูู ููู ุฌุฒุก ุฌูุงู ู ุด ุนุงูุฒ ูุชุนู ูู check
Data Type
C# Method (Parse)
- ูู ุนุงูุฒ ุฃุบูุฑ ู
ู ููุน ููุชุงูู ููู ู
ุด ุณุงู
ุญ ุจูุฏุง ุจุณุชุฎุฏู
Parse
int a = Console.ReadLine(); // String -> int : ERROR
int a = int.Parse(Console.ReadLine());
// All data types have that exept Strings
// ERROR
int x = 5;
string y = string.Parse(x);
.NET Method (Convert)
ู
ู
ูู ูุบูุฑ ู
ู ููุน ูููุน ุชุงูู ุจุงุณุชุฎุฏุงู
ุดููุฉ methods ูุฏุง
Convert.ToBoolean
,ย Convert.ToDouble
,ย Convert.ToString
,ย Convert.ToInt32
ย (int
) andย Convert.ToInt64
ย (long
)
int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string
Info
ุจุงููุณุจุฉ ูู Convert ููููุง ู ูุฒุฉ ูููุฉ ุฌุฏูุง ููู ุฅู ูู ุจุชุนู ู convert ู int ู ุซููุง ูุงูููู ุฉ null ููู ููุญุทูุง ุจ zero ููููุฏู ุงูุฏููุง ููุญุฏู