开发者

How to return an Empty value instead of a null in C#

开发者 https://www.devze.com 2023-03-17 11:01 出处:网络
I have an extension for string arrays which returns an empty String if the array is null, if the array length is == 1 it returns the first element otherwise element at index position is returned, no b

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

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 figured this extension method could 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 default(String) is not String.Empty but null. So I'm returning nulls now for Strings, which was the first requirement...

Is there a replacement for default there that could return an empty generic value instead of a null?

Edit 2: (first one was the definition)

开发者_高级运维

I'm using this approach now, which it works, but it's not as elegant as I wanted (well, the method is not elegant either, 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 the generic one for the rest.


There is no generic default other than null for reference types and some variant of 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];
}


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

Not quite. The method returns the first element if the length of the array is one, but it doesn't check the index at all. What you describe 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];
}

The generic version would be:

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 replacement for default there that could return an empty generic value instead of a null?

No, there is no way of getting an empty value for a generic type. Most types doesn't have any empty value at all. An int for example can't be empty.


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


You could try returning a new T() - but you'll need to add a where T : new constraint on your method declaration.

Otherwise, default of string is null. So the next option is to create a string overload... I know it’s not the best option but you may manage with it.


You could check for the "special" type string. Also your code is not doing what you said it would do - fixed some edge 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];
}


What happens when you return default of T?

0

精彩评论

暂无评论...
验证码 换一张
取 消