Basic Operations and Syntax

Your Progress in this Chapter

0%

0 / 12 units completed

Chapter Overview

Primitive Types

Section 1.1 of 623 min11 code examples

Primitive Types in C#

In C#, primitive types are the basic data types provided by the language. The primitive (built-in) types in C# include bool, byte, sbyte, short, ushort, int, uint, long, ulong, char, float, double, decimal, nint, nuint, object, string, and dynamic

C# Keyword.NET TypeCategoryDescription
boolSystem.BooleanValue typeTrue/false values
byteSystem.ByteValue type8-bit unsigned integer (0–255)
sbyteSystem.SByteValue type8-bit signed integer (−128–127)
shortSystem.Int16Value type16-bit signed integer
ushortSystem.UInt16Value type16-bit unsigned integer
intSystem.Int32Value type32-bit signed integer
uintSystem.UInt32Value type32-bit unsigned integer
longSystem.Int64Value type64-bit signed integer
ulongSystem.UInt64Value type64-bit unsigned integer
nintSystem.IntPtrValue typeNative-sized signed integer
nuintSystem.UIntPtrValue typeNative-sized unsigned integer
charSystem.CharValue typeSingle 16-bit Unicode character
floatSystem.SingleValue type32-bit floating-point number
doubleSystem.DoubleValue type64-bit floating-point number
decimalSystem.DecimalValue type128-bit precise decimal (financial)
objectSystem.ObjectReference typeBase type for all objects
stringSystem.StringReference typeSequence of characters
dynamicSystem.ObjectReference typeType resolved at runtime

Example 1C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Code is ready to run

Example 2C#

1
2
3
4
Code is ready to run

Example 3C#

1
2
Code is ready to run

Example 4C#

1
2
3
Code is ready to run

Example 5C#

1
2
Code is ready to run

Example 6C#

1
2
Code is ready to run

These primitive types are the building blocks for more complex data structures and are essential for various operations in C#.

Examples

.. Admonition:: Example 1 : Integer Arithmetic

Example 7C#

1
2
3
4
Code is ready to run
OutputFrom the book
Sum = 30

.. Admonition:: Example 2 : Floating Point Calculation

Example 8C#

1
2
3
Code is ready to run
OutputFrom the book
Area = 78.53981633974483

.. Admonition:: Example 3 : Character and ASCII

Example 9C#

1
2
3
Code is ready to run
OutputFrom the book
Character: A, ASCII: 65

.. Admonition:: Example 4 : Boolean Logic

Example 10C#

1
2
3
4
Code is ready to run
OutputFrom the book
Go out? False

.. Admonition:: Example 5 : Strings

Example 11C#

1
2
3
4
Code is ready to run
OutputFrom the book
Full Name: Lateef K.

Inbuilt Derived Types in C#

In C#, beyond the primitive types (like int, bool, char), there are several inbuilt derived types that the language provides out of the box. These are types that are not primitives themselves but are built into the framework and derive from other base classes (usually System.Object).

TypeBase ClassCategoryDescription
stringSystem.ObjectReference typeRepresents a sequence of characters (immutable)
objectRoot of all typesReference typeBase type for all classes in C#.
dynamicSystem.ObjectReference typeType resolved at runtime.
ArraySystem.ObjectReference typeBase class for all arrays (e.g., int[], string[]).
DelegateSystem.ObjectReference typeBase class for all delegates (function pointers).
MulticastDelegateDelegateReference typeSupports invocation of multiple methods.
EnumSystem.ValueTypeValue typeBase class for all enumerations.
ValueTypeSystem.ObjectValue typeBase class for all structs.
Nullable <T>System.ValueTypeValue typeAllows value types to be null (e.g., int?).