How to return an empty value instead of zero in C #

I have an extension for string arrays that returns an empty string if the array is null, if the length of the array is == 1, it returns the first element, otherwise the element is returned at the index position, without checking the boundaries.

public static Strings IndexOrDefault(this String[] arr, Int32 index) { if (arr == null) return String.Empty; return arr.Length == 1 ? arr[0] : arr[index]; } 

Now I need to extend this functionality to other arrays, so I decided that this extension method can do the job

 public static T IndexOrDefault<T>(this T[] arr, Int32 index) { if (arr == null) return default(T); return arr.Length == 1 ? arr[0] : arr[index]; } 

And everything was fine, except for default (String) not String.Empty, but null. So I am returning nulls now for Strings, which was the first requirement ...

Is there a default replacement there that can return an empty common value instead of a null?

Edit 2 : (the first was the definition)

Now I use this approach, which it works, but it is not as elegant as I wanted (well, the method is also not elegant, but the solution could :))

 public static T IndexOrDefault<T>(this T[] arr, Int32 index) { if (arr == null) return default(T); return arr.Length == 1 ? arr[0] : arr[index]; } public static String IndexOrDefault(this String[] arr, Int32 index) { if (arr == null) return String.Empty; return arr.Length == 1 ? arr[0] : arr[index]; } 

Two different methods: one for String and a common for the rest.

+4
source share
6 answers

For reference types, there is no common default value other than zero, and some option 0 for value types

You can easily provide one though

 public static T IndexOrDefault<T>(this T[] arr, Int32 index, T theDefaultValue) { if (arr == null) return theDefaultValue; return arr.Length == 1 ? arr[0] : arr[index]; } public static T IndexOrDefault<T>(this T[] arr, Int32 index, Func<T> defaultFactory) { if (arr == null) return defaultFactory(); return arr.Length == 1 ? arr[0] : arr[index]; } 
+3
source

or the first element if the array does not have an element at the index position

Not really. The method returns the first element if the length of the array is one, but it does not check the index at all. What you described would rather be something like this:

 public static Strings IndexOrDefault(this String[] arr, Int32 index) { if (arr == null || arr.Length == 0) return String.Empty; if (index >= arr.Length) return arr[0]; return arr[index]; } 

General version:

 public static T IndexOrDefault<T>(this T[] arr, Int32 index) { if (arr == null || arr.Length == 0) return default(T); if (index >= arr.Length) return arr[0]; return arr[index]; } 

Is there a default replacement there that can return an empty common value instead of a null?

No, there is no way to get an empty value for a generic type. Most types do not have a null value at all. For example, int cannot be empty.

+1
source

No, null is the default value for string . You need to either add code that specifically checks when T is a string, or use a different approach.

0
source

You can try to return new T() - but you need to add a where T : new constraint in the method declaration.

Otherwise, the default value for the string is null. So, the next option is to create a line overload ... I know that this is not the best option, but you can handle it.

0
source

You can check the "special" type of string . In addition, your code does not do what you said it would do - fixed some boundary conditions for you:

 public static T IndexOrDefault<T>(this T[] arr, Int32 index) { if (arr == null || arr.Length == 0) return default(T); return arr.Length <= index ? arr[0] : arr[index]; } 
0
source

What happens when you return the default value of T?

0
source

All Articles