C#-এ string (alias of System.String) একটি রেফারেন্স টাইপ কিন্তু এটি ভ্যালু সেম্যান্টিক্স ফলো করে। string.Equals() মেথড বা == অপারেটর ব্যবহার করে দুটি স্ট্রিং-এর কন্টেন্ট সমান কিনা যাচাই করা যায়।
উদাহরণ ১: সমান স্ট্রিং
using System;
public class Demo {
public static void Main(string[] args) {
string str1 = "জন";
string str2 = "জন";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
}
}
আউটপুট
String 1 = জন String 2 = জন String 1 is equal to String 2: True
উদাহরণ ২: অসমান স্ট্রিং
using System;
public class Demo {
public static void Main(string[] args) {
string str1 = "টম";
string str2 = "কেভিন";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
}
}
আউটপুট
String 1 = টম String 2 = কেভিন String 1 is equal to String 2: False
String.Equals() ওভারলোডসমূহ
// ১. Instance method (null-safe)
string s1 = "হ্যালো";
string s2 = null;
bool result1 = s1.Equals(s2); // False (NullReferenceException নয়)
// ২. Static method (উভয় null-safe)
bool result2 = string.Equals(s1, s2); // False
bool result3 = string.Equals(null, null); // True
// ৩. StringComparison Enum দিয়ে কালচার/কেস সংবেদনশীলতা নিয়ন্ত্রণ
string s3 = "HELLO";
string s4 = "hello";
Console.WriteLine(s3.Equals(s4)); // False (Ordinal, case-sensitive)
Console.WriteLine(s3.Equals(s4, StringComparison.OrdinalIgnoreCase)); // True
Console.WriteLine(string.Equals(s3, s4, StringComparison.CurrentCultureIgnoreCase)); // True
StringComparison অপশনসমূহ
| এনাম ভ্যালু | বিবরণ |
|---|---|
Ordinal | বাইনারি কম্পারিজন (ফাস্ট, ডিফল্ট) |
OrdinalIgnoreCase | বাইনারি কম্পারিজন, কেস-ইনসেনসিটিভ (সুপারিশকৃত) |
CurrentCulture | বর্তমান কালচার নিয়ম অনুযায়ী |
CurrentCultureIgnoreCase | বর্তমান কালচার, কেস-ইনসেনসিটিভ |
InvariantCulture | ইনভেরিয়েন্ট কালচার (সংস্কৃতি-নিরপেক্ষ) |
InvariantCultureIgnoreCase | ইনভেরিয়েন্ট কালচার, কেস-ইনসেনসিটিভ |
== অপারেটর vs Equals()
string a = "হ্যালো";
string b = "হ্যালো";
// উভয়ই একই ফলাফল দেয় (ভ্যালু বেইসড)
Console.WriteLine(a == b); // True
Console.WriteLine(a.Equals(b)); // True
Console.WriteLine(string.Equals(a, b)); // True
// কিন্তু null হ্যান্ডলিং-এ পার্থক্য:
string c = null;
Console.WriteLine(a == c); // False (নাল চেক বিল্ট-ইন)
Console.WriteLine(a.Equals(c)); // False (নাল চেক বিল্ট-ইন)
Console.WriteLine(c.Equals(a)); // NullReferenceException! ❌
Console.WriteLine(string.Equals(c, a)); // False ✅ (নাল-সেফ)
স্ট্রিং ইন্টার্নিং (String Interning)
// কম্পাইল টাইম কনস্ট্যান্ট স্ট্রিং ইন্টার্ন হয় (সেম রেফারেন্স)
string s1 = "হ্যালো";
string s2 = "হ্যালো";
Console.WriteLine(object.ReferenceEquals(s1, s2)); // True
// রানটাইম কনক্যাটেনেশন ইন্টার্ন হয় না
string s3 = "হ্যাল" + "ো";
Console.WriteLine(object.ReferenceEquals(s1, s3)); // True (কম্পাইলার অপ্টিমাইজেশন)
string s4 = "হ্যাল";
string s5 = s4 + "ো";
Console.WriteLine(object.ReferenceEquals(s1, s5)); // False (রানটাইম)
// ম্যানুয়াল ইন্টার্নিং
string s6 = string.Intern(s5);
Console.WriteLine(object.ReferenceEquals(s1, s6)); // True
সেরা অনুশীলন (Best Practices)
- সাধারণ ব্যবহারের জন্য:
==অপারেটর ব্যবহার করুন (পঠনযোগ্য, নাল-সেফ)। - কেস-ইনসেনসিটিভ তুলনার জন্য:
string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase) - কলচার-স্পেসিফিক তুলনার জন্য:
StringComparison.CurrentCultureIgnoreCase - নাল-সেফ স্ট্যাটিক মেথড:
string.Equals(s1, s2)প্রেফার করুন। - Object.Equals এড়িয়ে চলুন: বক্সিং এবং নাল রিস্ক থাকে।