Biurrun Biurrun - 2 years ago 121
Java Question

Equivalent in c# of java generic type parameter

I have this method in Java which I would like to implement in C#, but I don't know exactly how.

public static <T> Set<T> getAddedItems(Set<T> before, Set<T> after) {
Set<T> out = new HashSet<T>(after);
return out;

For implementing
, I have chosen the class
. The problem is that I don't know what to do with the generic type parameter
. I am quite new to C# and in Java I hadn't worked with generics until a couple weeks ago, so my apologies if my question is too obvious.

Answer Source

Maybe something like this:

public static HashSet<T> getAddedItems<T>(HashSet<T> before, HashSet<T> after)
  HashSet<T> out2 = new HashSet<T>(after);
  out2.RemoveWhere(x => before.Contains(x));
  return out2;

out is a keyword in C#, so i've renamed that slightly. Also note that HashSet doesn't have RemoveAll(), but RemoveWhere() appears to do the same thing (needs linq though).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download