Advertisement

Responsive Advertisement

Lesson:3 C# Operators I . Operators Overview, II- Arithmetic Operators

 

I . Operators Overview

C# provides a number of operators. Many of them are supported by the built-in types and allow you to perform basic operations with values of those types. Those operators include the following groups:

- Arithmetic operators that perform arithmetic operations with numeric operands

- Comparison operators that compare numeric operands

- Boolean logical operators that perform logical operations with bool operands

- Bitwise and shift operators that perform bitwise or shift operations with operands of the integral types

- Equality operators that check if their operands are equal or not


Typically, you can overload those operators, that is, specify the operator behavior for the operands of a user-defined type.

The simplest C# expressions are literals (for example, integer and real numbers) and names of variables. You can combine them into complex expressions by using operators. Operator precedence and associativity determine the order in which the operations in an expression are performed. You can use parentheses to change the order of evaluation imposed by operator precedence and associativity.



II- Arithmetic Operators

In c#, Arithmetic Operators are useful to perform basic arithmetic calculations like addition, subtraction, division, etc., based on our requirements.

For example, we have integer variables x = 20, y = 10, and if we apply an arithmetic operator + (x + y) to perform an addition operator. We will get the result as 30 like as shown below.

int result;

int x = 20, y = 10;

result = (x + y);


The following operators perform arithmetic operations with operands of numeric types:


Unary ++ (increment), -- (decrement), + (plus), and - (minus) operators

Binary * (multiplication), / (division), % (remainder), + (addition), and - (subtraction) operators

Those operators are supported by all integral and floating-point numeric types.


In the case of integral types, those operators (except the ++ and -- operators) are defined for the int, uint, long, and ulong types. When operands are of other integral types (sbyte, byte, short, ushort, or char), their values are converted to the int type, which is also the result type of an operation. When operands are of different integral or floating-point types, their values are converted to the closest containing type, if such a type exists. For more information, see the Numeric promotions section of the C# language specification. The ++ and -- operators are defined for all integral and floating-point numeric types and the char type. The result type of a compound assignment expression is the type of the left-hand operand.

II.1- The Basic Arithmetic Operators



Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Arithmetic_Operators
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //User Define Function
        private bool IsNumeric(string s)
        {
            try
            {
                double.Parse(s);
            }
            catch
            {
                return false;
            }
            return true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            double x = 0;
            double y = 0;
            if (IsNumeric(txtNumber1.Text))
            {
                x = double.Parse(txtNumber1.Text);
            }
            if (IsNumeric(txtNumber2.Text))
            {
                y = double.Parse(txtNumber2.Text);
            }
            txtResult_Add.Text = (x + y).ToString();
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            double x = 0;
            double y = 0;
            if (IsNumeric(txtNumber1.Text))
            {
                x = double.Parse(txtNumber1.Text);
            }
            if (IsNumeric(txtNumber2.Text))
            {
                y = double.Parse(txtNumber2.Text);
            }
            txtResult_Sub.Text = (x - y).ToString();
        }

        private void btnMult_Click(object sender, EventArgs e)
        {
            double x = 0;
            double y = 0;
            if (IsNumeric(txtNumber1.Text))
            {
                x = double.Parse(txtNumber1.Text);
            }
            if (IsNumeric(txtNumber2.Text))
            {
                y = double.Parse(txtNumber2.Text);
            }
            txtResult_Mult.Text = (x * y).ToString();
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            double x = 0;
            double y = 0;
            if (IsNumeric(txtNumber1.Text))
            {
                x = double.Parse(txtNumber1.Text);
            }
            if (IsNumeric(txtNumber2.Text))
            {
                y = double.Parse(txtNumber2.Text);
            }
            txtResult_Div.Text = (x / y).ToString();
        }
    }
}

Result:




Post a Comment

0 Comments