0% found this document useful (0 votes)
3 views

Data Types in C sharp

The document provides an overview of data types in C#, categorizing them into value types, reference types, and pointer types. It details various value types, including integer, floating-point, boolean, character, and decimal types, along with their sizes, ranges, and default values. Additionally, it explains reference types such as object and string, as well as implicitly typed variables and dynamic types.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Types in C sharp

The document provides an overview of data types in C#, categorizing them into value types, reference types, and pointer types. It details various value types, including integer, floating-point, boolean, character, and decimal types, along with their sizes, ranges, and default values. Additionally, it explains reference types such as object and string, as well as implicitly typed variables and dynamic types.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Types in C#

1. Introduction to Data Types in C#


Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed
programming language because in C# each type of data (such as integer, character, float, and so
forth) is predefined as part of the programming language and all constants or variables defined
for a given program must be described with one of the data types.

• Value Types
• Reference Types
• Pointer Types (Advanced, used in unsafe code)

2. Value Types
Value types directly contain their data in memory and are stored on the stack. When assigned to
another variable, a copy of the value is made. In C#, the Value Data Types will directly store the
variable value in memory, and it will also accept both signed and unsigned literals.

2.1 Integer Types

There are 8 integral types which provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in
signed or unsigned form.

Type Size Range Default Value


byte 8-bit 0 to 255 0
sbyte 8-bit -128 to 127 0
short 16-bit -32,768 to 32,767 0
ushort 16-bit 0 to 65,535 0
int 32-bit -2,147,483,648 to 2,147,483,647 0
uint 32-bit 0 to 4,294,967,295 0
long 64-bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
ulong 64-bit 0 to 18,446,744,073,709,551,615 0UL

2.2 Floating-Point Types

Float: It is 32-bit single-precision floating point type. It has 7 digit Precision. To initialize a float
variable, use the suffix f or F. Like, float x = 3.5F;. If the suffix F or f will not use then it is
treated as double.
Double:It is 64-bit double-precision floating point type. It has 14 – 15 digit Precision. To
initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix because by
default floating data types are the double type.

Type Size Precision Range Default String


Value Literal
float 32-bit 7 digits ±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸ 0.0f "3.4f"
double 64-bit 15-16 ±5.0 × 10⁻³²⁴ to ±1.7 × 0.0d "1.7d"
digits 10³⁰⁸
decimal 128- 28-29 ±1.0 × 10⁻²⁸ to ±7.9 × 10²⁸ 0.0m "7.9m"
bit digits

2.3 Boolean Type

It has to be assigned either true or false value. Values of type bool are not converted
implicitly or explicitly (with casts) to any other type. But the programmer can easily
write conversion code.

• bool (1-bit): Holds true or false. Default value is false.


• bool isActive = true;

2.4 Character Type

• char (16-bit Unicode): Stores a single character. Default value is \0 (null character).
• char letter = 'A';

2.5 Decimal Types

The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has
28-29 digit Precision. To initialize a decimal variable, use the suffix m or M.
Like as, decimal x = 300.5m;. If the suffix m or M will not use then it is treated as double.

3. Reference Types
Reference type store memory addresses of objects on the heap. When assigned to another variable,
only the reference is copied. The Reference Data Types will contain a memory address of variable
value because the reference types won’t store the variable value directly in memory. When you
create a reference type variable, such as an object or a string, you are storing a reference (or pointer)
to the location in memory where the data is held. The actual data for reference types is stored on
the heap. The heap is a large pool of memory used for dynamic memory allocation. The built-in
reference types are string and object.
3.1 Object (object)

In C#, all types, predefined and user-defined, reference types and value types, inherit
directly or indirectly from Object. So basically it is the base class for all the data types
in C#. Before assigning values, it needs type conversion. When a variable of a value
type is converted to object, it’s called boxing. When a variable of type object is
converted to a value type, it’s called unboxing. Its type name is System.Object.

• Base type of all data types in C#.


• Can store any type of data. Default value is null.
• object obj = 42;
• obj = "Hello"; // Allowed

3.2 String (string)

• Represents a sequence of characters.


• Immutable (once created, cannot be modified). Default value is null.
• All of the String methods and C# operators that appear to modify a string actually return
the results in a new string object.
• string name = "C# Programming";

3.3 Arrays

• A collection of elements of the same type. Default value is null.


• int[] numbers = { 1, 2, 3, 4, 5 };

5. Implicitly Typed Variables (var)


The compiler infers the data type at compile time. Not a datatype but is used to define variables.

var age = 25; // Inferred as int

6. Dynamic Type (dynamic)


Allows runtime type determination.

dynamic data = 100;


data = "C#"; // No compile-time error

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy