GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Assignment Operators
Selected Reading
C# - Assignment OperatorsWhat Are Assignment Operators in C#?C# assignment operators assign values to variables along with the assignment; in some cases, these operators perform mathematical operators. List of C# Assignment OperatorsThere are following assignment operators supported by C# −
Example of Assignment OperatorsThe following example demonstrates all the assignment operators available in C# − using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
int a = 21;
int c;
c = a;
Console.WriteLine("Line 1 - = Value of c = {0}", c);
c += a;
Console.WriteLine("Line 2 - += Value of c = {0}", c);
c -= a;
Console.WriteLine("Line 3 - -= Value of c = {0}", c);
c *= a;
Console.WriteLine("Line 4 - *= Value of c = {0}", c);
c /= a;
Console.WriteLine("Line 5 - /= Value of c = {0}", c);
c = 200;
c %= a;
Console.WriteLine("Line 6 - %= Value of c = {0}", c);
c <<= 2;
Console.WriteLine("Line 7 - <<= Value of c = {0}", c);
c >>= 2;
Console.WriteLine("Line 8 - >>= Value of c = {0}", c);
c &= 2;
Console.WriteLine("Line 9 - &= Value of c = {0}", c);
c ^= 2;
Console.WriteLine("Line 10 - ^= Value of c = {0}", c);
c |= 2;
Console.WriteLine("Line 11 - |= Value of c = {0}", c);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result − Line 1 - = Value of c = 21 Line 2 - += Value of c = 42 Line 3 - -= Value of c = 21 Line 4 - *= Value of c = 441 Line 5 - /= Value of c = 21 Line 6 - %= Value of c = 11 Line 7 - <<= Value of c = 44 Line 8 - >>= Value of c = 11 Line 9 - &= Value of c = 2 Line 10 - ^= Value of c = 0 Line 11 - |= Value of c = 2 More Examples on C# Assignment OperatorsExample 1: Basic Assignment (=)In this example, we assign a value to a variable and print it. using System;
class Program
{
static void Main()
{
int x = 10; // Assigning value 10 to x
Console.WriteLine("x: " + x);
}
}
When the above code is compiled and executed, it produces the following result − x: 10 Example 2: Using += (Addition and Assignment)In this example, we add and assign a value using +=. using System;
class Program
{
static void Main()
{
int x = 5;
x += 3; // Equivalent to x = x + 3
Console.WriteLine("x after += : " + x);
}
}
When the above code is compiled and executed, it produces the following result − x after += : 8 Example 3: Using -= (Subtraction and Assignment)In this example, we subtract and assign a value using -=. using System;
class Program
{
static void Main()
{
int x = 10;
x -= 4; // Equivalent to x = x - 4
Console.WriteLine("x after -= : " + x);
}
}
When the above code is compiled and executed, it produces the following result − x after -= : 6 Example 4: Using *= (Multiplication and Assignment)In this example, we multiply and assign a value using *=. using System;
class Program
{
static void Main()
{
int x = 5;
x *= 2; // Equivalent to x = x * 2
Console.WriteLine("x after *= : " + x);
}
}
When the above code is compiled and executed, it produces the following result − x after *= : 10 Example 5: Using /= (Division and Assignment)In this example, we divide and assign a value using /=. using System;
class Program
{
static void Main()
{
int x = 20;
x /= 4; // Equivalent to x = x / 4
Console.WriteLine("x after /= : " + x);
}
}
When the above code is compiled and executed, it produces the following result − x after /= : 5 Example 6: Using %= (Modulus and Assignment)In this example, we use modulus and assign a value using %=. using System;
class Program
{
static void Main()
{
int x = 10;
x %= 3; // Equivalent to x = x % 3
Console.WriteLine("x after %= : " + x);
}
}
When the above code is compiled and executed, it produces the following result − x after %= : 1 Real-World Use Cases for Assignment OperatorsExample 7: Accumulating Scores in a GameIn this example, we add points to a player's score. using System;
class Program
{
static void Main()
{
int score = 50;
score += 10; // Add points
Console.WriteLine("Current Score: " + score);
}
}
When the above code is compiled and executed, it produces the following result − Current Score: 60 Example 8: Decreasing Inventory Stock in an E-Commerce SystemIn this example, we decrease stock when items are sold. using System;
class Program
{
static void Main()
{
int stock = 100;
stock -= 2; // Two items sold
Console.WriteLine("Stock Remaining: " + stock);
}
}
When the above code is compiled and executed, it produces the following result − Stock Remaining: 98 Example 9: Doubling Investment AmountIn this example, we double an investment amount. using System;
class Program
{
static void Main()
{
double investment = 1000;
investment *= 2; // Double the amount
Console.WriteLine("Total Investment: " + investment);
}
}
When the above code is compiled and executed, it produces the following result − Total Investment: 2000 Best PracticesThe following are the best practices while using assignment operators:
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |