C#Generic - 转换此函数? [英] C# Generic - Convert this function?

查看:64
本文介绍了C#Generic - 转换此函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于仿制药,我非常环保。



我可以将这样的函数转换为通用函数吗?如果有的话,有人可以给我一个示例实现吗?



I am very green when it comes to generics.

Can I convert a function like this to a generic one, and if so can someone give me an example implementation of it?

public Dictionary<Sheet, bool> DeleteSheets(List<Sheet> sheets)
        {
            Dictionary<Sheet, bool> results = new Dictionary<Sheet, bool>();
            foreach (Sheet s in sheets)
                results.Add(s, DeleteSheet(s));

            return results;
        }





我有几个这样的函数,除了返回值,方法参数和调用的方法。添加功能是不同的类型。



就像我说的那样,我说这是绿色的....


谢谢!

推荐答案

尝试:

Try:
public Dictionary<T, bool> DeleteSheets<T>(List<T> data)
    {
    Dictionary<T, bool> results = new Dictionary<T, bool>();
    foreach (T s in data)
        results.Add(s, Delete<T>(s));

    return results;
    }
public bool Delete<T>(T s) { return true; }





格里夫,



Delete方法根据它的功能执行不同的代码,但它们都返回bool。我该怎么处理?使用代表吗?




这并不像听起来那么简单:你可以使用代表:



Griff,

The Delete method executes different code, based on what function it is, but they all return bools. How can I handle that? Use a delegate for this?


That's not as simple as it sounds: you can use a Delegate:

Dictionary<string, bool> data = DeleteSheets(new List<string>() { "hello", "goodbye" }, DeleteString);
    Dictionary<int, bool> data2 = DeleteSheets(new List<int>() { 1, 2, 3 }, DeleteString);
    }

bool DeleteString(string s) { Console.WriteLine(s); return true; }
bool DeleteString(int s) { Console.WriteLine("Del" + s ); return true; }
public delegate bool DeleteMethod<T>(T t);
public Dictionary<T, bool> DeleteSheets<T>(List<T> data, DeleteMethod<T> delete ) //where T: Base
    {
    Dictionary<T, bool> results = new Dictionary<T, bool>();
    foreach (T s in data)
        results.Add(s, delete(s));

    return results;
    }



它将调用相应的方法。



但我宁愿限制基类的泛型,而是通过重载来实现:


And it will call the appropriate method.

But I'd rather restrict the generic to a base class and do it via overloading instead:

Dictionary<Base, bool> data2 = DeleteSheets(new List<Base>() { new A(), new A(), new B() });
    }

public Dictionary<T, bool> DeleteSheets<T>(List<T> data ) where T: Base
    {
    Dictionary<T, bool> results = new Dictionary<T, bool>();
    foreach (T s in data)
        results.Add(s, s.Delete());

    return results;
    }
public abstract class Base
    {
    public abstract bool Delete();
    }
public class A : Base
    {
    public override bool Delete()
        {
        Console.WriteLine("A Delete");
        return true;
        }
    }
public class B : Base
    {
    public override bool Delete()
        {
        Console.WriteLine("B Delete");
        return true;
        }
    }





它看起来像是一个更清洁的解决方案。



It just seems like a "cleaner" solution.

这应该接近你想要的。不确定foreach中的DeleteSheets()应该做什么。如果我这样做,我也可以在那里提供帮助。



进行更改以将DeleteSheet函数作为参数。



This should be close to what you are looking for. Not sure what the DeleteSheets() in the foreach is supposed to do. If I did I might be able to help there too.

Made changes to take the DeleteSheet func as a parameter.

public Dictionary<T, bool> DeleteSheets<T>(List<T> sheets, Func<T, bool> delT)
       {
           Dictionary<T, bool> results = new Dictionary<T, bool>();
           foreach (T s in sheets)
               results.Add(s, delT(s));

           return results;
       }


只需用字母替换你想要通用的类型(约定是 T ):

Just replace the type you want to be generic with a letter (the convention is T):
public Dictionary<T, bool> DeleteSheets(List<T> sheets)
        {
            Dictionary<T, bool> results = new Dictionary<T, bool>();
            foreach (T s in sheets)
                results.Add(s, DeleteSheet(s));

            return results;
        }


这篇关于C#Generic - 转换此函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆