C# 这个代码是什么意思?

C# 这个代码是什么意思?,c#,.net,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> /// <r

我刚开始在一家.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)));

}
它检查一个可为null的结构是否为空,如果为空,则只返回null,否则只返回相同的可为null的结构

下一段代码的作用与此相反,它为空对象创建了一个对象类型的可空结构。

基本上,它做到了总结中所说的。 ËmptyToNull返回null或字符串的值。 这是一种扩展方法,您可以看到这一点,因为它是静态的,并且使用此字符串源,如果使用得当,您可以使用如下内容:

string test = "I like big butts";
test.EmptyToNull();
第二和第三部分检查可能的类型byte、short、int等是否为null,如果为null,第二部分返回null,第三部分返回null、null、null等。
这也是一种扩展方法。

首先,发布雇主代码是否明智


其次,代码的另一部分显然需要空值,而0被威胁为空值。值类型用作可空值。这意味着它们可以为null,即使它们确实不能为null,因此HasValue用于确定是否显式设置了变量。

方法1是一种字符串扩展方法,它允许您编写:

string s1 = "";
string s2 = s1.EmptyToNull();
这将使s1为null,而不是空字符串

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


如果有人试图用不受支持的类型调用这些函数,将引发异常。

您需要了解:

泛型方法和泛型类型约束,其中 struct关键字,表示值类型。 可为空的类型,您可以通过它们的简短书写来了解它们,即int?i=零; 扩展方法。 因此,这段代码所做的是将值类型的任何默认值转换为null,并将string.empty转换为null。我想不出有人想要这样做的合理原因,但我将为您提供一种方法,使代码更可读、更简洁,并且能够处理个性化的值类型enum和struct值类型

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);
}
它比较短,避免了编写丑陋的NotImplementedException

不要让自己被一些C关键字的双重含义所迷惑,这取决于它们的使用位置

struct也是一个,但它也表示a中的值类型。 ? 是,但在与值类型变量声明同位时也可为null。 通常引用当前对象,但也用于定义。 您还可以阅读有关关键字的信息


我不认为复制你的雇主代码有什么问题,它是很普通的,我们不知道你的雇主是谁,我也不知道有谁会想复制它。

也许会问你的一个同事?我问过,但我没有得到解释:除非你有很好的理由,删除别人花时间回答的问题是不礼貌的。谢谢,我想我必须仔细阅读一下什么是扩展方法和可为null。@流氓:不用担心,伙计,当然你还可以发布一些代码。这部分内容不是高度机密的,也不是针对您的程序的,因为我们使用了类似的扩展方法。您可以尝试查看常见问题解答。我没有尝试删除帖子。无法删除帖子。。只有评论/回复才可以。编辑无用的文本是他做的最好的选择