通用类型参数C#“不能将'T'隐式转换为'int' [英] Generic type parameters C# "cannot implicitly convert 'T' as 'int'

查看:468
本文介绍了通用类型参数C#“不能将'T'隐式转换为'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我正在试图找出泛型



这是测试代码(这是我自己的)



我无法理解错误发生的原因不能隐含地将'T'转换为'int'

left变量肯定是 INT (int)left 之类的演员无效

你能否建议

Hi!

I'm trying to figure out Generics

Here's the test code (it's my own)

I cannot realize why the error occuires "Cannot implicitly convert 'T' as 'int'"
the "left" variable is definitely INT and the cast like (int)left is not useful
Could you please suggest

public class MyClass<t> //where T : IEnumerable<t>
{
    static Int32 iLeft;
    static string sLeft;

    public MyClass(T left)
    {
        if(typeof(T).Equals(typeof(Int32)))
        {
            <b>MyClass<t>.iLeft =left;     //the error - <b>Cannot implicitly convert 'T' as 'int'</b> </t></b>
        }
    }

    public static implicit operator MyClass<t>(T left)
    {
        return new MyClass<t>(left);
   }

    public static implicit operator Int32(MyClass<t> left)
    {
        if(left.Equals(typeof(Int32)))
        {
            return MyClass<t>.iLeft;
        }
        else
        {
            return -1;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyClass<int32> r = 5;
    }
}





[edit]已添加代码块 - OriginalGriff [/ edit] < br $> b $ b

我的尝试:



Jeffrey Richter C#,codeproject中的文章



[edit]Code block added - OriginalGriff[/edit]

What I have tried:

Jeffrey Richter C#, articles in codeproject

推荐答案

编译器不知道调用代码定义的T是什么 - 它可以是任何东西,字符串,双精度,动物类。所以它不能将它分配给你的int参数。



在你将它分配给你的财产之前,你必须将T转换为int。但是,只有当T为int时,你的代码才能工作,所以这是泛型的无意义使用,如果泛型类只能支持一种类型,你可能根本不使用泛型,而是使用强类型。



更新;



我会写下面的内容,但是我再次强调使用泛型是一个这里毫无意义。通常可以完全处理类型,或者处理基类型,但编写每种类型的异常只是不好的做法。



The compiler doesn't know what "T" is as that is defined by the calling code - it could be anything, a string, a double, an Animal class. So it can't allocate it to your int parameter.

You'd have to cast "T" into int before you assign it to your property. However, your code can only work if "T" is int so this is a pointless use of generics, if a generic class can only support one type you may as well not use generics at all and use strong types instead.

Updated;

I'd write it something like below, however again I stress that the use of generics is a bit pointless here. It's usually ok to deal with types completely generically, or to deal with base types, but writing per-type exceptions is just bad practice.

public class MyClass<T>
{
    public T iLeft { get; set; }
    public string sLeft { get { return this.iLeft.ToString(); } }

    public MyClass(T left)
    {
        iLeft = left;
    }

    public static implicit operator MyClass<T>(T left)
    {
        return new MyClass<T>(left);
    }

    public static implicit operator Int32(MyClass<T> left)
    {
        if (typeof(T).Equals(typeof(Int32)))
        {
            return (int)(object)left.iLeft;
        }
        else
        {
            return -1;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass<int> r = 5;
        MyClass<int> x = 10;

        Console.WriteLine("r after creation = " + r.iLeft);
        Console.WriteLine("x after creation = " + x.iLeft);

        r = 3;

        Console.WriteLine("r after assignment = " + r.iLeft);

        int rAsInt = r;
        int xAsInt = x;

        Console.WriteLine("rAsInt = " + rAsInt);
        Console.WriteLine("xAsInt = " + xAsInt);

        int y = x * 2;

        Console.WriteLine("x * 2 = " + y);

        int z = r * x;

        Console.WriteLine("r * x as ints= " + z);

        MyClass<int> classMult = r * x;

        Console.WriteLine("r * x as MyClass<int> = " + classMult);        
        Console.ReadLine();


这篇关于通用类型参数C#“不能将'T'隐式转换为'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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