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
Jaimin ShethiyaPosted Aug 19, 2023, 3:05 PM
You can use the below statement
if (!string.IsNullOrWhiteSpace(Course))
{
//write here your code
}
Rijwan AnsariPosted Aug 10, 2023, 1:11 PM
Simply use string.IsNullOrEmpty(Course) to validate null or empty.
Cr BhargaviPosted Aug 10, 2023, 11:37 AM
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".Vishal YelvePosted Aug 10, 2023, 10:49 AM
try this
Jignesh KumarPosted Aug 9, 2023, 7:57 AM
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 you want to show string as empty then you can assign,
Amit MohantyPosted Aug 9, 2023, 5:51 AM
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:
Naimish MakwanaPosted Aug 9, 2023, 5:25 AM
Try below,
Thanks
Tahir AnsariPosted Aug 8, 2023, 6:34 PM
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.Mark TaborPosted Aug 8, 2023, 5:14 PM
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
Tahir AnsariPosted Aug 8, 2023, 12:02 PM
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.