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

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.

Related

What is #property_name declaration means in C#? [duplicate]

This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 1 year ago.
I was browsing sources for microsoft .netcore runtime and came across these lines of code
as you can see they are using # symbol infront of every error message getter like #Error_InvalidFilePath.
My question is, what is this language feature that is being used here?
And, Where can I read more about it?
Thanks
The # is a way to use reserved words as names. E.g. the variable class could be used as variable name like #class.
For non reserved names this won't add anything. But of course you don't know which names are reserved in the future. Your code example is generated code, which should preferably work for newer language versions and so the # makes sense there.
See docs

Evaluate the string as math expression [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance

Count the characters changed in a string [duplicate]

This question already has answers here:
How to calculate distance similarity measure of given 2 strings?
(7 answers)
How to compare two rich text box contents and highlight the characters that are changed?
(3 answers)
Closed 5 years ago.
What I need to do seems simple enough but I can't seem to find a good way to do it. My app reads text off of a document but sometimes gets it wrong. Users are allowed to change the text in a verification step. What I need to know is how many characters changed, including case.
For example,
Original value: i23 MAin St
Value changed to: 123 Main Street
The number of characters changed in this instance would be 6. I then need to capture this value in a variable for later use. Is there a good way to do this in c#?

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.

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.

Categories