I have a parameter in method with name as Course its a string type and default value is null now i am not assigning it any value and it have defult value of null I wrote if condition
if(Course!=null || Course!="" || Course != "null")
{
}
my parameters is null but the if conditio always get true even if my Course is null why it is so

Jignesh KumarRank #91 · 22.9K reputationPosted Aug 9, 2023, 3:57 AM EDT
Hello,
You can use below to check string is null or empty, string just we need to use inbuilt method which will check string is null or empty.
if (!string.IsNullOrEmpty(Course)) { // Your code here }if you want to show string as empty then you can assign,
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2023, 8:02 AM EDT
In your condition, you're using the logical OR (
||) operator, which means that if any one of the conditions is true, the whole expression will evaluate to true. Since you're checking for different conditions using OR operators, at least one of them will always be true, which is why yourifcondition is always true.if (Course != null && Course != "") { // Your code here }Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 9, 2023, 1:25 AM EDT
Try below,
if (!string.IsNullOrEmpty(Course) && !Course.Equals("null")) { // Your code here }Thanks
Rijwan AnsariRank #82 · 24.2K reputationPosted Aug 10, 2023, 9:11 AM EDT
Simply use string.IsNullOrEmpty(Course) to validate null or empty.
Mark TaborRank #914 · 2.3K reputationPosted Aug 8, 2023, 1:14 PM EDT
I have used only one like if(Course !=null) still same result
Also I have tried if(Course!="") still same
Also I have tried if(Course!="null") still same my actual value of course is null when I hover the mouse on that variable its value is null
Vishal YelveRank #178 · 13.6K reputationPosted Aug 10, 2023, 6:49 AM EDT
try this
if (!string.IsNullOrEmpty(Course)) { }Jaimin ShethiyaRank #177 · 13.7K reputationPosted Aug 19, 2023, 11:05 AM EDT
You can use the below statement
if (!string.IsNullOrWhiteSpace(Course))
{
//write here your code
}
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2023, 2:34 PM EDT
Based on what you've described, if you are directly checking
if (Course != null)and still encountering unexpected behavior where the condition evaluates to true whenCourseis null, there might be some other issue in your code or environment.Amit MohantyRank #112 · 20K reputationPosted Aug 9, 2023, 1:51 AM EDT
Because you're using the OR (||) operator, if any of these conditions is true, the entire expression will be true. So, even if Course is null, the first condition will be true and the entire condition will be true.
To fix this, you should use the AND (&&) operator instead of the OR (||) operator:
if (Course != null && !Course.equals("") && !Course.equals("null")) { // Your code here }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 10, 2023, 7:37 AM EDT
if (Course != null && Course != "" && Course != "null") { // Your code here }In this corrected condition, the
ifstatement will only be true if all three conditions are true:Course != null- This part checks ifCourseis not null.Course != ""- This part checks ifCourseis not an empty string.Course != "null"- This part checks ifCourseis not the string "null".