C#-তে একটি নাম্বারের স্ট্রিং রিপ্রেজেন্টেশনকে ইন্টিজারে কনভার্ট করতে `int.Parse` বা `Convert.ToInt32` মেথড ব্যবহার করা হয়। যদি স্ট্রিংটি কনভার্ট করা না যায়, তবে `int.Parse` বা `Convert.ToInt32` মেথড এক্সসেপশন রিটার্ন করে।
Convert.ToInt32 null ভ্যালু অনুমতি দেয়, এটি কোনো এরর থ্রো করে না। int.Parse null ভ্যালু অনুমতি দেয় না, এবং এটি `ArgumentNullException` এরর থ্রো করে।
int.Parse উদাহরণ
উদাহরণ
class Program
{
static void Main()
{
int res;
string myStr = "5000";
res = int.Parse(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
Console.ReadLine();
}
}
আউটপুট
Converting String is a numeric representation: 5000
Convert.ToInt32 null এর সাথে উদাহরণ
উদাহরণ
class Program
{
static void Main()
{
int res;
string myStr = null;
res = Convert.ToInt32(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
Console.ReadLine();
}
}
আউটপুট
Converting String is a numeric representation: 0
int.Parse null এর সাথে উদাহরণ (এক্সসেপশন)
উদাহরণ
class Program
{
static void Main()
{
int res;
string myStr = null;
res = int.Parse(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
Console.ReadLine();
}
}
আউটপুট
Unhandled exception. System.ArgumentNullException: Value cannot be null.