E
Stop reading good practices! This only creates programming and illusion addictions that are learning to program better. Study the fundamentals, understand why things work that way, research and see on their own or ask for specialists who can be challenged and evaluated by others, as they are doing now (in the past it was more reliable, today the site evaluates answers with problems as if they were good, so it is not as reliable).You did the tests? The right way? Are you sure you saw slower? In the right situation? Be very careful with tests that have no control of the environment. I see a lot of error when the person will test and there gives different results of reality. And even if it does, in normal use may be that the result is different from the actual and correct test, because the code does not perform alone. I did the test of Rodolfo's response and my machine, under the conditions of my solution, gave different result, even in different executions the result was not very consistent.It does not happen, but it could have a compiler that analyzes the whole context (and it is not so difficult in certain cases) and could see that most of it is not necessary and eliminate everything.Okay, intuitively I even believe I'm right, but only a correct test can guarantee.The first does something different from the others, so we are already comparing oranges with bananas. It checks if the string is null before checking the content. If semantics are different already complicates comparison. Here I have to ask: you can guarantee that the string Isn't it null?I already gave one https://pt.stackoverflow.com/q/172694/101 and this method actually does only two things: check if it is null and if its size is 0. Then we can already conclude that it itself . NET prefers to check size, and makes sense, because it avoids a comparison with https://pt.stackoverflow.com/q/181032/101 of memory and numerically compares a constant. There we can verify that IsNullOrWhiteSpace() is potentially much less performative and wasted resources if not what you need, and semantics are different in certain situations.If you can guarantee it is not null then the third option is better. I can affirm this without testing for the knowledge I have, but it could have some optimization and not be different. Nothing prevents the compiler or https://pt.stackoverflow.com/q/146250/101 identify what you want and exchange for a more performing code. And this can change from version to version, so if you want a certain information, you need to test the version you will use, on the platform you will use. Anyway, everything can influence.If you want to guarantee the best performance, don't count that there will be optimization. But rarely is this really necessary.And of course, I would avoid the second whenever possible because it tends not to be optimized. I would rule out the first if I guarantee it is not null, which I usually guarantee already and even more in C# 8.If someone finds a reason to use another way they need to justify.As a useful note, in C# 8 it is possible to ensure that the string and other types by reference never be null in compilation time, so any comparison with null will be unnecessary unless the type is declared anulable (string?).NoteThis part doesn't make any more sense because the question has been edited, but it can be useful to other people.Finally in this specific case posted in the question I would do so (it is not viewing error):
If I declare a type variable string and do not put any value it will be null for sure, there is nothing else to do, nor need to check if it is null, even more if there is something. Of course, examples 2 and 3 will make a mistake. So it's not that it's good practice, it just makes sense to do anything. I replied considering that the example was an error and the intention is in another context.