I have a List<Points>() and I want to sort it with a custom comparer function.
I made:
public int MyCompare(Point p1, Point p2)
{
...
}
// In my main
// ...
points_.Sort(MyCompare);
// ...
I works, all right.
Now I want to sort everything but the first element, so I thought to do:
points_.Sort(1, points_.Count()-1, MyCompare);
But with this overload he wants as argument an IComparer.
How can I solve this?
Note that Point is not a custom class, it is from Xna framework. I don't want to implement a custom class with : IComparer
If you do not want to implement IComparer, you could create one from a delegate using the Comparer<T>.Create static method, like this:
points_.Sort(1, points_.Count()-1, Comparer.Create(MyCompare));
or even
points_.Sort(1, points_.Count()-1, Comparer.Create((a, b) => {
... // comparison logic goes here
}));
As pointed out by #dasblinkenlight with .NET 4.5+ there's an ad-hoc method to convert a Comparison<T> delegate to an IComparer<T>.
But if you're stuck with a lower version, you can use this class to convert a Comparison<T> delegate to IComparer<T>:
public class DelegateComparer<T> : IComparer<T>
{
private readonly Comparison<T> compDelegate;
public DelegateComparer(Comparison<T> compDelegate)
{
if (compDelegate == null)
throw new ArgumentNullException("compDelegate");
this.compDelegate = compDelegate;
}
public int Compare(T x, T y)
{
return compDelegate(x, y);
}
}
Usage example:
points_.Sort(1, points_.Count()-1, new DelegateComparer<Point>(MyCompare));
Related
How would an IComparer that needs an argument be implemented (might not be relevant but I'm using it on Linq query)?
I suppose it should be called like this:
ListOfObjectsToSort.orderBy(x => x, myCustomComparer(argument));
And this is what i found on how to implement the IComparer but i can't figure out how to change it to pass the argument here:
public class MyComparer : IComparer<object>
{
public int Compare(object x, object y)
{
// code will then return 1,-1 or 0
You can't add an argument to the Compare method or you violate the interface contract. Add a property to the class that can be used in the method:
public class MyComparer : IComparer<object>
{
public int MyArgument {get; set;}
public int Compare(object x, object y)
{
// code will then return 1,-1 or 0
// use MyArgument within the method
}
You can set it in the constructor:
public MyComparer(int argument)
{
MyArgument = argument;
}
Then your syntax would be:
var myCustomComparer = new MyComparer(argument);
ListOfObjectsToSort.orderBy(x => x, myCustomComparer);
or just
ListOfObjectsToSort.orderBy(x => x, new MyComparer(argument));
the problem is that this cannot be done exactly as you're asking.
The reason for this is that you're trying to change the signature of the Compare method, which would result in outside libraries (like Linq!) being unable to call the method, because the arguments they pass are no longer the arguments needed. Thus, the compiler simply does not allow this.
However, there is a way around this, for this particular case, in that since IComparers are used as objects, you could create a class which implements IComparer and takes a custom object in the constructor, saving it to a field and using that for comparison calculations. Thus, you end up with
var comp = new CustomComparer(argument);
ListOfObjectsToSort.OrderBy(x => x, comp);
In JQuery you can write $('.my-class').hide() and it will call hide() on all the results. There's no for loop, no iterating, no LINQ extensions and lambdas etc. and it makes dealing with lists super fun. I want to be able to have this functionality on IEnumerables in C#. I think Matlab has a similarly concise syntax when operating on arrays/matrices.
Long story short, I want the following code (or similar) to work:
class Program
{
static List<MyClass> MyList = new List<MyClass>();
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
MyList.Add(new MyClass());
MyList.MyMethod();
// should be exactly equivalent to:
MyList.Select(n => n.MyMethod());
}
}
class MyClass
{
public int MyMethod() { return 123; }
}
I'm aware this is possible on a case-by-case basis using extension methods:
public static IEnumerable<int> MyMethod(this IEnumerable<MyClass> lst)
{
return lst.Select(n => n.MyMethod());
}
But we'd have to create one extension method for every single method on every single type that you wanted this behaviour on.
Ideally this would be possible for all types and all methods and still be type-safe at compile time. I suspect I'm asking too much from the C# language here, but how would we do this or something similar in a as-generic-as-possible way?
Possible solutions:
Auto-generate extension methods for particular types. If we only intend to use this notation for a few types, we could just generate the extension methods once automatically. This would achieve the exact syntax and full type safety but generating code would be a pain.
A single extension method that returns a dynamic object built using reflection on the supplied type. The idea is that we'd use reflection to iterate through the type's methods and build up a dynamic object that would have all the methods like .MyMethod() that would behind the scenes call Select(...) on the IEnumerable. The syntax would end up being something like MyList.Selector().MyMethod(). But now we've lost the syntax and type safety. Clever, maybe. Useful, probably not.
Intercepting method calls? Is it possible to decide how to react to a method call at runtime? I don't know. Again you'd lose type safety.
The most simple solution is using dynamic objects. If you are willing to throw away type safety, you can make a IEnumerable type that behaves statically when needed and dynamically otherwise, here's a sample prototype:
public class DynamicIEnumerable<T> : DynamicObject, IEnumerable<T>
{
public IEnumerable<T> _enumerable;
public DynamicIEnumerable(IEnumerable<T> enumerable)
{
this._enumerable = enumerable;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
result = new DynamicIEnumerable<T>(_enumerable.Select(x => (T)typeof(T).InvokeMember(binder.Name, BindingFlags.InvokeMethod, null, x, null)));
return true;
}
public IEnumerator<T> GetEnumerator()
{
return _enumerable.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _enumerable.GetEnumerator();
}
}
In TryInvokeMember, the invoked member on IENumerable is applied on all items using reflection. The only constraints on this approach is that you have to return this in invoked method. Here's a sample of how to use this approach:
public class SomeClass
{
public int Value {get;set;}
public SomeClass(int value)
{
this.Value = x;
}
public SomeClass Plus10()
{
Value += 10;
return this;
}
}
static void Main()
{
dynamic d = new DynamicIEnumerable<X>(Enumerable.Range(0, 10).Select(x => new SomeClass(x)));
foreach (var res in d.Plus10().Plus10())
Console.WriteLine(res.Value);
}
how would we do this or something similar in a as-generic-as-possible way?
This isn't a pretty solution but it does work:
public class MyClass
{
public void MyMethod()
{
}
public void MyMethod2()
{
}
}
Extension Method:
public static class WeirdExtensions
{
public static IEnumerable<T> CallOnAll<T>(this IEnumerable<T> instance ,
Action<T> call)
{
foreach(var item in instance)
{
call(item);
}
return instance;
}
}
Usage (chaining/fluent):
var blah = new List<MyClass>();
blah.CallOnAll(b => b.MyMethod())
.CallOnAll(b => b.MyMethod2());
Notes
This isn't quite possible due to a the underlying assumption that you'd have to every single method on every single type. In jQuery/Html there is only one underlying type of an Html Element. All elements are exposed to the same methods (whether or not the type supports it). In jQuery, you can call $('head').hide() but it won't do anything visually, but because it is an element, it will be inline styled. If you need a new method, you do have a build one, but for only one type because there is only one type.
In contrast with C# you build your types (many many types) and they all have different methods (sure there could be overlap).
I have a function I'd like to write where return a sorted list. I want to pass in a custom comparison for the objects. The examples below are rather oversimplified, but if I can get past this, I should be set.
I've tried declaring a delegate of the right type (I think):
public delegate int ObjectSorter(MyObject x, MyObject y);
Calling it with the right syntax:
GetList(delegate(MyObject a, MyObject b) { return a.CompareTo(b); });
But when I pass that to the list, I get that there are argument problems:
public List<MyObject> GetList(ObjectSorter os)
{
List<MyObject> objectList = FillTheList();
objectList.Sort(os); // Invalid
return ObjectList;
}
So trying a different approach:
GetList((x, y) => { return x.CompareTo(y); });
public List<MyObject> GetList(Func<MyObject, MyObject, int> sorter)
{
List<MyObject> objectList = FillTheList();
objectList.Sort(sorter); // Invalid also
// This syntax DOES work, but too specific. And why does it work?
nl.Sort((x, y) => x.CompareTo(y));
return ObjectList;
}
Doesn't work either.
I'm trying to make this as easy as possible for the caller to get a custom comparison into the function, and for the function to know as little as possible about the workings of the sort itself. I'd rather not have the caller go through all of the trouble of creating an class derived from IComparer and passing that in.
Should be:
objectList.Sort(new Comparison<MyObject>(sorter));
Or just change your method signature to:
public List<MyObject> GetList(Comparison<MyObject> sorter)
Comparison<MyObject> is equivalent to your Func<MyObject, MyObject, int> sorter.
nl.Sort((x, y) => x.CompareTo(y)); works because the compiler automatically turns (x, y) => x.CompareTo(y) into a Comparison<MyObject> delegate. When you have the code (x, y) => x.CompareTo(y), the compiler uses the context to determine its type.
Once it's stored as a specific delegate type (e.g. Func<MyObject, MyObject, int> or Comparison<MyObject>), that is its type, and it will not implicitly be converted to another. However, it can be explicitly converted to another type if it is compatible, as in new Comparison<MyObject>(sorter).
You can pass in a function using Func for example:
private void Foo(Func<MyObject, MyObject, int> sortMethod)
{
list.Sort(new Comparison<MyObject>(sortMethod));
}
Example sort method:
public static int SortBytName(MyObject x, MyObject y)
{
return x.Name.CompareTo(y.Name);
}
Is there a way of passing in a method to a function as a parameter and then calling it via list.Sort()? I've tried this:
public static string BuildHumanSitemap(Func<TreeNode, TreeNode, int> sortMethod, params string[] classNames)
{
//calling list sort on method passed as parameter
nodes.sort(sortMethod);
}
Where the functions i want to pass in all take the same params e.g.
private static int SortByDateCreated(TreeNode x, TreeNode y)
{
DateTime xT = (DateTime)x["DocumentCreatedWhen"];
DateTime yT = (DateTime)y["DocumentCreatedWhen"];
return xT.CompareTo(yT);
}
I've also tried using an Action delegate type but the sort method complains when i pass it as a parameter. Can anyone offer a suggestion on how to do this?
Thankyou
Create new Comparison delegate and pass it to Sort method:
nodes.Sort(new Comparison<TreeNode>(sortMethod));
Maybe instead of taking in a Func<,,> delegate, you should consume a Comparison<> delegate. Because that's what List<> wants (for historical reasons; the List<>.Sort method was written for .NET 2.0, before the Func delegates were introduced).
Therefore:
public static string BuildHumanSitemap(Comparison<TreeNode> sortMethod, params string[] classNames)
{
//calling list sort on method passed as parameter
nodes.Sort(sortMethod);
}
Then call your method very simply like this:
BuildHumanSitemap(SortByDateCreated);
where SortByDateCreated is the "method group" from your question.
There's no need for first creating a delegate instance of type Func<TreeNode, TreeNode, int> and then create another delegate instance (of type Comparison<TreeNode>) which references the first one.
Of course you can also call your BuildHumanSitemap method with a lambda arrow as the first argument.
It works this way:
TreeView.TreeViewNodeSorter = new CustomNodeSorter();
private class CustomNodeSorter : IComparer
{
public int Compare(object x, object y)
{
DateTime xT = (DateTime)x["DocumentCreatedWhen"];
DateTime yT = (DateTime)y["DocumentCreatedWhen"];
return xT.CompareTo(yT);
}
}
Solution with IComparer<T>.
Comparer
public class MyTreeNodeComparer : IComparer<TreeNode>
{
public int Compare(TreeNode x, TreeNode y)
{
DateTime xT = (DateTime)x["DocumentCreatedWhen"];
DateTime yT = (DateTime)y["DocumentCreatedWhen"];
return xT.CompareTo(yT);
}
}
Usage
list.Sort(new MyTreeNodeComparer());
How would you do specialization in C#?
I'll pose a problem. You have a template type, you have no idea what it is. But you do know if it's derived from XYZ you want to call .alternativeFunc(). A great way is to call a specialized function or class and have normalCall return .normalFunc() while have the other specialization on any derived type of XYZ to call .alternativeFunc(). How would this be done in C#?
In C#, the closest to specialization is to use a more-specific overload; however, this is brittle, and doesn't cover every possible usage. For example:
void Foo<T>(T value) {Console.WriteLine("General method");}
void Foo(Bar value) {Console.WriteLine("Specialized method");}
Here, if the compiler knows the types at compile, it will pick the most specific:
Bar bar = new Bar();
Foo(bar); // uses the specialized method
However....
void Test<TSomething>(TSomething value) {
Foo(value);
}
will use Foo<T> even for TSomething=Bar, as this is burned in at compile-time.
One other approach is to use type-testing within a generic method - however, this is usually a poor idea, and isn't recommended.
Basically, C# just doesn't want you to work with specializations, except for polymorphism:
class SomeBase { public virtual void Foo() {...}}
class Bar : SomeBase { public override void Foo() {...}}
Here Bar.Foo will always resolve to the correct override.
Assuming you're talking about template specialization as it can be done with C++ templates - a feature like this isn't really available in C#. This is because C# generics aren't processed during the compilation and are more a feature of the runtime.
However, you can achieve similar effect using C# 3.0 extension methods. Here is an example that shows how to add extension method only for MyClass<int> type, which is just like template specialization. Note however, that you can't use this to hide default implementation of the method, because C# compiler always prefers standard methods to extension methods:
class MyClass<T> {
public int Foo { get { return 10; } }
}
static class MyClassSpecialization {
public static int Bar(this MyClass<int> cls) {
return cls.Foo + 20;
}
}
Now you can write this:
var cls = new MyClass<int>();
cls.Bar();
If you want to have a default case for the method that would be used when no specialization is provided, than I believe writing one generic Bar extension method should do the trick:
public static int Bar<T>(this MyClass<T> cls) {
return cls.Foo + 42;
}
I was searching for a pattern to simulate template specialization, too. There are some approaches which may work in some circumstances. However what about the case
static void Add<T>(T value1, T value2)
{
//add the 2 numeric values
}
It would be possible to choose the action using statements e.g. if (typeof(T) == typeof(int)). But there is a better way to simulate real template specialization with the overhead of a single virtual function call:
public interface IMath<T>
{
T Add(T value1, T value2);
}
public class Math<T> : IMath<T>
{
public static readonly IMath<T> P = Math.P as IMath<T> ?? new Math<T>();
//default implementation
T IMath<T>.Add(T value1, T value2)
{
throw new NotSupportedException();
}
}
class Math : IMath<int>, IMath<double>
{
public static Math P = new Math();
//specialized for int
int IMath<int>.Add(int value1, int value2)
{
return value1 + value2;
}
//specialized for double
double IMath<double>.Add(double value1, double value2)
{
return value1 + value2;
}
}
Now we can write, without having to know the type in advance:
static T Add<T>(T value1, T value2)
{
return Math<T>.P.Add(value1, value2);
}
private static void Main(string[] args)
{
var result1 = Add(1, 2);
var result2 = Add(1.5, 2.5);
return;
}
If the specialization should not only be called for the implemented types, but also derived types, one could use an In parameter for the interface. However, in this case the return types of the methods cannot be of the generic type T any more.
By adding an intermediate class and a dictionary, specialization is possible.
To specialize on T, we create an generic interface, having a method called (e.g.) Apply. For the specific classes that interface is implemented, defining the method Apply specific for that class. This intermediate class is called the traits class.
That traits class can be specified as a parameter in the call of the generic method, which then (of course) always takes the right implementation.
Instead of specifying it manually, the traits class can also be stored in a global IDictionary<System.Type, object>. It can then be looked up and voila, you have real specialization there.
If convenient you can expose it in an extension method.
class MyClass<T>
{
public string Foo() { return "MyClass"; }
}
interface BaseTraits<T>
{
string Apply(T cls);
}
class IntTraits : BaseTraits<MyClass<int>>
{
public string Apply(MyClass<int> cls)
{
return cls.Foo() + " i";
}
}
class DoubleTraits : BaseTraits<MyClass<double>>
{
public string Apply(MyClass<double> cls)
{
return cls.Foo() + " d";
}
}
// Somewhere in a (static) class:
public static IDictionary<Type, object> register;
register = new Dictionary<Type, object>();
register[typeof(MyClass<int>)] = new IntTraits();
register[typeof(MyClass<double>)] = new DoubleTraits();
public static string Bar<T>(this T obj)
{
BaseTraits<T> traits = register[typeof(T)] as BaseTraits<T>;
return traits.Apply(obj);
}
var cls1 = new MyClass<int>();
var cls2 = new MyClass<double>();
string id = cls1.Bar();
string dd = cls2.Bar();
See this link to my recent blog and the follow ups for an extensive description and samples.
I think there is a way to achieve it with .NET 4+ using dynamic resolution:
static class Converter<T>
{
public static string Convert(T data)
{
return Convert((dynamic)data);
}
private static string Convert(Int16 data) => $"Int16 {data}";
private static string Convert(UInt16 data) => $"UInt16 {data}";
private static string Convert(Int32 data) => $"Int32 {data}";
private static string Convert(UInt32 data) => $"UInt32 {data}";
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Converter<Int16>.Convert(-1));
Console.WriteLine(Converter<UInt16>.Convert(1));
Console.WriteLine(Converter<Int32>.Convert(-1));
Console.WriteLine(Converter<UInt32>.Convert(1));
}
}
Output:
Int16 -1
UInt16 1
Int32 -1
UInt32 1
Which shows that a different implementation is called for different types.
Some of the proposed answers are using runtime type info: inherently slower than compile-time bound method calls.
Compiler does not enforce specialization as well as it does in C++.
I would recommend looking at PostSharp for a way to inject code after the usual compiler is done to achieve an effect similar to C++.
A simpler, shorter and more readable version of what #LionAM proposed (about half of the code size), shown for lerp since this was my actual use case:
public interface ILerp<T> {
T Lerp( T a, T b, float t );
}
public class Lerp : ILerp<float>, ILerp<double> {
private static readonly Lerp instance = new();
public static T Lerp<T>( T a, T b, float t )
=> ( instance as ILerp<T> ?? throw new NotSupportedException() ).Lerp( a, b, t );
float ILerp<float>.Lerp( float a, float b, float t ) => Mathf.Lerp( a, b, t );
double ILerp<double>.Lerp( double a, double b, float t ) => Mathd.Lerp( a, b, t );
}
You can then just e.g.
Lerp.Lerp(a, b, t);
in any generic context, or provide the method as a grouped Lerp.lerp method reference matching T(T,T,float) signature.
If ClassCastException is good enough for you, you can of course just use
=> ( (ILerp<T>) instance ).Lerp( a, b, t );
to make the code even shorter/simpler.
If you just want to test if a type is derrived from XYZ, then you can use:
theunknownobject.GetType().IsAssignableFrom(typeof(XYZ));
If so, you can cast "theunknownobject" to XYZ and invoke alternativeFunc() like this:
XYZ xyzObject = (XYZ)theunknownobject;
xyzObject.alternativeFunc();
Hope this helps.