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

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.

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?

C# What does ? in Arguments Allow [duplicate]

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

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.

Conditional operator in C# and return types [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Why does null need an explicit type cast here?
Nullable types and the ternary operator. Why won't this work?
Attempting to do the following:
sqlCmd.Parameters.Add("#DateCreated", System.Data.SqlDbType.DateTime).Value
= myObject.DateCreated == DateTime.MinValue
? DBNull.Value : myObject.DateCreated;
I am getting this error:
Type of conditional expression cannot
be determined because there is no
implicit conversion between
'System.DBNull' and 'System.DateTime'
I obviously understand the error but why does type even matter given that Parameters.Value is of type object? Is there a way to accomplish what I am trying to do?
It doesn't make a difference that the return value is going into something that is an object, because the type of the return value has to be determined first.
Cast one of the two values (DBNull.Value, myObject.DateCreated) to a base of the other and you 'll be fine. In this case, the base can even be object.

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