I have written the following method to return a list of Unserializable classes (LINQ classes) from a list of Serializable classes (POCOs):
List<UnSerializableEntity> ToListOfUnserializables(List<SerializableEntity> entityList) { var tempList = new List<UnSerializableEntity>(); entityList.ForEach(e => { if (e != null) { tempList.Add(ConvertFromSerializableToUnserializable(e)); } }); return tempList; }
Now, Resharper has ‘complained’ about this line: if (e != null), and suggested to change it to this:
if (!Equals(e, default(SerializableEntity)))
My question is to what has this change actually improved or prevented from happening? and I know the default keyword in this context has to do something with generics, but I’m not sure what it represents exactly.
PS. UnSerializableEntity and SerializableEntity are class generics.
If
SerializableEntityis a value type, it can never benull. Therefore, the body of yourifstatement will always execute because it is checking fornull. Thedefaultkeyword will return the default value of the generic type. For reference types, the default value isnull. For value types, it is zero (or whatever zero represents for that value type).If you only want reference types as entities, you should set constraints on your generic parameters. For example: