这段代码的含义是什么?

时间:2011-02-17 09:15:27

标签: c# .net

我刚开始在一家.Net公司工作,我看到了这段代码,但我不明白它的作用。有人可以对我有所了解吗?感谢

/// <summary>
/// If string is string.Empty ("") return null, else returns the copy of the original reference passed in.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string EmptyToNull(this string str)
{
    return string.IsNullOrEmpty(str) ? null : str;
}

/// <summary>
/// Converts some native .Net nullable instances of immutable structures to null if they are empty.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static Nullable<T> EmptyToNull<T>(this Nullable<T> obj) where T: struct
{
    if (obj == null) return null;
    else if (obj is Nullable<byte>) return (obj as byte?) == 0 ? null : obj;
    else if (obj is Nullable<short>) return (obj as short?) == 0 ? null : obj;
    else if (obj is Nullable<int>) return (obj as int?) == 0 ? null : obj;
    else if (obj is Nullable<long>) return (obj as long?) == 0 ? null : obj;
    else if (obj is Nullable<double>) return (obj as double?) == 0 ? null : obj;
    else if (obj is Nullable<float>) return (obj as float?) == 0 ? null : obj;
    else if (obj is Nullable<DateTime>) return (obj as DateTime?) == DateTime.MinValue ? null : obj;
    else if (obj is Nullable<Guid>) return (obj as Guid?) == Guid.Empty ? (T)default(Nullable<T>) : obj; 
    else throw new NotImplementedException(string.Format("Method not implemented for type {0}", typeof(Nullable<T>)));

}

/// <summary>
/// Converts some native .Net immutable structures to null if they are empty.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static Nullable<T> EmptyToNull<T>(this T obj) where T : struct
{
    var val = new Nullable<T>();
    val = obj;

    if (!val.HasValue) return (T)val;
    else if (obj is byte) return (byte)(object)val.Value == 0 ? new Nullable<T>() : obj;
    else if (obj is short) return (short)(object)val.Value == 0 ? new Nullable<T>() : obj;
    else if (obj is int) return (int)(object)val.Value == 0 ? new Nullable<T>() : obj;
    else if (obj is long) return (long)(object)val.Value == 0 ? new Nullable<T>() : obj;
    else if (obj is double) return (double)(object)val.Value == 0 ? new Nullable<T>() : obj;
    else if (obj is DateTime) return (DateTime)(object)val.Value == DateTime.MinValue ? new Nullable<T>() : obj;
    else if (obj is Guid) return (Guid)(object)val.Value == Guid.Empty ? new Nullable<T>() : obj; 
    else throw new NotImplementedException(string.Format("Method not implemented for type {0}", typeof(T)));

}

5 个答案:

答案 0 :(得分:2)

问候。

基本上它完成了总结中所说的内容。 ËmptyToNull返回null或字符串的值。 这用作扩展方法(你可以看到它,因为它是静态的并且使用“this string source”,正确使用时你可以使用这样的东西:

string test = "I like big butts";
test.EmptyToNull();

第二部分和第三部分检查可能的类型(byte,short,int等),如果它们为null,如果是,则第二部分返回null,第三部分返回NullAble,NullAble,NullAble等。 这也是一种扩展方法。

答案 1 :(得分:2)

首先,发布雇主的代码是否如此聪明?

其次,显然代码的某些其他部分需要空值(并且0被声明为空)为空值。值类型用作可为空的。这意味着它们可以为null,即使它们确实不能,因此使用HasValue来确定是否显式设置了变量。

答案 2 :(得分:0)

它检查Nullable结构是否为空,如果是,则仅返回null,如果不是,则返回相同的Nullable

下一段代码以相反的方式执行相同的操作,它为空对象创建了对象类型的Nullable结构。

答案 3 :(得分:0)

方法1是一个字符串扩展方法,它允许你写:

string s1 = "";
string s2 = s1.EmptyToNull();

这将使s1为null而不是空字符串。

后两个方法执行相同的操作,但由于它们是泛型方法,因此可以为任何可空类型调用它们(但是它们实际上不适用于任何可空类型,因为程序员只考虑了一组可空的可空类实现中的类型。)

如果有人试图使用不受支持的类型调用这些类型,则会抛出异常。

答案 4 :(得分:0)

您需要了解:

  • 泛型方法和泛型类型约束(where)
  • struct关键字,表示值类型。
  • 可以为空的类型(您可以通过简短的写作来了解,即int? i = null;
  • 扩展方法。

所以这段代码的作用是将value类型的任何默认值转换为null,并将string.empty转换为null。我可以想到没有理由有人想做这样的事情,但我会给你一种方法来使代码更具可读性,更简洁并能够处理个性化的价值类型(enum和{{1值类型)。

struct

它更短,避免编写丑陋的NotImplementedException。

不要让自己对某些C#关键字的双重含义感到困惑,具体取决于它们的使用位置。

  • public static string EmptyToNull(this string obj) { return obj == string.Empty ? null : obj; } public static T? EmptyToNull<T>(this T obj) where T : struct { return obj.Equals(default(T)) ? default(T?) : obj; } public static T? EmptyToNull<T>(this T? obj) where T : struct { return obj == null ? obj : EmptyToNull(obj.Value); } 也是type of class declaration,但在generic type constraint中也表示“值类型”。
  • 当与价值类型变量声明相关时,
  • structconditional/ternary operator,也是means ?
  • this通常引用当前对象,但也用于定义extension methods

您还可以阅读default关键字。

我不认为复制雇主代码存在问题,这是非常通用的,我们不知道你的雇主是谁,我也看不出有人想要复制它。