Lesson 2.8

uint data type C# - programming example


Objective

What is uint data type in c#?
How to declare and initialize
Programming examples
Conversion and Casting

What is uint Data Types in C#?

"Uint is unsigned 32-bit integer type data type which is derived from System.UInt32 Class. It stores value between 0 to 4,294,967,295. It is used for storing large number of positive value."

TypeRangeSize.Net Framework type
uint0 to 4,294,967,295Unsigned 32-bit integerSystem.UInt32

Declaration and Initialization

You can declare uint data type as follow:

		  
  uint num1=35; //or 
  uint num1=35U; //you can suffix U in uint value.

		  

Note:if the value of uint variable exceed from its range a compilation error occurs:

Constant value 'xxx' cannot be converted to a 'uint'

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace uint_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            uint num1, num2, result;
            num1 = 15;
            num2 = 20;
            result = num1 * num2;
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

		  

Output

300
_

Conversion

If you are doing arithmetic operation using uint variable; it is not necessary to convert right hand side value into uint data type. for example:

Correct Method
  result = num1 * num2; //Correct: Implicit Conversion
		  

Correct Method
  result = (uint)(num1 * num2); //Correct: Explicit Conversion
		  

Implicit Conversion - No need to convert when left hand side data type is: 
  long, float, double or decimal
  
  Example:
  
  uint a=5, b=6;
  long result=a+b;

Summary

uint is unsigned 32-bit integer type data type which stores value between 0 to 4,294,967,295. It is mostly used for storing numeric value. In the next chapter we will discuss on Long data type.

BackNext