nullable Questions
7
Solved
In my application I have a textbox - txtDiscount where the admin can set a discount percentage for a certain user, or he may not. On the back end I want to save the data so for now I have this:
do...
9
Solved
I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g....
Nuno asked 30/8, 2010 at 10:6
2
Solved
By definition Nullable<> is a struct but when I call some generic function, it behaves like it's an object.
class GenController
{
public void Get<T>(T id) where T : struct
{
Console....
4
Solved
I have code like this:
IEnumerable<string?> items = new [] { "test", null, "this" };
var nonNullItems = items.Where(item => item != null); //inferred as IEnumerable<string?>
var len...
Jester asked 14/10, 2019 at 8:27
2
Solved
In php-8 and older versions the following code works
class Foo {
public function __construct(string $string = null) {}
}
But in php-8, along with property promotion, it throws an error
class Foo ...
Canso asked 22/10, 2020 at 4:51
13
Solved
I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools.
Is the following good or bad coding style? Is there a way to express the cond...
Gypsy asked 20/4, 2010 at 9:26
4
Solved
Our MySQL web analytics database contains a summary table which is updated throughout the day as new activity is imported. We use ON DUPLICATE KEY UPDATE in order that the summarization overwrites ...
Rameau asked 19/8, 2009 at 6:28
5
As the title says, are nullable booleans (bool?) to be considered atomic in their read/write operations? I made a search through the C# documentation to no avail. I know for a fact that only certai...
Ettaettari asked 4/11, 2015 at 17:35
3
Solved
With numeric it is always same pretty:
if(a < 123) { ... } // disregards if `b` is `int?` or `int`
But with bool?:
bool? b = ...
if(b) { ... } // compiler error: can't convert bool? to bool....
8
So I have this class:
public class Foo<T> where T : ???
{
private T item;
public bool IsNull()
{
return item == null;
}
}
Now I am looking for a type constraint that allows me to us...
3
Solved
When compiling with -Wnullable-to-nonnull-conversion, we get a proper warning with the following code:
NSString * _Nullable maybeFoo = @"foo";
^(NSString * _Nonnull bar) {
}(maybeFoo);
Tests.m:3...
Whitesmith asked 24/2, 2017 at 20:18
8
Solved
I have a few places where I need to compare 2 (nullable) values, to see if they're the same.
I think there should be something in the framework to support this, but can't find anything, so instead...
Suchta asked 26/2, 2010 at 12:53
6
Solved
EmployeeNumber =
string.IsNullOrEmpty(employeeNumberTextBox.Text)
? null
: Convert.ToInt32(employeeNumberTextBox.Text),
I often find myself wanting to do things like this (EmployeeNumber is a Nu...
Dibranchiate asked 16/9, 2008 at 19:1
2
Solved
Is there a way to make the analyzer understand that the variable Bar has a value for the following case?
#nullable enable
class Foo {
bool GenerateArray => Bar.HasValue;
int? Bar { get; set; ...
1
Solved
In this contrived C# 8 example:
#nullable enable
class Fred<T>
{
T Value; // If T is a nullable type, Value can be null.
public Fred() { }
public void SetValue(T value) { Value = value; }...
Borg asked 16/6, 2020 at 15:39
4
Solved
Why does this not compile?
int? number = true ? 5 : null;
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>
4
Solved
I have a class that uses the same type guard in multiple functions; something like this:
function validData(d: Data | null): d is Data {
return d !== null;
}
class C {
data: Data | null;
publ...
Pelota asked 6/4, 2018 at 5:30
3
Solved
I want to write a convenience extension to extract values from a Map while parsing them at the same time. If the parsing fails, the function should return a default value. This all works fine, but ...
Nikethamide asked 13/4, 2018 at 12:34
2
Solved
In the same way we can have
nullableClassInstance?.method(blah)
Is there a way to do
nullableFunctionInstance?(blah)
In other words, is there an operator that checks whether a function insta...
2
Solved
In Java, I have recently began to adopt the Optional type more in my code. This allows for better null-value handling and to some extend also safer code. Optional has the ifPresentOrElse meth...
Metamerism asked 24/3, 2020 at 21:4
4
Solved
I just need to be able to cast an object to nullable enum. Object can be enum, null, or int. Thanks!
public enum MyEnum { A, B }
void Put(object value)
{
System.Nullable<Myenum> val = (Syst...
6
Solved
I've seen this question but have a few more questions about the usage of the assert keyword. I was debating with a few other coders about using assert. For this use case, there was a method t...
9
Solved
I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this:
DateTime? d;
bool success = DateTime.TryParse("some date text", out (DateTime...
4
Solved
I would like to convert a Nullable(Of Byte)() array (a.k.a. byte?[]) to a non-nullable array of the same type, that is, from byte?[] to byte[].
I'm looking for the simpler, easier, faster generic ...
2
I often use extension methods to check method arguments
void MyMethod(string? test)
{
Require.NotNull(test, nameof(test));
// Same as
// if (test == null) throw new ArgumentNullExeception...
© 2022 - 2024 — McMap. All rights reserved.