ููŠ ู„ุบุฉ C# ูˆุงู„ู„ุบุงุช ุงู„ู„ูŠ ู‚ุฑูŠุจุฉ ู…ู†ู‡ุง ููŠ ุงู„ุชุนุงู…ู„ ู…ุน ุงู„ู€ MemoryุŒ ู„ู…ุง ุจู†ุญุชุงุฌ ู†ุบูŠุฑ ู†ูˆุน ุจูŠุงู†ุงุช Variable ู…ุนูŠู† ู„ู†ูˆุน ุจูŠุงู†ุงุช ุชุงู†ูŠุŒ ุจู†ุนู…ู„ ุญุงุฌุฉ ุงุณู…ู‡ุง Casting ุฃูˆ Type Conversion. ุงู„ู…ูˆุถูˆุน ุฏู‡ ู„ูŠู‡ ู†ูˆุนูŠู† ุฃุณุงุณูŠูŠู†:

  • ุงู„ู€ Implicit Casting: ูˆุฏู‡ ุจูŠุญุตู„ ุชู„ู‚ุงุฆูŠ ู…ู† ุงู„ู€ Compiler. ุจูŠูƒูˆู† ูˆุฃู†ุช ุจุชุญูˆู„ ู…ู† ู†ูˆุน ุจูŠุงู†ุงุช ุญุฌู…ู‡ ุตุบูŠุฑ ููŠ ุงู„ู€ Memory ู„ู†ูˆุน ุญุฌู…ู‡ ุฃูƒุจุฑ. ุงู„ุชุฑุชูŠุจ ุงู„ุชู‚ุฑูŠุจูŠ ู„ู„ุชุญูˆูŠู„ ุงู„ุขู…ู† ุฏู‡ ุจูŠูƒูˆู† ูƒุงู„ุชุงู„ูŠ: char โ†’ int โ†’ long โ†’ float โ†’ double

  • ุงู„ู€ Explicit Casting: ูˆุฏู‡ ุจุชุญุชุงุฌ ุชุนู…ู„ู‡ ุฃู†ุช ูŠุฏูˆูŠู‹ุง ูƒู€ Developer. ุจูŠูƒูˆู† ูˆุฃู†ุช ุจุชุญุงูˆู„ ุชุญูˆู„ ู…ู† ู†ูˆุน ุจูŠุงู†ุงุช ุญุฌู…ู‡ ูƒุจูŠุฑ ููŠ ุงู„ู€ Memory ู„ู†ูˆุน ุญุฌู…ู‡ ุฃุตุบุฑ. ุงู„ุชุฑุชูŠุจ ุงู„ุชู‚ุฑูŠุจูŠ ู„ู„ุชุญูˆูŠู„ ุฏู‡ ุจูŠูƒูˆู† ุนูƒุณ ุงู„ู€ Implicit Casting ูˆู…ู…ูƒู† ูŠุญุตู„ ููŠู‡ ูู‚ุฏุงู† ู„ู„ู€ Data: double โ†’ float โ†’ long โ†’ int โ†’ char

ุงู„ู€ Type Conversion (ุณูˆุงุก Implicit ุฃูˆ Explicit) ุจูŠูƒูˆู† ู…ู…ูƒู† ุจุณ ู„ูˆ ู†ูˆุนูŠู† ุงู„ู€ Data ู…ุชูˆุงูู‚ูŠู† ู…ุน ุจุนุถ. ู„ูˆ ู…ููŠุด ุชูˆุงูู‚ุŒ ุบุงู„ุจู‹ุง ู‡ูŠุทู„ุนู„ูƒ Compiler Error.

Implicit Casting

ุงู„ู†ูˆุน ุฏู‡ ู…ู† ุงู„ู€ Casting ุจูŠุญุตู„ ู„ูˆุญุฏู‡ ุฃูˆุชูˆู…ุงุชูŠูƒ ู…ู† ุบูŠุฑ ู…ุง ุชูƒุชุจ ุฃูŠ ุญุงุฌุฉ ุฒูŠุงุฏุฉ ููŠ ุงู„ู€ Code. ุฏู‡ ุจูŠูƒูˆู† ูˆุฃู†ุช ุจุชุญูˆู„ ู…ู† ู†ูˆุน ุจูŠุงู†ุงุช ุตุบูŠุฑ (ุฒูŠ int) ู„ู†ูˆุน ุจูŠุงู†ุงุช ุฃูƒุจุฑ (ุฒูŠ double). ุงู„ู€ Compiler ุจูŠุนู…ู„ ุฏู‡ ู„ุฃู† ุงู„ุชุญูˆูŠู„ ููŠ ุงู„ุงุชุฌุงู‡ ุฏู‡ ุจูŠูƒูˆู† ุขู…ู† (Safe Casting)ุŒ ูˆู…ุด ุจูŠุญุตู„ ููŠู‡ ุฃูŠ ูู‚ุฏุงู† ู„ู„ู€ Data ูˆู„ุง ุจุชุทู„ุนู„ูƒ RunTime Errors ู„ูŠู‡ุง ุนู„ุงู‚ุฉ ุจุงู„ู…ูˆุถูˆุน ุฏู‡.

int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
// Safe Casting, No RunTime Errors
Console.WriteLine(myDouble); // Output: 9

Explicit Casting

ุงู„ู†ูˆุน ุฏู‡ ุจุชุญุชุงุฌ ุชุนู…ู„ู‡ ุฃู†ุช ุจู†ูุณูƒ ููŠ ุงู„ู€ Code ุจุงุณุชุฎุฏุงู… ุงู„ู€ Cast Operator ุงู„ู„ูŠ ู‡ูˆ ุงู„ุฃู‚ูˆุงุณ () ูˆุจุชุญุท ุฌูˆุงู‡ุง ู†ูˆุน ุงู„ุจูŠุงู†ุงุช ุงู„ู„ูŠ ุนุงูŠุฒ ุชุญูˆู„ ู„ูŠู‡. ุฏู‡ ุจูŠุญุตู„ ุบุงู„ุจู‹ุง ู„ู…ุง ุจุชุญูˆู„ ู…ู† ู†ูˆุน ุจูŠุงู†ุงุช ูƒุจูŠุฑ ู„ู†ูˆุน ุจูŠุงู†ุงุช ุฃุตุบุฑ. ุงู„ู†ูˆุน ุฏู‡ ูŠุนุชุจุฑ ุบูŠุฑ ุขู…ู† (Unsafe Casting) ู„ุฃู† ู…ู…ูƒู† ูŠุญุตู„ ููŠู‡ ูู‚ุฏุงู† ู„ู„ู€ Data (Data Loss) ู„ูˆ ุงู„ู‚ูŠู…ุฉ ุงู„ู„ูŠ ุจุชุญูˆู„ู‡ุง ุฃูƒุจุฑ ู…ู† ุงู„ู€ Type ุงู„ุฌุฏูŠุฏ ูŠู‚ุฏุฑ ูŠุดูŠู„ู‡ุง.

Memory

ู…ุน ุงู„ู€ Explicit CastingุŒ ุฎุตูˆุตู‹ุง ู„ู…ุง ุจุชุญุงูˆู„ ุชุญุท ู‚ูŠู…ุฉ ูƒุจูŠุฑุฉ ุฌูˆู‡ Variable ู…ู† ู†ูˆุน ุตุบูŠุฑุŒ ู…ู…ูƒู† ุงู„ู€ Data Type ุฏู‡ ู…ูŠู‚ุฏุฑุด ูŠุดูŠู„ ุงู„ู‚ูŠู…ุฉ ูƒู„ู‡ุงุŒ ูˆุฏู‡ ุงู„ู„ูŠ ุจูŠุญุตู„ ู…ุน ุงู„ู€ unsafe casting.

ู…ุซุงู„ ูŠูˆุถุญ ู…ุดูƒู„ุฉ ุงู„ู€ Overflow:

int x;
double y = 50.5;
 
x = (int) y; // Converting double to int. Fractional part is lost.
Console.WriteLine(x); // Output: 50
 
long largeValue = long.MaxValue; // Max value for long
// largeValue = 9223372036854775807
 
int smallInt;
// Trying to put the large long value into an int
smallInt = (int) largeValue;
// Overflow occurs here. int cannot hold this value.
// CLR does not throw an exception by default. Result is garbage value.
Console.WriteLine(smallInt); // Output will be due to overflow

ููŠ ุงู„ู…ุซุงู„ ุงู„ู„ูŠ ูุงุชุŒ ู„ู…ุง ุจู†ุญุงูˆู„ ู†ุญุท ู‚ูŠู…ุฉ ุฃูƒุจุฑ ุจูƒุชูŠุฑ ู…ู† ุงู„ู„ูŠ ุงู„ู€ int ูŠู‚ุฏุฑ ูŠุดูŠู„ู‡ุง (ู…ู† ุงู„ู€ long.MaxValue)ุŒ ุจูŠุญุตู„ ุญุงุฌุฉ ุงุณู…ู‡ุง Overflow. ุงู„ู€ CLR (Common Language Runtime) ููŠ ุงู„ุญุงู„ุฉ ุฏูŠ ู…ุด ุจูŠุฑู…ูŠ Exception ูŠูˆู‚ู ุงู„ุจุฑู†ุงู…ุฌุŒ ูˆุฏูŠ ุญุงุฌุฉ ู…ู…ูƒู† ุชุนู…ู„ ู…ุดุงูƒู„ ู„ูˆ ู…ุด ูˆุงุฎุฏ ุจุงู„ูƒ.

ู…ุซุงู„ ุชุงู†ูŠ ุนู„ู‰ ุงู„ู€ Overflow ู…ุน ุฃู†ูˆุงุน ุฃุตุบุฑ:

int x = 300;
// byte can hold values from 0 to 255
byte y = (byte) x; // Trying to put 300 into a byte. Overflow occurs.
// The value will wrap around based on byte's range (300 % 256 roughly)
Console.WriteLine(y); // Output: 44 (garbage value relative to original 300)

Checked

ู„ูˆ ุนุงูŠุฒ ุชุชุฃูƒุฏ ุฅู† ุนู…ู„ูŠุฉ ุงู„ู€ Explicit Casting ู…ุด ู‡ุชุณุจุจ Overflow ูˆุชุทู„ุน Exception ู„ูˆ ุญุตู„ Overflow ูุนู„ู‹ุงุŒ ู…ู…ูƒู† ุชุณุชุฎุฏู… ุงู„ู€ Keyword ุงู„ู„ูŠ ุงุณู…ู‡ุง checked.

int x = 300;
try
{
    checked // Enable overflow checking
    {
        // CLR will check for overflow here
        byte y = (byte)x; // This will throw System.OverflowException
        Console.WriteLine(y);
    }
}
catch (OverflowException ex)
{
    Console.WriteLine($"An overflow occurred: {ex.Message}");
    // Handle the exception here
}
// Output: An overflow occurred: Arithmetic operation resulted in an overflow.

ุงุณุชุฎุฏุงู… ุงู„ู€ checked ู…ููŠุฏ ู„ู…ุง ุชูƒูˆู† ู…ุด ู…ุชุฃูƒุฏ ุฅู† ุงู„ู‚ูŠู…ุฉ ุงู„ู„ูŠ ุจุชุญูˆู„ู‡ุง ู‡ุชู†ุงุณุจ ุงู„ู€ Type ุงู„ุฌุฏูŠุฏ ูˆุนุงูŠุฒ ุงู„ุจุฑู†ุงู…ุฌ ูŠุทู„ุน Error ูˆุงุถุญ ุจุฏู„ ู…ุง ูŠุญุท ู‚ูŠู…ุฉ ุบู„ุท.

Unchecked

ุงู„ู€ Keyword ุงู„ู„ูŠ ุงุณู…ู‡ุง unchecked ุจุชุนู…ู„ ุงู„ุนูƒุณ ุชู…ุงู…ู‹ุง ู…ู† checked. ู„ูˆ ุงุณุชุฎุฏู…ุชู‡ุง ุญูˆุงู„ูŠู† ุฌุฒุก ู…ู† ุงู„ู€ CodeุŒ ุจุชุถู…ู† ุฅู† ุงู„ู€ CLR ู…ุด ู‡ูŠุชุฃูƒุฏ ู„ูˆ ููŠู‡ Overflow ูˆู…ุด ู‡ูŠุฑู…ูŠ Exception ุญุชู‰ ู„ูˆ ุญุตู„. ุงุณุชุฎุฏุงู…ู‡ุง ุงู„ุฃุณุงุณูŠ ุจูŠูƒูˆู† ุฌูˆู‡ checked block ู„ูˆ ููŠู‡ ุฌุฒุก ุตุบูŠุฑ ุฌูˆู‡ ุงู„ู€ Block ุฏู‡ ู…ุด ุนุงูŠุฒ ูŠุชุทุจู‚ ุนู„ูŠู‡ ุงู„ู€ Check ุจุชุงุน ุงู„ู€ Overflow ู„ุฃูŠ ุณุจุจ.

int x = 300;
checked // Outer checked block
{
    // checked behavior is active
    unchecked // Inner unchecked block
    {
        // No overflow check in this block
        byte y = (byte)x; // Overflow occurs, but no exception
        Console.WriteLine($"Inside unchecked: {y}"); // Output: Inside unchecked: 44
    }
    // checked behavior resumes here
}

Data Type

ุบูŠุฑ ุทุฑูŠู‚ุฉ ุงู„ู€ Cast Operator ุงู„ู„ูŠ ุดูู†ุงู‡ุงุŒ ููŠู‡ ุทุฑู‚ ุชุงู†ูŠุฉ ู„ุชุญูˆูŠู„ ุงู„ู€ Data Types ุฎุตูˆุตู‹ุง ุจูŠู† ุงู„ุฃู†ูˆุงุน ุงู„ู„ูŠ ู…ุด ู…ุชูˆุงูู‚ุฉ ู…ุจุงุดุฑุฉ ุฒูŠ ุงู„ู€ string ูˆุงู„ุฃุฑู‚ุงู….

C# Method (Parse)

ู„ูˆ ุนุงูŠุฒ ุชุญูˆู„ string ู„ู†ูˆุน ุจูŠุงู†ุงุช ุชุงู†ูŠ (ุฒูŠ ุฑู‚ู… ุตุญูŠุญ int ู…ุซู„ุงู‹)ุŒ ู…ุด ู‡ูŠู†ูุน ุชุณุชุฎุฏู… ุงู„ู€ Cast Operator ุงู„ุนุงุฏูŠ ู…ุจุงุดุฑุฉ. ู‡ู†ุง ุจูŠูŠุฌูŠ ุฏูˆุฑ ุงู„ู€ Parse Method. ุชู‚ุฑูŠุจู‹ุง ูƒู„ ุงู„ู€ Primitive Data Types (ุฒูŠ int, double, boolุŒ ูˆู‡ูƒุฐุง) ุจูŠูƒูˆู† ููŠู‡ู… static Parse Method ุจุชุงุฎุฏ string ูƒู€ parameter ูˆุจุชุญุงูˆู„ ุชุญูˆู„ู‡ุง ู„ู„ู€ Type ุฏู‡.

// Example: Reading console input (which is string) and converting to a number
// int a = Console.ReadLine(); // Compile-time ERROR: Cannot implicitly convert string to int
 
Console.Write("Enter a number: ");
string input = Console.ReadLine();
int a = int.Parse(input); // Use Parse to convert the input string to int
Console.WriteLine($"You entered: {a}");
 
// Example: Converting string to a boolean value
string boolString1 = "true";
bool b1 = bool.Parse(boolString1);
Console.WriteLine(b1); // Output: True
 
string boolString2 = "false";
bool b2 = bool.Parse(boolString2);
Console.WriteLine(b2); // Output: False
 
// Note: If the string cannot be parsed (e.g., int.Parse("Hello")),
// a Run-time Error (FormatException) will occur.

ุงู„ู€ Parse Method ู…ููŠุฏุฉ ุฌุฏู‹ุง ู„ุชุญูˆูŠู„ ุงู„ู€ input ุงู„ู„ูŠ ุจูŠูŠุฌูŠ ุนู„ู‰ ุดูƒู„ string (ุฒูŠ ู…ู† ุงู„ู€ Console ุฃูˆ ู…ู„ูุงุช ุงู„ู€ Text) ู„ู„ู€ Data Type ุงู„ู…ู†ุงุณุจ.

.NET Method (Convert)

ุงู„ู€ .NET Framework ููŠู‡ Class ุงุณู…ู‡ุง Convert ูˆุฏูŠ ููŠู‡ุง ู…ุฌู…ูˆุนุฉ ูƒุจูŠุฑุฉ ู…ู† ุงู„ู€ Static Methods ุงู„ู„ูŠ ุจุชุณู‡ู„ ุนู…ู„ูŠุฉ ุงู„ุชุญูˆูŠู„ ุจูŠู† ุฃู†ูˆุงุน ู…ุฎุชู„ูุฉ ู…ู† ุงู„ู€ Data TypesุŒ ุญุชู‰ ู„ูˆ ู…ูƒุงู†ูˆุด ู…ุชูˆุงูู‚ูŠู† ุจุดูƒู„ ู…ุจุงุดุฑ ุฒูŠ ุงู„ู€ string ูˆุงู„ุฃุฑู‚ุงู…. ุฃู…ุซู„ุฉ ุนู„ู‰ ุงู„ู€ Methods ุฏูŠ: Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (ู„ู€ int)ุŒ ูˆ Convert.ToInt64 (ู„ู€ long).

int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
string myString = "123";
 
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 (will lose the decimal part)
Console.WriteLine(Convert.ToString(myBool));   // convert bool to string (will output "True")
Console.WriteLine(Convert.ToInt32(myString));  // convert string "123" to int (will output 123)
 
// Note: Convert Methods can handle null values safely in some cases.
// For example, Convert.ToInt32(null) will result in 0 instead of an exception.
// However, if the string has invalid format (like letters), it will still throw an exception (e.g., FormatException).

ุงู„ู€ Convert Class ุจุชูˆูุฑ ู…ุฑูˆู†ุฉ ุฃูƒุจุฑ ููŠ ุงู„ุชุญูˆูŠู„ ุจูŠู† ุฃู†ูˆุงุน ู…ุฎุชู„ูุฉ ู…ู‚ุงุฑู†ุฉ ุจุงู„ู€ Cast Operator ุฃูˆ ุงู„ู€ Parse Method ู„ูˆุญุฏู‡ุง.

Info

ุจุงู„ู†ุณุจุฉ ู„ู„ Convert ูููŠู‡ุง ู…ูŠุฒุฉ ู‚ูˆูŠุฉ ุฌุฏู‹ุง ูˆู‡ูŠ ุฅู† ู„ูˆ ุจุชุนู…ู„ convert ู„ int ู…ุซู„ู‹ุง ูˆุงู„ู‚ูŠู…ุฉ null ูู‡ูˆ ู‡ูŠุญุทู‡ุง ุจ zero ู‡ูŠู‡ู†ุฏู„ ุงู„ุฏู†ูŠุง ู„ูˆุญุฏู‡

Using TryParse Method

ุฒูŠ ู…ุง ุดูู†ุงุŒ ุงู„ู€ Parse Method ุจุชุฑู…ูŠ Exception ู„ูˆ ุงู„ู€ string ุงู„ู„ูŠ ุจู†ุญุงูˆู„ ู†ุญูˆู„ู‡ุง ู…ุด format ู…ู†ุงุณุจ ู„ู„ู€ Type ุงู„ู„ูŠ ุจู†ุญูˆู„ ู„ูŠู‡. ุนุดุงู† ู†ุชุฌู†ุจ ุงู„ู€ Exceptions ุฏูŠ ูˆู†ุฎู„ูŠ ุงู„ู€ Code ุจุชุงุนู†ุง ุฃุฃู…ู† ู„ู…ุง ู†ุญูˆู„ ู…ู† stringุŒ ุจู†ุณุชุฎุฏู… ุงู„ู€ TryParse Method.

ุงู„ู€ TryParse Method ู…ูˆุฌูˆุฏุฉ ุจุฑุถู‡ ูƒู€ Static Method ููŠ ู…ุนุธู… ุงู„ู€ Value Types (ุฒูŠ int.TryParse, double.TryParse, bool.TryParse). ู‡ูŠ ุจุชุญุงูˆู„ ุชุญูˆู„ ุงู„ู€ stringุŒ ูˆู„ูˆ ู†ุฌุญุช ุจุชุฑุฌุน true ูˆุจุชุญุท ุงู„ู†ุชูŠุฌุฉ ููŠ Variable ุจุชุญุฏุฏู‡ ุจุงุณุชุฎุฏุงู… ุงู„ู€ out Keyword. ู„ูˆ ูุดู„ุช ููŠ ุงู„ุชุญูˆูŠู„ุŒ ุจุชุฑุฌุน false ูˆุงู„ู‚ูŠู…ุฉ ููŠ ุงู„ู€ Variable ุจุชุงุน ุงู„ู€ out ุจุชูƒูˆู† ุงู„ู‚ูŠู…ุฉ ุงู„ุงูุชุฑุงุถูŠุฉ ู„ู„ู€ Type ุฏู‡ (ุฒูŠ 0 ู„ู„ุฃุฑู‚ุงู…ุŒ false ู„ู„ู€ boolุŒ ูˆู‡ูƒุฐุง).

string str1 = "100";
int i1;
// Attempt to parse str1 into an int. Result goes into IsConverted1 (true/false), value into i1 if successful.
bool IsConverted1 = int.TryParse(str1, out i1);
 
if (IsConverted1)
{
    Console.WriteLine($"String \"{str1}\" was successfully converted to int: {i1}"); // Output: String "100" was successfully converted to int: 100
}
else
{
    Console.WriteLine($"Failed to convert string \"{str1}\" to int.");
}
 
string str2 = "Hello";
int i2;
// Attempt to parse str2 into an int. This will fail.
bool IsConverted2 = int.TryParse(str2, out i2);
 
if (IsConverted2)
{
    Console.WriteLine($"String \"{str2}\" was successfully converted to int: {i2}");
}
else
{
    Console.WriteLine($"Failed to convert string \"{str2}\" to int."); // Output: Failed to convert string "Hello" to int.
    Console.WriteLine($"Default value of i2 after failed TryParse: {i2}"); // Output: Default value of i2 after failed TryParse: 0
}

ุงุณุชุฎุฏุงู… ุงู„ู€ TryParse ุฃุญุณู† ููŠ ุฃุบู„ุจ ุงู„ุฃูˆู‚ุงุช ู…ู† ุงู„ู€ Parse ู„ูˆ ู…ุด ู…ุชุฃูƒุฏ ู…ู† ุงู„ู€ format ุจุชุงุน ุงู„ู€ string ุงู„ู„ูŠ ุฌุงูŠุŒ ู„ุฃู†ู‡ ุจูŠุฎู„ูŠูƒ ุชุชุนุงู…ู„ ู…ุน ุญุงู„ุฉ ุงู„ูุดู„ ู…ู† ุบูŠุฑ ู…ุง ุงู„ุจุฑู†ุงู…ุฌ ูŠุถุฑุจ ุจู€ Exception.