开发者

What's the meaning of this code?

I just started working in a .Net company and I saw this code but I don't understand what it does. Could somebody shed some light on me ? thanks

/// <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 开发者_Python百科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)));

}


First, is it so smart to publish your employer's code?

Second, apparantly some other part of the code wants empty values (and 0 is threated as empty) to be null values. Value types are used as nullable. That means they can be null, even if they really can't be, so HasValue is used to find out if a variable is explicitly set.


Greetings.

Basicly it does what is being said in the summarry . The ËmptyToNull returns a null or the value of the string. This is used as a Extension Method (you can see this because it's static and uses "this string source", when properly used you can use something like this:

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

The second and third part checks the possible types (byte, short, int etc) if they are null and if so, the second returns null, and the third returns a NullAble, NullAble, NullAble etc. This is also a Extension method.


It checks whether a Nullable structure is empty, and if so returns just null in it's stead, and if not it just returns the same Nullable.

The next piece of code does the same thing the other way around, it makes an Nullable structure of the type of the object for empty object.


Method 1 is an string extension method, it allows you to write:

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

This will make s1 null rather than an empty string.

The second two methods carry out the same action, but as they are generic methods they can be called for any nullable type (however they will not actually work for any nullable type as the programmer has only accounted for a set group of nullable types in the implementation).

If someone attempts to call these with a type that is not supported an exception is thrown.


You need to know about:

  • generic methods and generic type constraint (where)
  • the struct keyword, which means value type.
  • Nullable types (which you may know by their short writing, i.e. int? i = null;)
  • extension methods.

So what this code does is transform any default value for value type into null, and transform string.empty into null. I can think of no sane reason someone would want to do something like this, but I'll give you a way to make that code more readable, more concise and able to handle personalized value types (enum and struct value types).

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);
}

It's shorter and avoids writing that ugly NotImplementedException.

Don't let yourself get confused by the double meaning some C# keywords have, depending on where they are used.

  • struct is also a type of class declaration, but it also means "value type" in a generic type constraint.
  • ? is the conditional/ternary operator, but also means Nullable when apposed to a value type variable declaration.
  • this generally references the current object, but is also used to define extension methods.

You can also read about the default keyword.

And I don't think there is a problem here copying your employer's code, it's really generic, we have no idea who your employer is, and I don't see how anyone would ever want to copy it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜