Home > Net > Questions about generic objects using arithmetic operators
Questions about generic objects using arithmetic operators
Time:10-02
I want to have a calculation with variables of type T But it does not support arithmetic operators , is there a way to solve this problem?
Using System; using System.Collections.Generic; using System.Linq; using System.Text; Using System. The Threading. The Tasks;
The namespace ConsoleApplication2 { Class Program { The static void Main (string [] args) { A The Add ();//T type is not suitable for this method is A The Add ();//jijubhuibuib A The Add ();
The Console. ReadKey (); } } The class a { Private static T T; Public static T the Add () { //(1) a subclass object can be directly assigned to the parent class variables; //(3) if the superclass object containing is subclass object, can be this strong superclass object into a subclass object; //is: if the transformation is successful, returns true, or false on failure. If (t is int | | (t as string==null))//if the return to true proved t type int and string { Console. WriteLine (" copy and connection or add "); T=t + t; return t; } The else { Console. WriteLine (" T type is not suitable for this method "); }
} } }
This is my search of a former post, this post seems to be no solutions to the https://bbs.csdn.net/topics/210029007
CodePudding user response:
Using System; using System.Collections.Generic; using System.Linq; using System.Text; Using System. The Threading. The Tasks;
The namespace ConsoleApplication2 { Class Program { The static void Main (string [] args) { A The Add ();//T type is not suitable for this method is A The Add ();//jijubhuibuib A The Add ();
The Console. ReadKey (); } } The class a { Private static T T; Private static string tt; Public static T the Add () { //(1) a subclass object can be directly assigned to the parent class variables; //(3) if the superclass object containing is subclass object, can be this strong superclass object into a subclass object; //is: if the transformation is successful, returns true, or false on failure. If (t is int | | (t as string==null))//if the return to true proved t type int and string { Console. WriteLine (" copy and connection or add "); Tt=t.T oString () + t.T oString (); return t; } The else { Console. WriteLine (" T type is not suitable for this method "); }
Return the default (T);
} } }
CodePudding user response:
You need to reload the arithmetic operators
CodePudding user response:
If you want to do a lot of arithmetic operation, you don't do generics,