📜  c# add int var - C# (1)

📅  最后修改于: 2023-12-03 14:59:39.766000             🧑  作者: Mango

C# Add Int Variable

When programming in C#, you may need to add integer variables to perform various operations. In this tutorial, we will learn how to add integer variables in C#.

Syntax

The syntax for adding integer variables in C# is simple. It is as follows:

int variable1 = 5;
int variable2 = 10;
int sum = variable1 + variable2;

In this code, we have created two integer variables named variable1 and variable2. We have assigned them the values of 5 and 10, respectively. Finally, we have added these two variables and assigned their sum to a new integer variable named sum.

Example

Here is an example of adding integer variables in C#:

using System;

namespace AddIntVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            int y = 10;
            int sum = x + y;

            Console.WriteLine("The sum of " + x + " and " + y + " is " + sum);
        }
    }
}

In this code, we have created two integer variables named x and y. We have assigned them the values of 5 and 10, respectively. Finally, we have added these two variables and assigned their sum to a new integer variable named sum. We have then printed the result to the console using the Console.WriteLine() method.

Conclusion

Adding integer variables in C# is a basic operation that is necessary for performing various tasks in programming. With the simple syntax and examples provided in this tutorial, you should now be able to add integer variables with ease in your C# programs.