You can use LINQ for this :) It will look good:
private bool moreThanOne(List<object> differentTypes)
{
return differentTypes.OfType<string>().Count() > 1;
}
Using:
List<object> listOfDifferentTypes = new List<object> { "string", 13, 52, "string", 54.3f };
var res = moreThanOne(listOfDifferentTypes);
I see that you are also checking some sort of identifier, and then try the following:
UPDATED to do what your code does
Updated: replaced .Count () with .Skip (1) .Any (), so it will stop if more than 1 is found :)
private void CheckType(object param, List<object> differentTypes)
{
var paramToCheck = param as TypeA;
if (paramToCheck == null) return;
var res = differentTypes.OfType<TypeA>().Where(t => t.ID == paramToCheck.ID).Skip(1).Any();
if (res) paramToCheck.m_Error = "error text";
}
As I have already done, you can replace:
if (param is TypeA)
{
TypeA paramToCheck = (TypeA) param;
... Do something
WITH
TypeA paramToCheck = param as TypeA; //Returns null if not a TypeA
if (param == null) return;
... Do something
This is a little faster :)