C#如果两个对象是相同类型,我如何比较? [英] C# how do I compare two objects if they are of same type?

查看:100
本文介绍了C#如果两个对象是相同类型,我如何比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用函数,

  List< Something&东西

public int countItems< T>(List< T> Items)
{
//这里我想比较Items的类型和something 看看他们是否兼容我该怎么办?
return 0;
}

它的一个C#通用类型问题!



  if(typeof(T) == typeof(Something)){...} 



请注意,有泛型取决于T(和行为不同)可能意味着你想要做的实际上不是非常 ...


I have a generic function,

List<Something> something;

public int countItems<T>(List<T> Items)
{
    // here i would like to compare the type of "Items" with the type of "something" to see if they are compatable how do i do it?
    return 0;
}

its a C# generic Type question!

解决方案

do you mean:

if(typeof(T) == typeof(Something)) {...}

?

Note that having generics depend hugely on the T (and act differently) may mean what you are trying to do isn't actually very generic...

这篇关于C#如果两个对象是相同类型,我如何比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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