Data Types & Keywords

Concept of data types, keywords, and escape sequences in C#.

C# Data Types

Data types define the type of data that a variable can store. The language supports various built-in data types, each with its own set of values and operations that can be performed on those values. Here are some of the common data types in C#:

Data Types Size (in bits) Range Format Specifier
int 32 -2,147,483,648 to 2,147,483,647 {0}
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 {0}
float 32 ±1.5 x 10^−45 to ±3.4 x 10^38 {0:F}
double 64 ±5.0 x 10^−324 to ±1.7 x 10^308 {0:F}
decimal 128 ±1.0 x 10^-28 to ±7.9 x 10^28 {0:F}
char 16 Unicode characters {0}
bool 8 (1 byte) true or false {0}
string Sequence of characters {0}
Additional data type information:
DateTime 64 January 1, 0001, through December 31, 9999 AD {0:yyyy-MM-dd HH:mm:ss}
TimeSpan 64 -106751.99:02:48.4775808 to 106751.99:02:48.4775807 {0}
byte 8 (1 byte) 0 to 255 {0}
short 16 -32,768 to 32,767 {0}
ushort 16 0 to 65,535 {0}
sbyte 8 (1 byte) -128 to 127 {0}
uint 32 0 to 4,294,967,295 {0}
ulong 64 0 to 18,446,744,073,709,551,615 {0}
float 32 ±1.5 x 10^−45 to ±3.4 x 10^38 {0:F}
double 64 ±5.0 x 10^−324 to ±1.7 x 10^308 {0:F}
decimal 128 ±±1.0 x 10^-28 to ±7.9 x 10^28 {0:F}

This table includes the size of each C# data type in bits, the typical range of values they can represent, and the format specifiers commonly used with them. Format specifiers are placeholders used in string formatting to control the representation of values when outputting to the console or other output streams.

C# Keywords

A keyword is a reserved word that has a specific meaning and cannot be used as an identifier (such as the name of a variable, method, or class) in your code. These keywords are an integral part of the language's syntax and are used to define the structure and behavior of programs. Some common C# keywords include:

Types Names
Access Modifiers: public
private
protected
internal
protected internal
Modifiers: static
readonly
const
abstract
sealed
Control Flow: if
else
switch
case
default
while
do
for
foreach
break
continue
return
Exception Handling: try
catch
finally
throw
Class & Object Keywords: class
struct
interface
enum
object
new
Others Keywords: namespace
using
this
base
virtual
override
delegate
event
params
as
is

These are some of the essential data types and keywords in C#. There are additional keywords and data types available for more specialized scenarios and functionalities.

Escape Sequences

Escape sequences in C# are special sequences of characters that are used to represent certain non-printable characters or to achieve specific formatting in strings. These sequences are prefixed with a backslash ('\') and are followed by a character or a combination of characters. Escape sequences are often used in strings to include characters that would otherwise be difficult to represent directly.

Escape Sequence Description
\n Newline
\r Carriage return
\t Tab
\\ Backslash
\' Single quote
\" Double quote
\a Alert (bell)
\b Backspace
\f Form feed
\v Vertical tab
\0 Null character
\uXXXX Unicode character (hex)
\xXX Unicode character (hex)

These escape sequences are used within string literals to represent special characters or control characters in C# strings.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.