TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Uint Example

This page shows the C# uint type, an unsigned integer. It provides an example program.

Uint is an unsigned integer.

It is similar to int, but does not reserve space for the sign. Unsigned integers can be used in the place of signed integers in many programs. They help when you need to increase the bounds of a loop.

Int

System.UInt32 information

uint.MinValue = 0
uint.MaxValue = 4294967295

Example. The uint type is the same as the System.UInt32 type in the System namespace. This alias provides for clearer code in programs. You can use UInt32 if you include the System namespace or use the fully qualified name "System.UInt32".

Namespace

Note: This example shows some differences between uints and ints, such as the MinValue and MaxValue.

C# program that uses uint type

using System;

class Program
{
    static void Main()
    {
	// Declare example unsigned and signed integers.
	uint value1 = 100;
	int value2 = 100;

	// Display values.
	Console.WriteLine(value1);
	Console.WriteLine(value2);

	// Assign the maximum values.
	uint max1 = uint.MaxValue;
	int max2 = int.MaxValue;

	// Display maximum values.
	Console.WriteLine(max1);
	Console.WriteLine(max2);

	// Assign the minimum values.
	uint min1 = uint.MinValue;
	int min2 = int.MinValue;

	// Write the minimum values.
	Console.WriteLine(min1);
	Console.WriteLine(min2);

	// Write the types.
	Console.WriteLine(typeof(uint));
	Console.WriteLine(typeof(int));
    }
}

Output

100
100
4294967295
2147483647
0
-2147483648
System.UInt32
System.Int32

In this example, the uint is initialized in the same way as an int type. When your code that uses uint is executed in the runtime, it is allocated in the method's stack. No dynamic heap allocation is done. This is very efficient.

Note: We must always assign uints that are local variables, so the compiler can prove no uninitialized memory on the stack is used.

Tip: This process is called definite assignment analysis. This helps improve program quality.

Definite Assignment

MaxValue on the System.UInt32 type in the base class library is equal to 4294967295. This is invariant and will always be true. There is no advantage to hard-coding 4294967295—the representation will be no different.

MinValue on the System.UInt32 type is also a constant in the base class library. This field is always equal to zero. This is true because uints lack the sign bit that, when set, makes a number equal a negative value.

Const

The final part of the program displays the System.Type object for the two numeric types. The typeof keyword is not actually a function but is considered an operator. All types in the language inherit from the object type.

And: The object type contains a type pointer that indicates its kind. The typeof operator accesses this pointer.

TypeObject

Errors. There exist some errors regarding the int and uint types. If you try to assign an int variable in your program to a numeric literal that is too big, the C# compiler will give you a compile-time error.

So: For example, try compiling the code "int value1 = 3000000000". The C# compiler will give the error noted below.

Tip: If you change the "int" type to "uint", your program will compile successfully.

Error: Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?)

Summary. The unsigned integer type is accessed with the uint keyword or the System.UInt32 type. We saw how uint values are used and what their maximum and minimum values are, and how you can access those values.

Finally: We noted the object model in the C# language and discussed a common error relating to the uint type.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf