C # 's addition operation

Source: Internet
Author: User
Tags integer

The addition operator can be used for integer type, real type, enumeration type, string type, and representative type. This is done through operator overloading, and we'll cover the contents of operator overloading in the 11th chapter, where we just need to know that these operators can operate on different types of variables. The addition operator actually defines the following prototypes for integer and floating-point operations:

int operator + (int x,int y);

UINT operator + (uint x,uint y);

Long Operater + (long x,long y);

ULONG operator + (ulong X,ulong y);

Float operator + (float x,float y);

Double operator + (double x,double y);

Decimal operator + (decimal x,decimal y);

We know that in mathematical operations the result may be positive infinity, negative infinity, or the result may not exist. In C #, this is handled in accordance with the rules of the international IEEE 754 algorithm. Table 7-2 shows all possible combinations of the operands and the target type for non-0 finite, 0, infinite, and Nan values (Null values) when adding two numbers. X and y are nonzero finite values in the table, and z is the result of the "x+y" operation. If x and Y values are the same, but the symbol is the opposite, Z is zero. If "x+y" is too small and the target type cannot be represented, then Z is the 0 value of the same symbol as "x+y".

Table 7-2

Y +0 -0 +∞ -∞ NaN
X Z X X +∞ -∞ NaN
+0 Y +0 +0 +∞ -∞ NaN
-0 Y +0 -0 +∞ -∞ NaN
+∞ +∞ +∞ +∞ +∞ NaN NaN
-∞ -∞ -∞ -∞ NaN -∞ NaN
NaN NaN NaN NaN NaN NaN NaN

Enumeration type Addition

For variables of enumerated types, the prototype of the addition operator is:

E operator + (e x,u y);

E operator + (U x,e y);

Here e is the enumeration type, and U is the base type of E. This operation is equivalent to (E) ((U) x+ (u) y)

Program 7-2 demonstrates the addition operation of an enumeration type.

Program Listing 7-2:

Using System;

Enum weekday{
  sunday,monday,tuesday,wednesday,thursday,friday,saturday
};
Class Test

{public
  static void Main () {
    weekday day1=weekday.sunday;
    Weekday day2=weekday.saturday;
    Weekday day3=day1+6;
    Console.WriteLine (day1);
    Console.WriteLine (day2);
    Console.WriteLine (DAY3);
  }

The result of the program output is:
0
6
6

string addition

An addition operation can also be performed for object and string types, and the return value is always of type string, when the addition operator's prototype is:

String operator + (string x,string y);
String operator + (string x,string y);
String operator + (object x,string y);

For example, the string "Welcome" and "to you" the result of the addition is "Welcome to You".

Representative merging

The addition operator can also be used as a variable for the delegate type, which is what we call merging. Prototype for

D operator + (d x,d y);

where d is a delegate type.

If the two operands are of the same delegate type D, the addition operator performs the representation of the merge operation. If the first operand is null, the result is the value of the second operand. Conversely, if the second operand is null, the result is the value of the first operand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.