C# What does ? in Arguments Allow [duplicate] - c#

This question already has answers here:
? (nullable) operator in C# [duplicate]
(5 answers)
Closed 7 years ago.
I have some code: partial void FuncName(TimeSpan? value) What does having the nullable operator there allow me to do? If it wasn't there could I not pass a null TimeSpan at all? Do I need it for every argument I pass that could be null?
Thanks

The ? is for nullable, it will allow you to pass null in the TimeSpan.
Nullable Types
Nullable types are instances of the System.Nullable struct. A
nullable type can represent the correct range of values for its
underlying value type, plus an additional null value. For example, a
Nullable, pronounced "Nullable of Int32," can be assigned any
value from -2147483648 to 2147483647, or it can be assigned the null
value.

The sign ? with DataType indicates that Datatype is nullable.
You can read more about Nullable types here

Related

How to use declaration pattern of "is" operator with ternary operator? [duplicate]

This question already has answers here:
Why does pattern matching on a nullable result in syntax errors?
(3 answers)
Closed 5 months ago.
int? a = null;
var b = (a is (int?) y) ? "bla bla bla" : y.ToString();
The name y does not exist in the current context.
No, this is not supported.
If you look at the MS documentation here:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types#how-to-identify-a-nullable-value-type
Then down a little it states
Also, don't use the is operator to determine whether an instance is of
a nullable value type. As the following example shows, you cannot
distinguish types of a nullable value type instance and its underlying
type instance with the is operator:
it says you should
Instead use the Nullable.GetUnderlyingType from the first example and
typeof operator to check if an instance is of a nullable value type.
See also the answer here, which provides a nice method IsNullable
How to check if an object is nullable?

Is there a better way to compare nullable dates? [duplicate]

This question already has answers here:
Comparing nullable DateTime?
(5 answers)
Closed 9 years ago.
i am trying to determine if there is a difference between 2 nullable dates . . is there a more elegant ways instead of this:
if (newDate.HasValue && (!oldDate.HasValue || (oldDate.HasValue && oldDate.Value.Date != mewDate.Value.Date)))
The C# compiler automatically lifts logical operators like == and != over nullalbe types, so you can usually compare them directly instead of checking HasValue. In this case, you can compare with != once you have checked that newDate is not null.
if(newDate.HasValue && newDate != oldDate)
It is described in the specification:
7.3.7 Lifted operators
Lifted operators permit predefined and user-defined operators that
operate on non-nullable value types to also be used with nullable
forms of those types.
For the equality operators
== != a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool.The
lifted form is constructed by adding a single ? modifier to each
operand type. The lifted operator considers two null values equal, and
a null value unequal to any non-null value. If both operands are
non-null, the lifted operator unwraps the operands and applies the
underlying operator to produce the bool result.

value Type data structure not accepting Null Why [duplicate]

This question already has answers here:
Why value types can't be null
(2 answers)
Closed 9 years ago.
I am new to C#.Net
There is two types of data type one is reference type and other is value type.
today I experience that if a create a instance of any value type and assign it to null that compiler gives error.means
int a = null.
I am talking about nullable variables means
int? a = null;
MY Question is that why error occcures when we assign null to any value type data structure.
Thanks in advance.
Because all types have an underlying range for there allowed values. For int null is not an allowable value by default. Thus, when you attempt to assign null to your int object you get your 'non-nullable type' error.
Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value.
See MSDN for more information...
I hope this helps.

What does a questionmark (?) mean in a function declaration in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “DateTime?” mean in C#?
What does the ? mean after a type?
I had a function declaration including a questionmark after the datatype like:
private TimeSpan? sometime()
{
}
What does this mean?
TimeSpan? is shorthand for System.Nullable<TimeSpan>.
A TimeSpan is a value type, which cannot take a null value. By wrapping it in a System.Nullable<> it can be null. Without that ? it would be illegal to return null from the function.
Nullable Structure
Represents an object whose underlying type is a value type that can
also be assigned null like a reference type.
Instead of writing Nullable<TimeSpan>, you can write TimeSpan?.
Nullable, that a value type can be null.
It means that the value type is a nullable type
Basically its a nullable TimeStamp.

How to determine programmatically in c# that a type accepts null value or not? [duplicate]

This question already has answers here:
Closed 14 years ago.
I know merely checking for whether the type is not a value type is not sufficient. How can i account for those Nullable?
DUPLICATE
How to check if an object is nullable?
You can use Nullable.GetUnderlyingType that will return null if the type is not nullable.
have you tried the keyword default(YourType) ?
Whether T will be a reference type or a value type.
If T is a value type, whether it will be a numeric value or a struct.
This also works:
bool nullable = yourType.IsGenericType && yourType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))

Categories