nullable

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

只愿长相守 提交于 2019-12-03 03:10:13
问题 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

Linq OrderBy breaks with navigation property being null

*爱你&永不变心* 提交于 2019-12-03 02:34:33
Working with four tables. Users -> has basic user info including a userid and a departmentid (int) Groups -> basic group info including a groupid GroupsMembers -> table that has the relationship between a group and it's members, many to many relationship, so groupid and userid are the columns Departments -> basic department info including deptid I have a fk from the departmentid in the users table to the deparmtnet id in the departments table. FK from groups groupid to groupsmembers groupid FK from users userid to groupsmembers userid This allows the groups in the edmx to have a users

Argument order for '==' with Nullable<T>

别说谁变了你拦得住时间么 提交于 2019-12-03 01:37:16
The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is: 36 bytes longer (IL code); calls additional functions including newobj and initobj ; declares four

Why doesn't incrementing Nullable<int> throw an exception?

你说的曾经没有我的故事 提交于 2019-12-03 00:52:23
Could you please explain, why does Console.WriteLine write empty line ( Console.WriteLine(null) give me compilation error) and why there isn't NullReferenceException (even a+=1 shouldn't raise it)? int? a = null; a++; // Why there is not NullReferenceException? Console.WriteLine(a); // Empty line You're observing the effects of a lifted operator . From section 7.3.7 of the C# 5 specification: Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined

MVC 3 doesn't bind nullable long

邮差的信 提交于 2019-12-02 22:19:47
I made a test website to debug an issue I'm having, and it appears that either I'm passing in the JSON data wrong or MVC just can't bind nullable longs. I'm using the latest MVC 3 release, of course. public class GetDataModel { public string TestString { get; set; } public long? TestLong { get; set; } public int? TestInt { get; set; } } [HttpPost] public ActionResult GetData(GetDataModel model) { // Do stuff } I'm posting a JSON string with the correct JSON content type: { "TestString":"test", "TestLong":12345, "TestInt":123 } The long isn't bound, it's always null. It works if I put the value

Can a WCF service contract have a nullable input parameter?

落花浮王杯 提交于 2019-12-02 22:14:15
I have a contract defined like this: [OperationContract] [WebGet(UriTemplate = "/GetX?myStr={myStr}&myX={myX}", BodyStyle = WebMessageBodyStyle.Wrapped)] string GetX(string myStr, int? myX); I get an exception: [InvalidOperationException: Operation 'GetX' in contract 'IMyGet' has a query variable named 'myX' of type 'System.Nullable 1[System.Int32]', but type 'System.Nullable 1[System.Int32]' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.] could not find anything about this error except the

How to exclude null properties when using XmlSerializer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 21:53:27
I'm serializing a class like this public MyClass { public int? a { get; set; } public int? b { get; set; } public int? c { get; set; } } All of the types are nullable because I want minimal data stored when serializing an object of this type. However, when it is serialized with only "a" populated, I get the following xml <MyClass ...> <a>3</a> <b xsi:nil="true" /> <c xsi:nil="true" /> </MyClass> How do I set this up to only get xml for the non null properties? The desired output would be <MyClass ...> <a>3</a> </MyClass> I want to exclude these null values because there will be several

What is the justification for this Nullable<T> behavior with implicit conversion operators

╄→尐↘猪︶ㄣ 提交于 2019-12-02 21:52:22
I encountered some interesting behavior in the interaction between Nullable and implicit conversions. I found that providing an implicit conversion for a reference type from a value type it permits the Nullable type to be passed to a function requiring the reference type when I instead expect a compilation error. The below code demonstrates this: static void Main(string[] args) { PrintCatAge(new Cat(13)); PrintCatAge(12); int? cat = null; PrintCatAge(cat); } private static void PrintCatAge(Cat cat) { if (cat == null) System.Console.WriteLine("What cat?"); else System.Console.WriteLine("The cat

XmlSerializer and nullable attributes

◇◆丶佛笑我妖孽 提交于 2019-12-02 20:15:36
I have a class with numerous Nullable<T> properties which I want to be serializable to XML as attributes. This is apparently a no-no as they are considered 'complex types'. So, instead I implement the *Specified pattern, where I create an addition *Value and *Specified property as follows: [XmlIgnore] public int? Age { get { return this.age; } set { this.age = value; } } [XmlAttribute("Age")] public int AgeValue { get { return this.age.Value; } set { this.age = value; } } [XmlIgnore] public bool AgeValueSpecified { get { return this.age.HasValue; } } Which works fine - if the 'Age' property

Java check if boolean is null

送分小仙女□ 提交于 2019-12-02 19:54:56
How do you check if a boolean is null or not? So if I know "hideInNav" is null. How do I stop it from further executing? Something like the below doesn't seem to work but why? boolean hideInNav = parent.getProperties().get("hideInNav", false); String hideNavigation = hideInNav != null ? hideInNav : ""; boolean can only be true or false because it's a primitive datatype (+ a boolean variables default value is false ). You can use the class Boolean instead if you want to use null values. Boolean is a reference type, that's the reason you can assign null to a Boolean "variable". Example: Boolean