Questions tagged [non-nullable]

232 questions
17
votes
5 answers

Why am I allowed to compare a non-nullable type with null?

Possible Duplicate: C# okay with comparing value types to null If I try to assign null to a non-nullable type in C#: System.DateTime time = null; I'll get a compile-time error: error CS0037: Cannot convert null to 'System.DateTime' because it…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
15
votes
6 answers

Alternatives to nullable types in C#

I am writing algorithms that work on series of numeric data, where sometimes, a value in the series needs to be null. However, because this application is performance critical, I have avoided the use of nullable types. I have perf tested the…
Ryan
  • 1,621
  • 2
  • 13
  • 14
14
votes
2 answers

Non-nullable reference types' default values VS non-nullable value types' default values

This isn't my first question about nullable reference types as it's been few months I'm experiencing with it. But the more I'm experiencing it, the more I'm confused and the less I see the value added by that feature. Take this code for…
Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
14
votes
1 answer

Cannot infer type for type parameter T, when the type is explicitly specified in a struct definition

I have a struct definition which includes, among other things, this field: pub struct Separated<'a, I, T> { ..., // other fields, separated: NonNull>, } Shortly after, in its constructor, I attempt to initialize…
coriolinus
  • 879
  • 2
  • 8
  • 18
14
votes
6 answers

When does using C# structs (value types) sacrifice performance?

I have been playing with structs as a mechanism to implicitly validate complex value objects, as well as generic structs around more complex classes to ensure valid values. I am a little ignorant as to the performance consequences, so I am hoping…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
13
votes
2 answers

How to Annotate an Array NonNull?

I'm using org.eclipse.jdt.annotation.NonNull to add extra information for static null analysis. I donn't know how to annotate arrays correctly: How can I say that an array reference is non-null? How can I say that an array consists of non-null…
Vertex
  • 2,682
  • 3
  • 29
  • 43
12
votes
1 answer

Lombok's @NonNull or javax @Nonnull

Lombok's @NonNull VS javax.annotation.Nonnull Which one is better to use for method parameters and when? The answer here vaguely compares all annotations but has no inference on which one is best. I just to know the better of the two.
adimoh
  • 658
  • 2
  • 8
  • 20
12
votes
5 answers

Writing unit test for @Nonnull annotated parameter

I have a method like this one: public void foo(@Nonnull String value) {...} I would like to write a unit test to make sure foo() throws an NPE when value is null but I can't since the compiler refuses to compile the unit test when static null…
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
11
votes
8 answers

Non-nullable reference types

I'm designing a language, and I'm wondering if it's reasonable to make reference types non-nullable by default, and use "?" for nullable value and reference types. Are there any problems with this? What would you do about this: class Foo { Bar?…
Zifre
  • 26,504
  • 11
  • 85
  • 105
10
votes
4 answers

Non-nullable fields initialized inside a method called from the constructor

I have some non-nullable fields which I want to initialize inside a helper methods, called from a constructor, to reduce the clutter inside the constructor: private FlowLayoutPanel _flowPanel; private ComboBox _printersComboBox; //... public…
noseratio
  • 59,932
  • 34
  • 208
  • 486
10
votes
1 answer

typescript: question mark vs type union with undefined

When specifying a member with a question mark at the end of it's name, the type signature automatically gets extended to include undefined. It's okay to create an instance without this member: interface Option{ val? : number; // hover over val…
lhk
  • 27,458
  • 30
  • 122
  • 201
10
votes
1 answer

Kotlin - nonnull getter for a nullable field

I'm new with Kotlin and I try to rework a small Java project to this new language. I use mongodb in my project and I have a class, for example: class PlayerEntity { constructor() {} //for mongodb to create an instance constructor(id: ObjectId,…
awfun
  • 2,316
  • 4
  • 31
  • 52
10
votes
2 answers

Why is integer == null a valid boolean expression in C#?

Why is integer == null a valid boolean expression in C#, if integer (variable of type int) is not nullable? (I'm not against it, in fact I like it, but I didn't know it was possible)
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
9
votes
6 answers

Type Inference failed in a call to 'join' on nullable and non-nullable int

In my Linq, I am trying to make an inner join to a nullable field. Employee and Department have a relation, Department may have an EmployeeID or may have a null. So what would be my join, if i want only the records that satisifed the inner join (no…
manav inder
  • 3,531
  • 16
  • 45
  • 62
9
votes
6 answers

Value of unassigned non-nullable variable (C#)

Just curious. If you go: string myString; Its value is null. But if you go: int myInt; What is the value of this variable in C#? Thanks David
David
  • 15,750
  • 22
  • 90
  • 150
1
2
3
15 16