Exclamation point usage in C# (not equality operator) [duplicate] - c#

This question already has answers here:
What does null! statement mean?
(5 answers)
What does exclamation mark mean before invoking a method in C# 8.0? [duplicate]
(3 answers)
Closed last month.
I am going through a Udemy tutorial on .NET WebAPI/Entity Framework and the instructor has used exclamation mark syntax I have never seen before. He doesn't explain it, and I am struggling to find an explanation online.
Can anyone fill me in on what these exclamation marks are accomplishing here? I've only used them as equality operators (e.g., "!=" for "not equal to"), but this seems to be something else entirely:
example 1 (before ".User" and after "NameIdentifier)":
private int GetCurrentUserId() => int.Parse(_httpContextAccessor.HttpContext!.User
.FindFirstValue(ClaimTypes.NameIdentifier)!);
Example two (after "SubmittedBy"):
.Where(i => i.SubmittedBy!.Id == GetCurrentUserId())
I tried omitting the exclamation marks and my requests worked just the same (tested w/ Swagger). I googled my question a few different ways, but couldn't find a phrasing that returned useful results. I hunted down Microsoft's "C# operators and expressions" documentation, but didn't see an explanation of it there.

Related

What is the use of an empty statement [duplicate]

This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.

Why do Boolean.TryParse()] and Convert.ToBoolean() evaluate a string differently? [duplicate]

This question already has answers here:
What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)?
(3 answers)
Closed 6 years ago.
Why do Boolean.TryParse() and Convert.ToBoolean() evaluate a string differently?
I understand how they end up evaluating differently:
Boolean.TryParse() will match (case insensitive) 'true' and 'false'.
Convert.ToBoolean() will match to a whole range of values (example demonstrated in Microsoft doco linked above) which I would consider more natural.
Its the reasoning behind the difference I dont understand.
There are a couple of discussions touching on this subject which don't seem to address this particular question.
It's in the method/class names.
Convert -> you already have some value, you convert it to another type. e.g. you have value 1 which can be converted to true.
Parse -> you have the value as a string and you parse it.

Difference between ContentPropertyAttribute and ContentProperty [duplicate]

This question already has answers here:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.

Does it make a difference to check null on either side of conditional? [duplicate]

This question already has answers here:
Difference between null == x and x == null? [duplicate]
(5 answers)
Closed 9 years ago.
I have seen both
if(something == null)
and
if(null == something)
Does it make a difference in which order this null check happens? I do not see a difference in functionality but would love to know if there is reasoning behind it.
This is legal in C# and is colloquially known as a Yoda Condition. Many people in the C/C++ world like this because it guards at compile time against replacing == with = by accident. However, it has fallen out of favor in C# due to the fact the compiler will flag it (the single =) as an error in that instance (so long as it's not a boolean eval).
Some programmers prefer to put the constant on the left side of an equality operator to avoid accidents (a typo of = instead of ==). In the second example, having the = typo would introduce a compiler error, which is easy to fix, whereas in the first example such a typo may introduce a bug that is very difficult to find.
This practice comes directly out of C and C++ programming style. I don't know whether it would affect C#. If it's no longer relevant, then it's more likely to be a habit rather than a strategy.

How can I use ReSharper to list unused methods in a solution? [duplicate]

This question already has answers here:
Find unused code [closed]
(9 answers)
Closed 9 years ago.
In a legacy ASP.NET project I have inherited, there are an abundance of methods defined which are used absolutely nowhere.
I'm familiar with the "Find usages" functionality, but would like to be able to generate a list of methods which are not called anywhere in the app. Does such functionality exist?
You can select ReSharper => Inspect => Codes Issues in solution ;
And there, you can group by "Issue Type" and you should see all issues that match with "Type or type member is never used" (unused method goes there)
(And if you click right on it, you can select : "Show only "type or type member is never used" issues" .. and there you go ;)

Categories