Check if integer == null [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to check if my Listentry = null but I don't know how.
Note:
ID is a Integer
Here is my Code
if(MeineGaeste[i].ID !=null)
{
i++;
}
else
{
MeinGast.ID = i++;
test = false;
}

As stated, an int cannot be null. If a value is not set to it, then the default value is zero. You can do a check for 0 if that is what you think it is being set to...
Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.
String.IsNullOrEmpty(ID.ToString());
Which returns a boolean so you can stick it into an if statement.

An int cannot be null. On the other hand a nullable int, int?, can be. That being said the check that you want to do is meaningless.
Changing the type of ID from int to int?, the check you do has a meaning and the compiler wouldn't complain about it.

you can use this ID.HasValue it return true when there is a value and false if null

As a normal int type is a value type, it can not be null.
You can use the nullable type int? for the property ID which is basically a struct, that allows null or any int value. See MSDN or this question or this question for additional documentation.
If it is not possible to change the type of the ID property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.

Related

One of the equality operators isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
(beginner here, I'm learning c#) I've just learned about equality operators and was testing one out. For some reason unbeknownst to me (it's probably some really simple mistake I'm overlooking) I'm getting an error. Here's the code:
string number = "number";
number == "number";
I'm getting an error for the line, number == "number". To my knowledge, when I run it, "true" should be printed. Thanks for helping out a beginner, I'll probably be kicking myself once I know the answer.
In the second line you use the equality operator ==. You correctly understand that the equality operator == returns true if its operands are equal, false otherwise. Thus, it returns a value of type bool. But to output the result of this operation to the console, you should use the method Console.WriteLine. So you should first save this value in a variable and then output the value of this variable to the console. This can be done like this:
string number = "number";
bool equalityComparisonResult = number == "number";
Console.WriteLine(equalityComparisonResult);
Or you can do without an intermediate variable and print the result of the equality directly to the console:
string number = "number";
Console.WriteLine(number == "number");
string number = "number";
number == "number"? Console.WriteLine("numbers are equal"): Console.WriteLine("numbers are different");
or you can use Equals Method:
string number = "number";
number.Equals("number")

what is the use of ?? in c# [duplicate]

This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
Closed 5 years ago.
I know ? checks for null when placed before a . member access and ?: for conditional statements. Although, I think ?? checks for null as well but I'm not very sure
I can't find useful information about ?? on https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
PS. Actually I didn't look well at the MSDN reference very well. I've just seen its definition now.
I though of closing this post before but I won't for the sake of anyone who wouldn't think of referring to ?? as double question marksin their question
that operator is sugarSyntax for operations with nullable operands
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
int? counter = null;
int backup = counter ?? 0;
in this case backup will be assigned with counter value IF counter is different to null, for something ELSE then backup will be assigned with 0
note that I bold the keywords IF-ELSE which make us infer that ?? operand can be replaced by simple old-school if else conditionals.

What is the cheapest way in .net to determine enumeration-vs-number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to determine whether a given string is an enumeration value or not. It is rather common that the string should be a decimal value. My question is whether it takes longer to do an Enum.TryParse() on the string or to do a regex check that it is a decimal value or do a Int32.TryParse(). If the string is not a numeric (unsigned) decimal value, it is highly likely, but not 100% guaranteed, that the string is an enum value.
This check is made many many times. I want to get the lowest average cost.
You should profile your code to determine if this is actually a problem. If not, just do whatever gives you the most clear, concise (working) code. If it is a problem, this is probably a good way to pick which approach to use:
bool probablyANumber = char.IsDigit(myStr[0]);
This should work well because enums can't start with digits, while numbers usually do (with exceptions like "-1" and " 1 "). Whether this is an improvement or just makes things worse depends on your data set.
It's worth noting that enums can be parsed from numbers, so the behavior of Enum.TryParse may not be what you were expecting.
enum MyEnum
{
Zero,
One,
Two,
Ten = 10
}
MyEnum e;
Enum.TryParse<MyEnum>("10", out e);
// success; e == MyEnum.Ten
MyEnum e;
Enum.TryParse<MyEnum>("11", out e);
// success; e == (MyEnum)11
The fastest? Put the string values in a HashSet. HashSet.Contains()

How to avoid this kind of exception? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an sqlite database, and a table, in which i have a column PIN as a TEXT, and it's empty.
when i'm getting it from the database, and trying to convert into a string it brings me an exception, When casting a number the value must be less then infinity...what is the reason?
In databases like sqlite, empty strings and null are two different things. It looks like you may be trying to cast your PIN to an int, or some other integer type. If the PIN field is null or DBNull, this cast will fail, giving the exception you listed. To further complicate things, null != DBNull.Value, which is what your database query is likely returning.
To check for this, you need to check the PIN field against DBNull.Value and cast only after you've checked.
See: DBNull Class
See: A similar SO question

when to use nullable short hand and what is the peformance impact, if any? [duplicate]

This question already has answers here:
When should one use nullable types in c#?
(10 answers)
Closed 8 years ago.
the null-able short hand property
public long? id{get;set}
what is the incentive of using it, other than if you are reading it from a database, is there a chance the value might be null...?
also, if you want to get the id,
int a = id.value;
what is the performance impact on this? and in which situation will you be compelled to use this shorthand. Please share your thoughts on this
The advantage is the direct check if is null or no
long v = id ?? v2; //if id is null v will get the v2 value
And the performance is pratically the same of a normal one

Categories

Resources