ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Or

Returns the object if it's not null or the first object which is not null.

Source

public static T Or<T>(this T t, params T[] args)
{
    foreach (var item in args)
        if (item != null) return item;
    return default(T);
}

Example

string s = GetValue(); // s == null
Console.WriteLine(s.Or("value not found")):

Author: Weidling C

Submitted on: 7 dec 2009

Language: C#

Type: System.Object<T>

Views: 3613