English 中文(简体)
如何创建和使用我的 C 通用类型?
原标题:How to create and use my Generic Type with C#?

我试图学习通用类型,我写了这个代码:

public class MyProp<TDictonary, TValor>
    {
        private List<TDictonary> MyDictionary = new List<TDictonary>();
        private List<TValor> TValue = new List<TValue>();

        public MyProp<TDictonary, TValue> Search(TDictonary dictionary)
        {
            MyProp<TDictonary, TValor> dic = new MyProp<TDictonary, TValor>();
            var test = MyDictionary.FirstOrDefault(item => item == dictionary);
            return dic;
        }

于是我试图创建 C# 字典。 我试图做的是将编辑和电视语言与列表联系起来, 将它们添加到我的通用类型中, 并将 MyProp 及其内容返回到 MyProp 。 我还计划添加其他方法, 如 添加、 删除等, 但如果我无法在一个列表中进行简单的搜索, 我肯定将无法继续前进 。

有人知道我是否走对路吗?如果没有,你能指对路吗?

谢谢

最佳回答

我试着创造出像C#字典一样的词典

您确实知道 < a href=> "" "http://msdn.microsoft.com/ en-us/library/xfhwa508.aspx" rel = "nofollow"\\ code>System. Collucations. Generic. dictionary< TKey, TValue> 但,对不对?

总之,我不知道你的通用名词(甚至类型)实际上代表什么(给他们更明确的名字!),所以我无法确切地告诉你你必须改变什么才能在字典上实际搜索。

然而,有一些提示:

  • In the method, you create an instance dic, do nothing on it and then simply return that – regardless of what you do previously.
  • You only work on the MyDictionary but completely ignore the TValue field.
  • Private fields are usually named in camelCase, i.e. starting a lower case character.
  • Naming a private field TValue is very confusing, due to the starting T. That is usually reserved for generic types only.

它给了我这个错误:错误 1 操作员 : 错误 1 操作员 : = = 无法应用到 TDittonary 和 TDittonary 类型的操作器 。

这是因为该类型是一种没有限制的通用类型。 因此它可以是参考类型,也可以是价值类型, 因为编译者无法决定对 < code_ / code > 运算符的比较。 简单但或许是限制, 修正就是指定一个基准类型, 要么是数值类型, 要么是参考类型:

public class MyProp<TDictonary, TValor>
    where TDictionary : class
问题回答

添加通用类型限制, 以便您可以调用 .Equals

public class MyProp<TKey, TValue>
   where TValue : IEquatable<TKey>


MyProp<TDictonary, TValor> prop = new MyProp<TDictonary, TValor>();
var val = MyDictionary.FirstOrDefault(item => item.Equals(dictionary));
prop.Key = val;
prop.Value = val;
return dic;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签