Linked Questions

20 votes
1 answer
3k views

Why does Nullable<T> not match as a reference type for generic constraints [duplicate]

Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass<T> where T:...
Earlz's user avatar
  • 64.3k
1 vote
2 answers
288 views

Is it possible to create class that uses nullable int as generic parameter? [duplicate]

I am trying to create class called Stats that uses generic parameter T. I intend to use T as int? or float?. However, when I try to create object of class Stats I get that: Error 1 The type 'int?' ...
caked bake's user avatar
0 votes
1 answer
305 views

Is it possible to restrict a generic parameter to just accept nullable types? [duplicate]

Possible Duplicate: Nullable type as a generic parameter possible? Is creating a C# generic method that accepts (nullable) value type and reference type possible? In c# I would like to restrict ...
Jesper Kihlberg's user avatar
-1 votes
1 answer
49 views

C#: Strongly type heterogenous container accept nullable [duplicate]

I am trying to adapt the following code, to also acceptable nullable type. Consider this: public sealed class JsonDictionary { private readonly IDictionary<string, object> _data; public ...
malat's user avatar
  • 12.7k
0 votes
0 answers
24 views

Problem with generic class with a nullable attribute [duplicate]

I have this C# code: using System; namespace NullableExample { public class PAttribute<T> { public T? Value { get; set; } public bool IsPertinent { get; set; } ...
Herman Meijer's user avatar
383 votes
30 answers
528k views

SQL Data Reader - handling Null column values

I'm using a SQLdatareader to build POCOs from a database. The code works except when it encounters a null value in the database. For example, if the FirstName column in the database contains a null ...
DenaliHardtail's user avatar
156 votes
10 answers
80k views

C# generic type constraint for everything nullable

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 ...
jkammerer's user avatar
  • 1,677
12 votes
2 answers
3k views

Why T? is not a nullable type?

Why the code below doesn't work? public T? Method<T>() { /* error CS0403: Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider ...
XWX's user avatar
  • 2,027
1 vote
5 answers
2k views

Why is my code so slow?

I wrote the following extension method to get an element from a dictionary, or null if the key isn't present: public static TValue ItemOrNull<TKey, TValue>(this IDictionary<TKey, TValue> ...
Rag's user avatar
  • 6,653
4 votes
3 answers
8k views

How to make Generic parameter T Nullable in C#?

I'm creating a generic method which should be able to return null. As an example I've created this method GetSecondSmallest(List<T>) below. This function loops through all of the IComparable ...
Duncan Lukkenaer's user avatar
1 vote
3 answers
1k views

Is there a way to define an generic interface with a constraint that the type can be null (but cannot be a value type)?

I'd like to define an generic interface that allows nullable value types (int?, double?, etc.) and class types (which can also be null). I do not want to allow a simple value type. Is there a way to ...
bsh152s's user avatar
  • 3,238
1 vote
1 answer
2k views

Nullable generic type that can be an int or string

I'm trying to create an interface that will add a column to a database entity which will be used the track the user making changes. public interface IAuditEntity<TKey> { TKey? UpdatedBy { ...
Sun's user avatar
  • 4,808
3 votes
5 answers
737 views

Pros and Cons of using "as", "is", "DBNull", ToString() in C#

When reading data from ExecuteReader. It returns me data in IDataReader. When I want to fill this data into my entities I will be assigning Non Nullable values in most of the cases to either Nullable ...
Vaibhav Jain's user avatar
  • 34.6k
8 votes
2 answers
2k views

Tell the caller that a `async Task<T>` method may return null

My situation is similar to the one described Proper nullable annotation for async generic method that may return default(T). However, I'd like to know the proper way to let the caller know that the &...
drs9222's user avatar
  • 4,518
0 votes
3 answers
517 views

How to write generic method for casting DBNull value to Nullable?

I have a database with a nullable column foo_date, where Npgsql maps the sql NULL value to an instance of the C# class DBNull. In my C# aggregate I use the type DateTime? for said column. So the ...
Joerg's user avatar
  • 964

15 30 50 per page