C
C#2w ago
Faker

Deference and Reference in C#

Hello guys, sorry to disturb you all, can someone explain why I obtain the warning "dereferencing possibly null reference" pls. What do we mean by "deferencing" and "referencing" something?
C#

if (role.Trim().ToLower().Equals("administrator") || role.Trim().ToLower().Equals("manager") || role.Trim().ToLower().Equals("user"))
{
Console.WriteLine($"Your input value ({role}) has been accepted.");
break;
}
else
{
Console.WriteLine($"The role name that you entered, \"{role}\" is not valid. Enter your role name (Administrator, Manager, or User) ");
}
C#

if (role.Trim().ToLower().Equals("administrator") || role.Trim().ToLower().Equals("manager") || role.Trim().ToLower().Equals("user"))
{
Console.WriteLine($"Your input value ({role}) has been accepted.");
break;
}
else
{
Console.WriteLine($"The role name that you entered, \"{role}\" is not valid. Enter your role name (Administrator, Manager, or User) ");
}
No description
39 Replies
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
Dereference is when you set something to null or you forget a typed instance
Faker
FakerOP2w ago
what do you mean by a "typed instance" pls here I didn't set anything to null but the var keyword assume that role can be null though using the string?
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
yeah. cuz Console.ReadLine() can be null
Faker
FakerOP2w ago
yep I see what do you mean by typed instance here pls Also, what's the meaning of "dereference" here? Like we want to retrive the value of the Console.ReadLine ?
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
means that something exists and is of a certain type Dereference is just basically telling you the variable can be null. and you can't reference a null variable. because null is null, it doesn't exist
Pobiega
Pobiega2w ago
Dereferencing is Accessing something via a reference
Faker
FakerOP2w ago
ah, yeah I see
Pobiega
Pobiega2w ago
And doing that to null causes null ref exception
Faker
FakerOP2w ago
yep just read that, when I have such warning, is there a proper way of handling/ checking if the variable isn't null ?
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
put a ? on a nullable variable which will basically convert to a if (variable != null) or something but C# does the magic for you
Pobiega
Pobiega2w ago
Yesnt. Depends on where you use it. maybeNull?.Something(); yep var x = maybeNull?.Value; returns null if maybeNull was null
Faker
FakerOP2w ago
C#
var role = Console.ReadLine();
if (role.Trim().ToLower().Equals("administrator") || role.Trim().ToLower().Equals("manager") || role.Trim().ToLower().Equals("user"))
{
Console.WriteLine($"Your input value ({role}) has been accepted.");
break;
}
C#
var role = Console.ReadLine();
if (role.Trim().ToLower().Equals("administrator") || role.Trim().ToLower().Equals("manager") || role.Trim().ToLower().Equals("user"))
{
Console.WriteLine($"Your input value ({role}) has been accepted.");
break;
}
hmm here for example, I declared role variable, then, what should I do, writing role?
Pobiega
Pobiega2w ago
If you do, you'll get an error Because null isn't a Boolean value
Faker
FakerOP2w ago
yeah, why is that
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
you should store role.Trim().ToLower() into a variable and check against that it'll be a lot simpler to handle
Pobiega
Pobiega2w ago
Yeah agreed Also, ?? false var role = Console.ReadLine()?.Trim().To lower() Then you can do if role == "admin" or whatever Don't use .Equals for no reason Silly java habit 🙂
Faker
FakerOP2w ago
hmm for string, it doesn't matter if we use == ? Like I know their is the reference thing
Pobiega
Pobiega2w ago
C# isn't java It's fine to use ==
Faker
FakerOP2w ago
yeah, I need to understand the syntax, I will just read a bit about the ? operator then comes back oh okay, noted, thanks !! is there a partiuclar scenario where .Equals is more appropriate ?
Pobiega
Pobiega2w ago
If you need to specify a culture
asdf
asdf2w ago
For strings it's fine, it's pretty random if == is eq by value or ref
Faker
FakerOP2w ago
ok ok, thanks guys, will just read a bit about the ?, then came back
asdf
asdf2w ago
For collections use SequenceEqual
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
better suggestion. switches
Faker
FakerOP2w ago
ah true, will try with switch statement, I always used if statements, will try to change this time I modified the code to something like this:
C#
while (true)
{
var role = Console.ReadLine()?.Trim().ToLower() ?? "Unknown";
switch (role)
{
case "administrator":
case "manager":
case "user":
Console.WriteLine($"Your input value ({role}) has been accepted.");
success = true;
break;
default:
Console.WriteLine($"The role name that you entered, \"{role}\" is not valid. Enter your role name (Administrator, Manager, or User) ");
break;
}

if (success)
{
break;
}
}
C#
while (true)
{
var role = Console.ReadLine()?.Trim().ToLower() ?? "Unknown";
switch (role)
{
case "administrator":
case "manager":
case "user":
Console.WriteLine($"Your input value ({role}) has been accepted.");
success = true;
break;
default:
Console.WriteLine($"The role name that you entered, \"{role}\" is not valid. Enter your role name (Administrator, Manager, or User) ");
break;
}

if (success)
{
break;
}
}
I have one question though, we can't "nest" break statement right? Like I wanted one for the switch case and 1 for the while loop in the same case statement Also, is there a way to test our fallback mechanism of "unknown" ? Like if I just press enter, it returns me an empty string but not a null reference
Anton
Anton2w ago
Make local functions that return a bool for this
Faker
FakerOP2w ago
Oh I see, yep, thanks !
Anton
Anton2w ago
no. change Console.ReadLine to ((string?) null) to test
Faker
FakerOP2w ago
var role = Console.ReadLine((string?)null)?.Trim().ToLower() ?? "Unknown"; Like that ?
Anton
Anton2w ago
no literally change the function call for a null it doesn't take a parameter afaik
Faker
FakerOP2w ago
the thing is, I tried to assign null to role, like after the data entry, I wrote role = null; but it didn't work, is there a reason for that ?
Anton
Anton2w ago
role is not nullable because you have a default at the end ?? "Unknown" change var to string? for that to work i front of role so it has a nullable type
Faker
FakerOP2w ago
hmm but var in this case doesn't act as string?
Anton
Anton2w ago
it's string not string? because there's a fallback
Faker
FakerOP2w ago
ahhh I see
Anton
Anton2w ago
remove the fallback (?? "Unknown")
Faker
FakerOP2w ago
yep noted, will try to change that
Anton
Anton2w ago
and it will be string?
Faker
FakerOP2w ago
yep noted, ty !!

Did you find this page helpful?