nullable

Nullable type issue with ?: Conditional Operator

廉价感情. 提交于 2019-12-17 01:41:12
问题 Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable<DateTime> foo; foo = true ? null : new DateTime(0); The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'." Not that I can't use the former, but the second style is more consistent with the rest of my code. 回答1: This

c# .net 4.0 : nullable input string parameter and the lambda expressions

▼魔方 西西 提交于 2019-12-14 03:11:53
问题 I have a following method: public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) { return db.Profiles.Where(p => p.CountryFrom.CountryName.Equals(CountryFrom,StringComparison.OrdinalIgnoreCase)); } Right now, it just filters by CountryFrom but I need to filter it by CountryLoc as well. So how do I modify the Where filter? Also, CountryFrom could be null or CountryLoc could be null. So how do I modify the method's signature to all the nullable input string parameter

with ef 4.1 code first how can i create a nullable column

馋奶兔 提交于 2019-12-13 07:42:52
问题 I have the following POCO: Public Class T1 <Required()> <MaxLength(128)> <Key(), Column(Order:=0)> Property K1 As String <Required()> <MaxLength(128)> <Key(), Column(Order:=1)> Property K2 As String <Required()> Property C1 As String Property C2 As String end Class I would expect C2 to be created as Nullable, but both C1 and C2 are Not Null. Adding <Required(AllowEmptyStrings:=True)> Does not make a difference, as it seems the decoration is aimed at data validation, not DB creation. So how do

Generic method to instantiate a variable of any type (including nullable struct)?

99封情书 提交于 2019-12-13 07:36:50
问题 Let's say I have this function which instantiates a new variable with default/empty value for any type public static T GetDefault<T>() where T : new() //body of this method is the question { T t = new T(); return t; } The problem with the above function is when I have something like int? for T . Because if I have int? t = new int?(); t will be null! I do not want this to happen, instead I want the default value of int to be returned which is 0 . To solve this, I can have different overloads

Why Android Studio has this @Nullable thing?

有些话、适合烂在心里 提交于 2019-12-13 05:59:13
问题 I am just kind of curious about this weird thing that Android Studio is showing me. I was about to use the addView method when this tool-tip-like thingy pops up: I am quite confused because of the @Nullable annotations on the parameters. View can be null because it is a reference type. But why an explicit @Nullable annotation? When I go into the definition of the addView method, I see no annotations! Then I am more confused. So I want to ask why Android Studio has this weird thingy. 回答1: View

Nullable generic field in generic class

僤鯓⒐⒋嵵緔 提交于 2019-12-12 17:13:15
问题 I'm trying to do something like this: public class MySuperCoolClass<T> { public T? myMaybeNullField {get; set;} } Is this possible? This gives me the error: error CS0453: The type T' must be a non-nullable value type in order to use it as type parameter T' in the generic type or method System.Nullable'. Thanks 回答1: Add where T : struct generic constraint to get rid of the error since Nullable<T> accepts only struct . public class MySuperCoolClass<T> where T : struct { public T?

Casting (int?)null vs. new int?() - Which is better?

落爺英雄遲暮 提交于 2019-12-12 11:00:47
问题 I use Nullable<T> types quite a lot when loading values into classes and structs when I need them to be nullable, such as loading a nullable value from a database (as the example below). Consider this snippet of code: public class InfoObject { public int? UserID { get; set; } } // Load the ID into an SqlInt32 SqlInt32 userID = reader.GetSqlInt32(reader.GetOrdinal("intUserID")); When I need to load a value into a nullable property, sometimes I do this: infoObject.UserID = userID.IsNull ? (int?

Convert String to Nullable DateTime [duplicate]

北战南征 提交于 2019-12-12 09:28:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I use DateTime.TryParse with a Nullable<DateTime>? I have this line of code DateTime? dt = Condition == true ? (DateTime?)Convert.ToDateTime(stringDate) : null; Is this the correct way to convert string to Nullable DateTime, or is there a direct method to convert without converting it to DateTime and again casting it to Nullable DateTime? 回答1: You can try this:- DateTime? dt = string.IsNullOrEmpty(date) ?

Best way to represent Nullable member in C++? [duplicate]

放肆的年华 提交于 2019-12-12 07:39:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Nullable values in C++ What is the best way to represent nullable member in C++? In C#, we can use Nullable<T> type. Such a data type is very much needed as not everything can have meaningful value. It is so important data type that @Jon Skeet has spent one entire chapter, spanned over 27 pages, describing only Nullable<T> in his outstanding book C# in Depth. One simple example can be a Person class 1 , defined

string was not recognized as a valid datetime while assigning the value to Nullable DateTime

时光毁灭记忆、已成空白 提交于 2019-12-12 02:57:20
问题 I have a textbox from which I am sending date as string in 'MM/dd/yyyy' formate, and when I am assigning that value to nullable datetime property value getting the error as string was not recognized as a valid datetime, I am converting the string as below then also getting the same error private Tbl_UserDetails GetAnnouncementInformation(Tbl_UserDetails userDetails, Dictionary<string, object> details) { userDetails.JoiningDate = string.IsNullOrEmpty(details["JoiningDate "].ToString()) ?