nullable

Why is a Nullable<T> not a valid Custom Attribute Parameter when T is?

喜你入骨 提交于 2019-11-30 08:02:22
If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks } and a custom attribute like this public class HungerAttribute : Attribute { public Hungry HungerLevel { get; set; } public Hungry? NullableHungerLevel { get; set; } } I can do this [Hunger(HungerLevel = Hungry.CouldEatMySocks)] public class Thing1 but I can't do this [Hunger(NullableHungerLevel = Hungry.CouldEatMySocks)] public class Thing2 It generates an error that says "'NullableHungerLevel' is not a valid named attribute argument because it is not a valid attribute parameter type". Why is that not allowed? I

Get PropertyType.Name in reflection from Nullable type

假如想象 提交于 2019-11-30 07:05:14
I want use reflection for get properties type. this is my code var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair<Type, string> (propertyInfo.PropertyType.Name, propertyInfo.Name) ); } this code propertyInfo.PropertyType.Name is ok but if my property type is Nullable i get this Nullable'1 string and if write FullName if get this stirng System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Igoy Change your code to look for nullable type, in that case take

Get short date for System Nullable datetime (datetime ?) in C#

被刻印的时光 ゝ 提交于 2019-11-30 06:51:00
问题 How to get short date for Get short date for System Nullable datetime (datetime ?) for ed 12/31/2013 12:00:00 --> only should return 12/31/2013 . I don't see the ToShortDateString available. 回答1: You need to use .Value first (Since it's nullable). var shortString = yourDate.Value.ToShortDateString(); But also check that yourDate has a value: if (yourDate.HasValue) { var shortString = yourDate.Value.ToShortDateString(); } 回答2: string.Format("{0:d}", dt); works: DateTime? dt = (DateTime?

Query to check whether a column is nullable

风格不统一 提交于 2019-11-30 05:34:26
Query to check whether a column is nullable (null values are allowed in the column or not). It should preferably return yes/no or 1/0 or true/false. You could also use the COLUMNPROPERTY and OBJECT_ID metadata functions: SELECT COLUMNPROPERTY(OBJECT_ID('SchemaName.TableName', 'U'), 'ColumnName', 'AllowsNull'); Andomar You could retrieve that from sys.columns : select is_nullable from sys.columns where object_id = object_id('Schema.TheTable') and name = 'TheColumn' You can also check all columns in a table for 'nullable' property or any other property that you want, for example table named Bank

Is there any difference between myNullableLong.HasValue and myNullableLong != null?

人盡茶涼 提交于 2019-11-30 04:40:56
When I have a nullable long, for example, is there any difference between myNullableLong.HasValue and myNullableLong != null ... or is it just 'syntactic sugar'? It's just syntactic sugar. They will behave exactly the same way - the nullity test actually gets compiled into a call to HasValue anyway. Sample: public class Test { static void Main() { int? x = 0; bool y = x.HasValue; bool z = x != null; } } IL: .method private hidebysig static void Main() cil managed { .entrypoint // Code size 25 (0x19) .maxstack 2 .locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0) IL_0000: ldloca.s V

Why shouldn't I always use nullable types in C#

拟墨画扇 提交于 2019-11-30 04:14:41
I've been searching for some good guidance on this since the concept was introduced in .net 2.0. Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use non-nullable types when that explicitly makes sense.) Is there a 'significant' performance hit to choosing a nullable data type over its non-nullable peer? I much prefer to check my values against null instead of Guid.empty, string.empty, DateTime.MinValue,<= 0, etc, and to work with nullable types in general. And the only reason I don't choose nullable

Compare nullable datetime objects

我们两清 提交于 2019-11-30 01:26:56
问题 I have two nullable datetime objects, I want to compare both. What is the best way to do it? I have already tried: DateTime.Compare(birthDate, hireDate); This is giving an error, maybe it is expecting dates of type System.DateTime and I have Nullable datetimes. I have also tried: birthDate > hiredate... But the results are not as expected...any suggestions? 回答1: To compare two Nullable<T> objects use Nullable.Compare<T> like: bool result = Nullable.Compare(birthDate, hireDate) > 0; You can

@OneToOne(optional=false) and @JoinColumn(nullable=false) used together

*爱你&永不变心* 提交于 2019-11-30 00:25:26
I've bumped into this example in JPA 2.0 FR Specification, 11.1.37. OneToOne Annotation, page 403: @OneToOne(optional=false) @JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false) public CustomerRecord getCustomerRecord() { return customerRecord; } Is there any reason that I should put @OneToOne(optional=false) and at that same time put @JoinColumn(... nullable=false) ? Aren't these two declarations the same? Isn't one of them redundant? Are both of them used in DDL schema generation? Formally optional=false is a runtime instruction to the JPA implementation, and nullable

C# 4: Dynamic and Nullable<>

▼魔方 西西 提交于 2019-11-29 23:46:01
So I've got some code that passes around this anonymous object between methods: var promo = new { Text = promo.Value, StartDate = (startDate == null) ? new Nullable<DateTime>() : new Nullable<DateTime>(DateTime.Parse(startDate.Value)), EndDate = (endDate == null) ? new Nullable<DateTime>() : new Nullable<DateTime>(DateTime.Parse(endDate.Value)) }; Methods that receive this anonymous object type declare its type as dynamic : private static bool IsPromoActive(dynamic promo) { return /* check StartDate, EndDate */ } At run-time, however, if StartDate or EndDate are set to new Nullable<DateTime>

Checking if Type instance is a nullable enum in C#

萝らか妹 提交于 2019-11-29 22:45:16
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? LukeH public static bool IsNullableEnum(this Type t) { Type u = Nullable.GetUnderlyingType(t); return (u != null) && u.IsEnum; } Jon Skeet EDIT: I'm going to leave this answer up as it will work, and it demonstrates a few calls that readers may not otherwise know about. However, Luke's answer is definitely nicer - go upvote it :) You can do: public static bool IsNullableEnum(this Type t) {