nullable

Should I use nullable types for Id's on my Entity Objects?

My current setup: I have an entity object with some properties, among them an int Id. To communicate with the database, I've created a repository with, among others, the following method: public int Add(TEntity entity) { ObjectSet.AddObject(entity); return entity.Id; } (It's a generic repository, that requires that TEntity is ...

Confusion about Nullable<T> constraints

Greetings everybody. I am sorry, if this was already asked before (searched in vain) or is really very simple, but i just can't get it. The MSDN definition of a Nullable type, states, that it is defined in a following manner: [SerializableAttribute] public struct Nullable<T> where T : struct, new() So the question is quite straightfor...

Is it possible to override Value on a nullable struct, to return a diffrent type?

This might sound crazy to you, but I need a Nullable<T> (where T is a struct) to return a different type for it's Value property. Rules being if Nullable<T>'s Property HasValue is true, Value will always return an object of a different specified type (then itself). I might be over thinking this, but this unit test bellow kind of shows ...

When should one use nullable types in c#?

I have been repeatedly asked these question in many interviews.... But still can't able to explain them with a simple example... What is nullable types in c#?When should one use nullable types in c#?Any simple ex? Any suggestion.... ...

Best way to check for nullable bool in a condition expression (if ...)

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 condition better/more cleanly? bool? nullableBool = true; if (nullableBool ?? false) { ... } else { ... } especially the if (nullableBool ?? false) p...

C# - Basic question: What is '?' ?

Hi, I'm wondering what ? means in C# ? I'm seeing things like: DateTime? or int?. I suppose this is specific to C# 4.0? I can't look for it in Google because I don't know the name of this thing. The problem is I'm using DateTime and I have a lot of cast errors (from DateTime to DateTime?). Thank you ...

Checking if Type instance is a nullable enum in C#

How do i check if a Type is a nullable enum in C# something like Type t = GetMyType(); bool isEnum = t.IsEnum; //Type member bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method? ...

Nullable integer in .NET

what is the nullable integer and where can it be used? ...

Which is preferred: new Nullable<int> or (int?)null ?

Which way is preferred in expressions like this: int? Id { get { int i; return Int32.TryParse(Request["id"], out i) ? i : (int?)null; } } is it better to cast on null or create a new Nullable<T> ? ...

LINQ 2 Entities , howto check DateTime.HasValue within the linq query

Hi all ... I have this method that is supposed to get the latest messages posted, from the Table (& EntitySet) called ENTRY ///method gets "days" as parameter, used in new TimeSpan(days,0,0,0);!! using (Entities db = new Entities()) { var entries = from ent in db.ENTRY where ent.DATECREATE.Value > Date...

Nullable<T> as a parameter

I alredy have this: public static object GetDBValue(object ObjectEvaluated) { if (ObjectEvaluated == null) return DBNull.Value; else return ObjectEvaluated; } used like: List<SqlParameter> Params = new List<SqlParameter>(); Params.Add(new SqlParameter("@EntityType", GetDBValue(EntityType))); Now i wanted to keep the sam...

Is there some way to assume @Nullable as default? (using FindBugs or any other free tool).

Consider such code public void m1(String text) { if(text == null) text = "<empty>"; System.out.println(text.toLowerCase()); } And this is a buggy version: public void m1(String text) { System.out.println(text.toLowerCase()); } If null value passed, the NullPointerException may be thrown. I would like the static...

FluentNHibernate: Not.Nullable() doesn't affect output schema

Hello I'm using fluent nhibernate v. 1.0.0.595. There is a class: public class Weight { public virtual int Id { get; set; } public virtual double Value { get; set; } } I want to map it on the following table: create table [Weight] ( WeightId INT IDENTITY NOT NULL, Weight DOUBLE not null, primary key (WeightId) ) ...

Nullables? Detecting them

Ok, im still a bit new to using nullable types. I'm writing a reflecting object walker for a project of mine, im getting to the point where im setting the value of a reflected property with the value i've retrieved from a reflected property. The value i've retrieved is still in object form, and it dawned on me, since i want my object w...

Type result with Ternary operator in C#

I am trying to use the ternary operator, but I am getting hung up on the type it thinks the result should be. Below is an example that I have contrived to show the issue I am having: class Program { public static void OutputDateTime(DateTime? datetime) { Console.WriteLine(datetime); } public static bool IsDateT...

What C# data types can be nullable types?

Can someone give me a list, or point me to where I can find a list of C# data types that can be a nullable type? For example: I know that `Nullable<int>` is ok I know that `Nullable<byte[]>` is not. I'd like to know which types are nullable and which are not. BTW, I know I can test for this at runtime. However, this is for a code ge...

nullable type and a ReSharper warning

I have the following code: private static LogLevel? _logLevel = null; public static LogLevel LogLevel { get { if (!_logLevel.HasValue) { _logLevel = readLogLevelFromFile(); } return _logLevel.Value; } } private static LogLevel readLogLevelFromFile() { ... } I get a ReSharper w...

Having virtual rows in a table in hibernate.

I'm sure this is a question to which most of the answers will be "Why are you doing that?", but I thought I would ask. I've got a table of (Users), the vast majority of which just have an ID (as they're lazily created users for sessions that have never provided any information), and don't use the rest of the columns in the database. I'v...

C# property ending with ?

What does that mean ? public bool? Verbose { get; set; } When applied to string? , there is an error Error 11 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' ...

Where in memory are nullable types stored?

This is maybe a follow up to question about nullable types. Where exactly are nullable value types (int?...) stored in memory? First I thought it's clear enough, as Nullable<T> is struct and those are value types. Then I found Jon Skeet's article "Memory in .NET", which says: Note that a value type variable can never have a value ...