I played with generics and hoped that I could get some feedback or suggestions on the function I created to help handle reading the null values โโfrom the database. My main problem is if statement. Is there a better way to find out if T is a string ect.? Thanks.
public static T CheckNull<T>(object value) { if ((value != null) && value.Equals(DBNull.Value)) { if (object.ReferenceEquals(typeof(T), typeof(String))) value = string.Empty; else if (object.ReferenceEquals(typeof(T), typeof(Boolean))) value = false; else value = null; } return (T)value; }
Regardless of the type T, you can go back to default(T), which will provide a default value for the type ( , , , etc.): string.Empty0falsenull
T
default(T)
string.Empty
0
false
null
value = default(T);
Edit: default(string)however, returns null.
default(string)
.
null, , 0, false - - T.
. default () null, string.Empty. , String.Empty, .
String.Empty
Source: https://habr.com/ru/post/1717927/More articles:Using the HttpUtility.HtmlDecode Method with the .NET Compact Framework? - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1717923/getting-wildcards-to-work-in-find-and-replace-function-in-vba-macro-for-microsoft-word&usg=ALkJrhijZwmj2jczFtO_mrbd3cteGus9NAC # How to get the coordinates of a specific point on the screen. (not mouselocation) - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1717925/spreading-controls-to-fill-space-as-a-dialog-resizes&usg=ALkJrhju3BjOYBVf_1DaviZuDykmjP84_QIs there a ApplyPropertyChanges method with datacontext? - .netWhich user should use the embedded application? - linuxmysql is calculated in a group by - sqlchange font size in text box automatically - c #How to use a collection to store a delegate? - c #double free problem - cAll Articles