Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# Variables & Operators: Practical Examples & Explanations

C# Variables

A variable in C# is a named storage location that holds a value of a specific data type. The data type determines the memory layout and the operations that can be performed on the variable.

For instance, a variable declared as string will store textual data, while an int holds whole numbers. Understanding the type is essential for effective coding and for avoiding runtime errors.

Below is a concise example that declares two variables of different types and displays their values:

Mastering C# Variables & Operators: Practical Examples & Explanations

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "The value is ";
            int val = 30;

            Console.Write(message + val);
            Console.ReadKey();
        }
    }
}

Code Breakdown

  1. Declare a string variable named message and assign the text "The value is ".
  2. Declare an int variable named val and assign the numeric value 30.
  3. Concatenate and output both values to the console.

Running the program produces the following output:

Mastering C# Variables & Operators: Practical Examples & Explanations

As shown, the string and int values are correctly combined and displayed.

C# Operators

Operators perform operations on variables and values. C# offers a wide range of operators categorized into arithmetic, relational, and logical groups.

Arithmetic Operators

OperatorDescription
+Adds two operands.
-Subtracts the second operand from the first.
*Multiplies both operands.
/Divides the numerator by the denominator.
%Returns the remainder of integer division.
++Increments an integer value by one.
--Decrements an integer value by one.

Relational Operators

OperatorDescription
==Checks if two operands are equal.
!=Checks if two operands are not equal.
>Checks if left operand is greater than right.
<Checks if left operand is less than right.
>=Checks if left operand is greater than or equal to right.
<=Checks if left operand is less than or equal to right.

Logical Operators

OperatorDescription
&&Logical AND – true if both operands are true.
||Logical OR – true if at least one operand is true.
!Logical NOT – inverts a Boolean value.

To illustrate operator usage, consider the following sample program:

Mastering C# Variables & Operators: Practical Examples & Explanations

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int val1 = 10, val2 = 20;
            bool status = true;

            Console.WriteLine(val1 + val2);      // Arithmetic: 30
            Console.WriteLine(val1 < val2);      // Relational: True
            Console.WriteLine(!status);          // Logical NOT: False
            Console.ReadKey();
        }
    }
}

Explanation

  1. Two int variables (val1 and val2) and one bool variable (status) are declared.
  2. The addition operator (+) computes the sum of val1 and val2.
  3. The less‑than operator (<) compares val1 and val2.
  4. The logical NOT operator (!) inverts the status value from true to false.

Executing the program yields:

Mastering C# Variables & Operators: Practical Examples & Explanations

This concise demonstration covers basic arithmetic, relational, and logical operations, laying a solid foundation for more advanced C# programming.

For deeper insight, refer to the official Microsoft Docs on C# Operators.

C Language

  1. VHDL Variables Explained: Practical Examples & Rules for Reliable Design
  2. C Variables, Constants, and Literals: A Complete Guide
  3. Understanding C Storage Classes: Scope, Lifetime, and Performance
  4. C++ Operators Explained: Types, Examples, and Sample Programs
  5. C++ Structs Explained with a Practical Example
  6. Mastering std::list in C++: Syntax, Functions & Practical Examples
  7. C# Enumerations (Enums) – Definition, Example, and Usage
  8. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  9. Java Variables and Data Types – A Comprehensive Guide with Examples
  10. Understanding Variables in C: Types, Naming Rules, and Memory Management