nullable

C# 4: Dynamic and Nullable<>

假装没事ソ 提交于 2019-11-30 11:18:34
问题 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

Why is there a questionmark on the private variable definition?

蓝咒 提交于 2019-11-30 11:15:14
I am reading an article about the MVVP Pattern and how to implement it with WPF. In the source code there are multiple lines where I cannot figure out what the question marks in it stand for. private DateTime? _value; What does the ? mean in the definition? I tried to find it in the help from VS but failed. It's a nullable value. Structs, by default, cannot be nullable, they must have a value, so in C# 2.0, the Nullable<T> type was introduced to the .NET Framework. C# implements the Nullable<T> type with a piece of syntactic sugar, which places a question mark after the type name, thus making

SqlParameter with Nullable value give error while ExecuteNonQuery?

♀尐吖头ヾ 提交于 2019-11-30 10:59:48
I have an sql query that has a parameter that can be null in the database (Sql Server). The update method work fine until that user put a blank in the field, this produce a null value for the DataTime object (this object is nullable). The problem is when the dbCommand.ExecuteNonQuery(); . Here is how I build the parameter for this field: IDataParameter dbParam_au_id = new SqlParameter(); dbParam_au_id.ParameterName = "@birthday"; dbParam_au_id.Value = birthday; dbParam_au_id.DbType = DbType.DateTime; dbCommand.Parameters.Add(dbParam_au_id); I have try to convert the null value of birthday to

Detecting a Nullable Type via reflection

落爺英雄遲暮 提交于 2019-11-30 10:58:59
Surprisingly the following code fails the Assert: int? wtf = 0; Assert.IsType<Nullable<int>>(wtf); So just out curiosity, how can you determine if a given instance is a Nullable<> object or not? Well firstly, Nullable<T> is a struct, so there isn't an object as such. You can't call GetType() , as that will box the value (at which point you either get null and thus an exception, or a boxed non-nullable value and therefore not the type you want). (Boxing is what's messing up your assertion here - I would assume that IsType accepts object .) You can use type inference though to get the type of

Why are generic and non-generic structs treated differently when building expression that lifts operator == to nullable?

只谈情不闲聊 提交于 2019-11-30 10:55:00
This looks like a bug in lifting to null of operands on generic structs. Consider the following dummy struct, that overrides operator== : struct MyStruct { private readonly int _value; public MyStruct(int val) { this._value = val; } public override bool Equals(object obj) { return false; } public override int GetHashCode() { return base.GetHashCode(); } public static bool operator ==(MyStruct a, MyStruct b) { return false; } public static bool operator !=(MyStruct a, MyStruct b) { return false; } } Now consider the following expressions: Expression<Func<MyStruct, MyStruct, bool>> exprA =

How to change a PG column to NULLABLE TRUE?

最后都变了- 提交于 2019-11-30 10:26:51
问题 How can I accomplish this using Postgres? I've tried the code below but it doesn't work: ALTER TABLE mytable ALTER COLUMN mycolumn BIGINT NULL; 回答1: From the fine manual: ALTER TABLE mytable ALTER COLUMN mycolumn DROP NOT NULL; There's no need to specify the type when you're just changing the nullability. 来源: https://stackoverflow.com/questions/4812933/how-to-change-a-pg-column-to-nullable-true

How to use nonnull and nullable Objective-C keywords in block-based API method

放肆的年华 提交于 2019-11-30 10:14:29
问题 Consider the following method - (void)methodWithArg:(NSString *)arg1 andArg:(NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler; With the new nonnull and nullable annotation keywords we can enrich it as follows: - (void)methodWithArg:(nonnull NSString *)arg1 andArg:(nullable NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler; but we also get this warning: Pointer is missing a nullability type specifier (

svcutil.exe - Proxy generated not allowing for nullable fields

妖精的绣舞 提交于 2019-11-30 09:39:33
I am trying to consume a web service specified using WSDL by creating a WCF proxy using svcutil.exe, but the WSDL specifies that some of the operations have parameters that are optional (minOccurs="0"), for example: <xs:element minOccurs="0" maxOccurs="1" name="meetingId" type="xs:int" /> Unfortunately, the generated proxy does not allow me to not specify the values (the parameters are not nullable), and there are no "specified" fields as part of the call to instruct the proxy that no value should be sent. Is there any way to use svcutil to generate a proxy that would allow me to do this? (On

Parse to Nullable Enum

谁说我不能喝 提交于 2019-11-30 09:13:20
I am trying to parse a string back to a nullable property of type MyEnum. public MyEnum? MyEnumProperty { get; set; } I am getting an error on line: Enum result = Enum.Parse(t, "One") as Enum; // Type provided must be an Enum. Parameter name: enumType I have a sample console test below. The code works if I remove nullable on the property MyEntity.MyEnumProperty . How can I get the code to work without knowing the typeOf enum except via reflection? static void Main(string[] args) { MyEntity e = new MyEntity(); Type type = e.GetType(); PropertyInfo myEnumPropertyInfo = type.GetProperty(

The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

拈花ヽ惹草 提交于 2019-11-30 08:06:23
问题 Why am I getting this error in the following code? void Main() { int? a = 1; int? b = AddOne(1); a.Dump(); } static Nullable<int> AddOne(Nullable<int> nullable) { return ApplyFunction<int, int>(nullable, (int x) => x + 1); } static Nullable<T> ApplyFunction<T, TResult>(Nullable<T> nullable, Func<T, TResult> function) { if (nullable.HasValue) { T unwrapped = nullable.Value; TResult result = function(unwrapped); return new Nullable<TResult>(result); } else { return new Nullable<T>(); } } 回答1: