nullable

Coding practices for C# Nullable type [closed]

江枫思渺然 提交于 2019-12-02 19:09:07
I have never used nullable types in my C# code. Now I have decided to change my coding practice by introducing nullable types in my code. What are the major changes in the coding practices should be made while making a transition from normal datatypes to nullable data-types in case of application programming? What are the areas that should be taken care of? What are the points I should always watch out for? Nullable<T> is useful for when you need a possible invalid state for a value type, or if the data is being retrieved from a database that may contain a null value for the column. It is very

How to compare nullable types?

谁都会走 提交于 2019-12-02 18:58:38
I have a few places where I need to compare 2 (nullable) values, to see if they're the same. I think there should be something in the framework to support this, but can't find anything, so instead have the following: public static bool IsDifferentTo(this bool? x, bool? y) { return (x.HasValue != y.HasValue) ? true : x.HasValue && x.Value != y.Value; } Then, within code I have if (x.IsDifferentTo(y)) ... I then have similar methods for nullable ints, nullable doubles etc. Is there not an easier way to see if two nullable types are the same? Update: Turns out that the reason this method existed

DataSet does not support System.Nullable<> in Export

左心房为你撑大大i 提交于 2019-12-02 16:59:34
I was trying to generate a Report using Export to Excell, PDF, TextFile. Well I am doing this in MVC. I have a class which I named SPBatch (which is the exact name of my Stored Procedure in my SQL) and it contains the following: public string BatchNo { get; set; } public string ProviderName { get; set; } public Nullable<System.Int32> NoOfClaims { get; set; } public Nullable<System.Int32> TotalNoOfClaims { get; set; } public Nullable<System.Decimal> TotalBilled { get; set; } public Nullable<System.Decimal> TotalInputtedBill { get; set; } public Nullable<System.DateTime> DateCreated { get; set;

Implicit conversion to System.Double with a nullable struct via compiler generated locals: why is this failing?

余生长醉 提交于 2019-12-02 16:39:59
Given the following, why does the InvalidCastException get thrown? I can't see why it should be outside of a bug (this is in x86; x64 crashes with a 0xC0000005 in clrjit.dll). class Program { static void Main(string[] args) { MyDouble? my = new MyDouble(1.0); Boolean compare = my == 0.0; } struct MyDouble { Double? _value; public MyDouble(Double value) { _value = value; } public static implicit operator Double(MyDouble value) { if (value._value.HasValue) { return value._value.Value; } throw new InvalidCastException("MyDouble value cannot convert to System.Double: no value present."); } } }

Get the 'Nullable' state programmatically of a Property in Entity Framework

与世无争的帅哥 提交于 2019-12-02 14:26:38
问题 I need to get the Nullable property out for a field in EF. Some magic code needs to be done on properties that are Nullable=True, and I cannot find a working solution to get the property out. foreach (PropertyInfo property in (PropertyInfo[])type.GetProperties()) { var getPropertyType = property.GetMethod.ReturnTypeCustomAttributes.ToString(); var getValue = property.GetValue(model); bool isNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() ==

Defining an XML element that must be empty or decimal

跟風遠走 提交于 2019-12-02 12:09:40
问题 I have the following XSD which was generated by VS from an example XML file: <xs:element name="amperage_rating"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:decimal"> <xs:attribute name="unit" type="xs:string" use="required" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> which demands a numeric value. But sometimes the value is not known and this must also be allowed: <amperage_rating unit="A"></amperage_rating> I've tried nillable="true" but it had no

Get the 'Nullable' state programmatically of a Property in Entity Framework

守給你的承諾、 提交于 2019-12-02 08:15:10
I need to get the Nullable property out for a field in EF. Some magic code needs to be done on properties that are Nullable=True, and I cannot find a working solution to get the property out. foreach (PropertyInfo property in (PropertyInfo[])type.GetProperties()) { var getPropertyType = property.GetMethod.ReturnTypeCustomAttributes.ToString(); var getValue = property.GetValue(model); bool isNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>); // isNullable always returns false !!!! and I need it to return true if the field is

Nullable create via reflection with HasValue = false, given a parameter of type `Type` only

白昼怎懂夜的黑 提交于 2019-12-02 08:11:00
问题 Given a type parameter which is a Nullable<> , how can I create an instance of that type which has HasValue = false ? In other words, complete this code: //type is guaranteed to implement Nullable<> public static object Create(Type type) { //Instantiate a Nullable<T> with reflection whose HasValue = false, and return it } My attempt, which doesn't work (it throws a NullReferenceException ) as there's no default constructor: static void Main(string[] args) { Console.WriteLine(Create(typeof

Nullable create via reflection with HasValue = false, given a parameter of type `Type` only

时光总嘲笑我的痴心妄想 提交于 2019-12-02 05:02:55
Given a type parameter which is a Nullable<> , how can I create an instance of that type which has HasValue = false ? In other words, complete this code: //type is guaranteed to implement Nullable<> public static object Create(Type type) { //Instantiate a Nullable<T> with reflection whose HasValue = false, and return it } My attempt, which doesn't work (it throws a NullReferenceException ) as there's no default constructor: static void Main(string[] args) { Console.WriteLine(Create(typeof(Nullable<int>))); Console.ReadLine(); } //type is guaranteed to be implement from Nullable<> public static

How to convert type int[] to int?[]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 02:54:44
I'm using a linq query to output an int array. But I need to pass this into a method that only accepts int?[]. So after searching on ways to convert int[] to int?[] I found something that seemed might work here The following code is a simplified example that shows what is working and not working. using System; using System.Collections.Generic; using System.Web; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { // working... int[] vids1 = new[] { "", "1", "2", "3" } .Where(x => !String.IsNullOrWhiteSpace(x)) .Select(x => Convert.ToInt32(x)) .ToArray();