Sobes.tech
Junior — Middle

Kuidas toimub stringide võrdlus programmis?

sobes.tech AI

Vastus AI-lt

In C#, strings are compared using methods of the String class. The main ways are:

  • string.Equals(str1, str2) — compares strings considering case sensitivity.
  • string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) — comparison ignoring case.
  • The == operator — compares the content of strings, not references.

Example:

string a = "hello";
string b = "Hello";

bool result1 = a == b; // false
bool result2 = string.Equals(a, b, StringComparison.OrdinalIgnoreCase); // true

It is important to consider culture and case sensitivity when comparing, for which there are different options in StringComparison.