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,

Such as can directly write dead for:

Class double precision Fourier transform
{
.
}

Class
Fourier transform{
.
}

CodePudding user response:

reference 1st floor tangyanzhi1111 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);

}
}
}

Bosses you this can realize the string connected operation, not achieve ints

CodePudding user response:

refer to the second floor OrdinaryCoder response:
you have to reload the arithmetic operators

The overloaded operators? And the specific how to do? Who can teach me?

CodePudding user response:

The
reference 3 floor github_36000833 response:
if you want to do a lot of arithmetic operation, don't do a generic,

Such as can directly write dead for:

Class double precision Fourier transform
{
.
}

Class
Fourier transform{
.
}

The bosses generics are used to do?
My understanding is a generic is a generic data type, so you can use when writing some method or class generic so that I can write the class or method can do processing method for a variety of data types or class
And I understand if there is any error or inaccurate, if please welcome bosses guidance for

CodePudding user response:

reference 5 floor xuerueheng reply:
Quote: refer to the second floor OrdinaryCoder response:

You need to reload the arithmetic operators

The overloaded operators? And the specific how to do? Who can teach me?


This is a generic programming, unable to generic overloaded operators, this approach won't solve,
Two ways to solve:
1. Use expression expression assembled addition
2. With the dynamic type of alternative generics

CodePudding user response:

 class Program 
{
The static void Main (string [] args)
{
Console. WriteLine (" Hello World!" );

Var a=new a ();
A.t=1;
Var ret=a.A dd ();

A.t="123";
Var ret1=a.A dd ();
}
}

Class A
{
Public static dynamic t;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related