Using nullable types in C#

Using nullable types in C#.

.

Use case – database-hosted persistent store, and a ‘date’ value that can be null

We must be careful when working with dates.

If you need to work with a ‘date’ value that can be null, and the value is in a database-hosted persistent store (e.g. using Entity Framework), then follow these techniques.

Declaration, as a property:

public DateTime? ReleaseDate { get; set; }

.

Reference info

MSDN

Nullable<T> Structure

Notable properties: HasValue, Value

Notable methods: GetValueOrDefault()

(The default value is DateTime.MinValue.)

.

C# Programming Guide

Nullable Types

Using Nullable Types

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: