nullable

What is the size of a Nullable<Int32>?

穿精又带淫゛_ 提交于 2019-12-05 01:02:42
So, a couple of questions, actually: An int ( Int32 ) is specified to be (obviously) 32 bits. What about an int? ( Nullable<int> )? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that. I would have answered my own question using sizeof(int?) ; but as int? is a managed type this is not allowed. I understand that the size of a type may be platform-dependent, and that in the case of objects which contain references to other objects, a sizeof -like operation would be misleading. However, is there a

How to create structure with null value support?

被刻印的时光 ゝ 提交于 2019-12-05 00:34:21
I'm new in C#. In c# I can't set value of a structure to null how can I create a structure with null value support? MrWednesday Structs and value types can be made nullable by using the Generic Nullable<> class to wrap it. For instance: Nullable<int> num1 = null; C# provides a language feature for this by adding a question mark after the type: int? num1 = null; Same should work for any value type including structs. MSDN Explanation: Nullable Types (c#) You can use Nullable<T> which has an alias in C#. Keep in mind that the struct itself is not really null (The compiler treats the null

Is there a better way to write this line of C# code in C#3.0?

与世无争的帅哥 提交于 2019-12-05 00:25:48
问题 I have a property declared as follows: public decimal? MyProperty { get; set; } I am needing to pass this value to another method as a string and so the only way I see to do so is as follows: MyProperty == null ? null : MyProperty.ToString() This looks very messy when you have a number of similar properties being passed into a method. Does anyone know if there is a better and more concise way of writing this? Oh, and if anyone can think of a more appropriate title to this question please feel

What is the use of Nullable<bool> type?

↘锁芯ラ 提交于 2019-12-05 00:20:34
a bool variable could hold true or false, while bool? could be null as well. Why would we need a third value for bool ? If it is not true , what ever it is, it is == false Can you suggest a scenario where I would fancy a bool? instead. Thanks Something can be true, false or undefined for many reasons. How would one answer "Is your third child a girl?" if one only has two children? Both true and false are incorrect. Null would be appropriate as saying that the comparison doesn't apply. Various answers discuss the importance of nullable types in general. There is an additional answer for the

C# determine a Nullable property DateTime type when using reflection

ε祈祈猫儿з 提交于 2019-12-04 22:56:00
I have a question on how to determine an object's Nullable property type. ObjectA has a property DateTime? CreateDate; When I iterate through its properties like the following code, how do I check if a property is a Nullable DateTime type? foreach (PropertyInfo pi in ObjectA.GetType().GetProperties()) { //do the compare here } pi.PropertyType == typeof(DateTime?) pi.PropertyType == typeof(Nullable<DateTime>); Try: property.PropertyType.Equals(typeof(DateTime?)) 来源: https://stackoverflow.com/questions/1180730/c-sharp-determine-a-nullable-property-datetime-type-when-using-reflection

How to use Conditional Operation with Nullable Int

我只是一个虾纸丫 提交于 2019-12-04 20:10:25
问题 A small problem. Any idea guys why this does not work? int? nullableIntVal = (this.Policy == null) ? null : 1; I am trying to return null if the left hand expression is True, else 1 . Seems simple but gives compilation error. Type of conditional expression cannot be determined because there is no implicit conversion between null and int . If I replace the null in ? null : 1 with any valid int , then there is no problem. 回答1: Yes - the compiler can't find an appropriate type for the

Get field of optional object or return null

浪尽此生 提交于 2019-12-04 19:16:20
问题 I have optional object: Optional<Detail> newestDetail; I would like to return newestDetail.getId() or if newestDetail is null return null . Do we have more sophisticated approach of doing this, than following? return newestDetail.isPresent()?newestDetail.get().getId():null; 回答1: Map the value to an Optional with the id field and turn that one into a null value if it is empty: return newestDetail.map(Detail::getId).orElse(null); 来源: https://stackoverflow.com/questions/47714105/get-field-of

How to insert NULL in database using Django

自作多情 提交于 2019-12-04 18:37:12
I have a problem inserting NULL in Django. I mean i don't know how to do this. I have function, lets call it find_position , then i have field in model like position (IntegerField, null=True, blank=True). This function inspect some html code, and if it match with some regex, it returns integer, but if it doesn't find - have to return NULL or None or something that I can put in database. def find_position(self, to_find, something): ... if re.search(to_find, something): return i + (page * 10) + 1 return NULL # or what i have to return here? And i have this: _position = find_position(to_find,

Restricting a generic to things that can be null

谁说胖子不能爱 提交于 2019-12-04 15:35:10
问题 I'd like to restrict a generic I'm coding to anything that can be null . That's basically any class + System.Nullable (e.g. int? and such). For the class part, it's rather easy: public class MyGeneric<T> where T : class {} But then, this doesn't allow me to do this: var myGeneric = new MyGeneric<int?>(); or this: var myGeneric = new MyGeneric<Nullable<int>>(); The compiler complains with: error CS0452: The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic

Can structs really not be null in C#?

半城伤御伤魂 提交于 2019-12-04 15:15:11
问题 Below is some code that demonstrates I cannot declare and initialize a struct type as null. The Nullable type is a struct, so why am I able to set it to null? Nullable<bool> b = null; if (b.HasValue) { Console.WriteLine("HasValue == true"); } //Does not compile... Foo f = null; if (f.HasValue) { Console.WriteLine("HasValue == true"); } Where Foo is defined as public struct Foo { private bool _hasValue; private string _value; public Foo(string value) { _hasValue = true; _value = value; }