How to check if an object is nullable?

There are two types of nullable – Nullable<T> and reference-type. Jon has corrected me that it is hard to get type if boxed, but you can with generics: – so how about below. This is actually testing type T, but using the obj parameter purely for generic type inference (to make it easy to call) … Read more

Correct way to check if a type is Nullable [duplicate]

Absolutely – use Nullable.GetUnderlyingType: if (Nullable.GetUnderlyingType(propertyType) != null) { // It’s nullable } Note that this uses the non-generic static class System.Nullable rather than the generic struct Nullable<T>. Also note that that will check whether it represents a specific (closed) nullable value type… it won’t work if you use it on a generic type, e.g. … Read more

nullable object must have a value

You should change the line this.MyDateTime = myNewDT.MyDateTime.Value; to just this.MyDateTime = myNewDT.MyDateTime; The exception you were receiving was thrown in the .Value property of the Nullable DateTime, as it is required to return a DateTime (since that’s what the contract for .Value states), but it can’t do so because there’s no DateTime to return, … Read more

Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]

The compiler first tries to evaluate the right-hand expression: GetBoolValue() ? 10 : null The 10 is an int literal (not int?) and null is, well, null. There’s no implicit conversion between those two hence the error message. If you change the right-hand expression to one of the following then it compiles because there is … Read more

How can I format a nullable DateTime with ToString()?

Console.WriteLine(dt2 != null ? dt2.Value.ToString(“yyyy-MM-dd hh:mm:ss”) : “n/a”); EDIT: As stated in other comments, check that there is a non-null value. Update: as recommended in the comments, extension method: public static string ToString(this DateTime? dt, string format) => dt == null ? “n/a” : ((DateTime)dt).ToString(format); And starting in C# 6, you can use the null-conditional … Read more

Laravel Migration Change to Make a Column Nullable

Laravel 5 now supports changing a column; here’s an example from the offical documentation: Schema::table(‘users’, function($table) { $table->string(‘name’, 50)->nullable()->change(); }); Source: http://laravel.com/docs/5.0/schema#changing-columns Laravel 4 does not support modifying columns, so you’ll need use another technique such as writing a raw SQL command. For example: // getting Laravel App Instance $app = app(); // getting laravel … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)