why you put a ? in Console.ReadLine() and ToLower() [duplicate] - c#

This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 7 months ago.
So I just done it as a habit now but I want to know how it really works. Console.ReadLine()?.ToLower() ?? ""; So why do you put a ? in between .ToLower() and Console.ReadLine(). I know that after the .ToLower() you put ?? ""; to make a null input into a string. But what about the other question mark?

Single question mark (?) means null check.
Basically, if Console.ReadLine is null, the other part will not be implemented.
In other words, Console.ReadLine()?.ToLower() is equal to this
var dummy = Console.ReadLine();
if(dummy != null)
{
dummy.ToLower()....
}

Related

What is the negation of the test "is"? [duplicate]

This question already has answers here:
C# : 'is' keyword and checking for Not
(13 answers)
Closed 4 years ago.
To test if an object is of a certain type then we write if ( myObject is SomeClassName )
But how do we write if I want to test that the object is not of the mentionned type ?
if (!(myObject is SomeClassName))
Is probably the cleanest way of checking not is. Just checks if it is the type of object then flips it.
There isn't one, you have to wrap it in a ! operator:
if (!(myObject is SomeClassName))

handling null and string.empty using ?? operator in C# [duplicate]

This question already has answers here:
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 4 years ago.
I want to handle "" and null while assigning value to the property of the class.
So how can i handle the same. Below is my example which works for null. But also want to handle empty string
Id = characater.Id ?? System.Guid.NewGuid().ToString(),
Use string.IsNullOrEmpty along with the ?: Operator.
Id = string.IsNullOrEmpty(characater.Id)
? System.Guid.NewGuid().ToString()
: characater.Id;
If you you also want to check for white space characters line spaces, line breaks, tabs, you can use String.IsNullOrWhiteSpace instead.

Is it okay to check for null with 'is' keyword? [duplicate]

This question already has answers here:
What is the difference between "x is null" and "x == null"?
(3 answers)
What is a difference between "foo is null" and "foo == null"
(2 answers)
Closed 4 years ago.
In a code review that I have made today to one of my team mates, I have seen him checking properties for null with the is keyword, like:
if(myVariable is null)
{
//do something
}
I was tempted to disagree of this type of checking, but I don't feel so confident on my knowledge about it. Is it really okay to check for null like this?

C# Regex won't find the matches [duplicate]

This question already has answers here:
How to check if a String contains any letter from a to z? [duplicate]
(6 answers)
Closed 5 years ago.
I am trying to detect whether my string contains the alphabets (a-z & A-Z), and I obtained my answer from this post. But not all string works as expected, take 8+a as an example:
string expression = "8+a";
if (Regex.IsMatch(expression, #"^[a-zA-Z]+$") == true)
true;
else
false;
This returns false which suppose to be true. How do I make this return true. Thanks!
^ Anchors your regex to the start of the string.
$ Anchors to the end of the string.
Remove those and your regex will work.
Also, there is no need to compare a bool value to true because it is done automatically.

string Replace not working in asp.net [duplicate]

This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
string mystring="the are boys";
string[] tags = {"the"};
string[] replace ={"they"}
mystring.Replace(tags[0],replace[0]) // is not working
mystring.Replace("the","they") // is working
I thought both are same but first statement is not working. The second one is.
Please help me to solve the problem.
I assume that you don't assign the return value of String.Replace to the variable. But since strings are immutable you have to do that:
mystring = mystring.Replace(tags[0],replace[0])

Categories