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

HasValueAndEquals

Substitutes this: int? index = GetIndex(); if (index.HasValue && index.Value == 10) ...

Source

public static bool HasValueAndEquals<T>(this Nullable<T> source, T target) 
  where T : struct
{
  return source.HasValue && source.Value.Equals(target);
}

public static bool HasValueAndEquals<T>(this Nullable<T> source, Nullable<T> target) 
  where T : struct
{
  return source.HasValue && source.Value.Equals(target);
}

Example

int? index = GetIndex();
if (index.HasValueAndEquals(10))...

Author: MatteoSp

Submitted on: 1 okt. 2008

Language: C#

Type: System.Nullable<T>

Views: 4694