Skip to Main Content
C# in a Nutshell, Second Edition
book

C# in a Nutshell, Second Edition

by Peter Drayton, Ben Albahari, Ted Neward
August 2003
Intermediate to advanced content levelIntermediate to advanced
928 pages
32h 1m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell, Second Edition

Predefined Types

This section explains each of C#’s predefined types:

  • Value types

    —Integer, signed (sbyte, short, int, long)
    —Integer, unsigned (byte, ushort, uint, ulong)
    —Floating-point (float, decimal, double)
  • Reference types

    —Object
    —String

All of these types alias types found in the System namespace. For example, there is only a syntactic difference between these two statements:

int i = 5;
System.Int32 i = 5;

Integral Types

C# type

System type

Size

Signed

sbyte

System.SByte

1 byte

yes

short

System.Int16

2 bytes

yes

int

System.Int32

4 bytes

yes

long

System.Int64

8 bytes

yes

byte

System.Byte

1 byte

no

ushort

System.UInt16

2 bytes

no

uint

System.UInt32

4 bytes

no

ulong

System.UInt64

8 bytes

no

For unsigned integers that are n bits wide, possible values range from 0 to 2n-1. For signed integers that are n bits wide, their possible values range from -2n-1 to 2n-1-1. Integer literals can use either decimal or hexadecimal notation:

int x = 5;
ulong y = 0x1234AF; // prefix with 0x for hexadecimal

When an integral literal is valid for several possible integral types, the default type chosen goes in this order: int, uint, long, and ulong. The following suffixes may be used to explicitly specify the chosen type:

U

uint or ulong

L

long or ulong

U

ulong

Integral conversions

An implicit conversion between integral types is permitted when the type to convert to contains every possible value of the type to convert from. Otherwise an explicit ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# in a Nutshell

C# in a Nutshell

Ben Albahari, Ted Neward, Peter Drayton
C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard

Publisher Resources

ISBN: 0596005261Catalog PageErrata