Integral division - CSharp Language Basics

CSharp examples for Language Basics:Operator

Introduction

Division operations on integral types always truncate remainders.

Dividing by a variable whose value is zero generates a runtime error (a DivideByZeroException):

Dividing by the literal or constant 0 generates a compile-time error.

Demo Code

using System;/* www .  j av a2s.com*/
class Test
{
   static void Main(){
      int a = 2 / 3;      // 0
      Console.WriteLine (a);
      int b = 0;
      int c = 5 / b;      // throws DivideByZeroException
      Console.WriteLine (c);
   }
}

Result


Related Tutorials