In SQL server we can use “isnull” function for example if Table1 Contains Field1 and only one record which Field1 is null we can write this Query:
select isnull(Field1,0) from Table1
which returns “0”.
can we use any function like this in C#? for Example Consider textBox1 is Empty. and I want to Show “0”.
MessageBox.show( FunctionName(textBox1.text , 0).toString());
Advertisement
Answer
You can create an extension method:
internal static class MyStringExtensions { public static string GetValueOrDefault(this string extendee, string defaultValue) { if(string.IsNullOrEmpty(extendee)) { return defaultValue;} return extendee; } }
Sample Use:
MessageBox.show( textBox1.text.GetValueOrDefault("0"));