The question
is not tagged with UI type WindowsForms, WPF, ASP.NET etc
. and it is not mentioned
which control is to be validated.
In
WindowsForms
the
Validating
event of the
Control
can be used to test for a condition and if the condition is not satisfied then the
System.ComponentModel.CancelEventArgs
argment property
Cancel
can be set to true to retain focus in the
Control
till the condition is satisified.
If a
TextBox
is to be validated in
WindowsForms
then the following code can be used
textBox1.Validating += (sender, args) => {
if(!textBox1.Text.Equals("hello", StringComparison.InvariantCulture))
args.Cancel=true;
};
If the purpose is only to a have
function
to check for the condition then the following method using
string.Equals
method can be used as it offers
StringComparison.InvariantCulture
option.
public bool IsValid(string stringToTest){
return stringToTest.Equals("hello",StringComparison.InvariantCulture);
};