Home > Articles > Programming > C#

Like this article? We recommend

Like this article? We recommend

Basic C# Types

A type is the organization and format of information. For instance, an integer type is a 32-bit number. C# language elements such as classes and structs permit creation of custom types. Some languages are weakly typed, and there are times when the interpretation of types in these weakly typed languages will cause program errors.

C# is a strongly typed language. This essentially means that the compiler and runtime system does a good job of verifying the type consistency of expressions. All variables have a type. The type produced by an expression always either is defined by the C# language or is a user-defined type. C# provides a mechanism for converting one type to another.

While discussing each type, this section also shows how to declare a literal of each type. Literals are values that can't be changed. They can't be referenced, either. They occupy the memory space where they're used. While it's possible to copy the value of a literal into a variable and then change the variable, this does not change the value of the original literal.

Variable Declarations

Variables are programming elements that can change during program execution. They're used as storage to hold information at any stage of computation. As a program executes, certain variables will change to support the goals of an algorithm. Every variable has a type, and this section will show how to specify a variable's type. The syntax of a variable definition always conforms to the following pattern:

 Type Identifier [Initializer];

In this example, Type is a placeholder, representing one of the types listed in this section or a user-defined type. Every variable must have a Type part in its declaration. Similarly, every variable declaration must have an identifier or name. Declarations may optionally include an initializer to set the value of a variable when it is created. The type of the value used to initialize a variable must be compatible with the type that the variable is declared as.

NOTE

C# variables may be declared and initialized in the same statement. Examples in this chapter show initializations to provide a clearer understanding of how the type can be used. It would be correct to assume that when I use the term "declaration," both a declaration and an initialization could be happening at the same time.

The Simple Types

The simple types consist of Boolean and numeric types. The numeric types are further subdivided into integral and floating-Point types.

The Boolean Type

There's only a single Boolean type named bool. A bool can have a value of either true or false. The values true and false are also the only literal values that you can use for a bool. Here's an example of a bool declaration:

bool isProfitable = true;

NOTE

The bool type will not accept integer values such as 0, 1, or -1. The keywords true and false are built into the C# language and are the only allowable values.

For C++ Programmers

There is no casting conversion between int and bool types. To accomplish a similar affect, a bool result can be obtained with the equality operator. That is, (x == 0) would evaluate to true when the integer type x is equal to 0.

The Integral Types

The integral types are further subdivided into eight types plus a character type: sbyte, byte, short, ushort, int, uint, long, ulong, and char. All of the integral types except char have signed and unsigned forms. All integral type literals can be expressed in hexadecimal notation by prefixing 0x to a series of hexadecimal numbers 0 through F. The exception is the char.

A char holds a single Unicode character. Some examples of char variable declarations include:

char middleInitial;    \\ uninitialized
char yesNo = 'Y';
char studentGrade = '\u005A';  \\ Unicode 'Z'
char studentGrade = '\x0041';  \\ Unicode 'A'

As shown previously, Unicode escape character notation requires four hexadecimal digits, prefixed by \u or \U. The digits are left padded with zeros to make the digit part four characters wide. A char may also be specified in hexadecimal notation by prefixing \x to between 1 and 4 hexadecimal digits.

For C++ Programmers

Notice the difference between the size of the C# char (16 bits) and the normal C++ char (8 bits). The C# char is similar to the C++ wchar_t (16 bits).

There are also special escape sequences representing characters. They're used for alert, special formatting, and building strings to avoid ambiguity. The following list shows the valid C# escape sequences:

\'  Single Quote
\"  Double Quote
\\  Backslash
\0  Null
\a  Bell
\b  Backspace
\f  Form Feed
\n  Newline (linefeed)
\r  Carriage Return
\t  Horizontal Tab
\v  Vertical Tab

A byte is an unsigned type that can hold 8 bits of data. Their range is from 0 to 255. An sbyte is a signed byte with a range of –128 to 127. This is how you declare byte variables:

byte age = 25;
sbyte normalizedTolerance = -1;

The short type is signed and holds 16 bits. It can hold a range from –32,768 to 32,767. The unsigned short, ushort, holds a range of 0 to 65,535. Here are a couple examples:

ushort numberOfJellyBeans = 62873;
short temperatureFarenheit = -36;

The integer type is signed and has a size of 32 bits. The signed type, int, has a range of –2,147,483,648 to 2,147,483,647. The uint is unsigned and has a range of 0 to 4,294,967,295. Unsigned integers may optionally have a u or U suffix. Examples follow:

uint nationalPopulation = 4139276850;  // also 4139276850u or 4139276850U
int tradeDeficit = -2058293762;

A long type is signed and holds 64 bits with a range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. A ulong is unsigned with a range of 0 to 18,446,744,073,709,551,615. Unsigned long literals may have suffixes with the combination of uppercase or lowercase characters UL. Their declarations can be expressed like this:

ulong lightYearsFromEarth = 72038289347236792;
              // also 72038289347236792ul
              //  or 72038289347236792UL
              //  or 72038289347236792uL
              //  or 72038289347236792Lu
              //  or 72038289347236792LU
              //  or 72038289347236792lU
long negativeVariance = -1636409717646593274;   
              // also –1636409717646593274l
              //  or –1636409717646593274L

Each of the types presented to this point have a unique size and range. Table 2.3 provides a summary and quick reference of the size and range of each integral type.

Table 3 The Integral Types

Type

Size(in Bits)

Range

char

16

0 to 65,535

sbyte

8

–128 to 127

byte

8

0 to 255

short

16

–32,768 to 32,767

ushort

16

0 to 65,535

int

32

–2,147,483,648 to 2,147,483,647

uint

32

0 to 4294967295

long

64

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong

64

0 to 18,446,744,073,709,551,615


For C++ Programmers

There is no native equivalent in C++ for the byte. However, there are ways of producing the same effect with typedef'd signed and unsigned chars. Also, a C++ long is 32 bits, whereas a C# long is 64 bits.

For Java Programmers

There are no unsigned types in Java, but there are in C#.

The Floating-Point Types

C# provides two floating-point types—float and double—and a new type called decimal. The floating-point types conform to IEEE 754 specifications.

Floating-point literals may optionally be specified with exponential notation. This allows specification of very large numbers with the least amount of space necessary to write them. The trade-off between exponential and normal notation is size versus precision. The general form of exponential syntax is

N.Ne±P

where N is some decimal digit, e can be uppercase or lowercase, and P is the number of decimal places. The ± indicates either a +, -, or neither, which is the same as +. This is standard scientific notation.

The float type can hold a range of around 1.5 x 10–45 to 3.4 x 1038. It has a seven-digit precision. To designate a floating-point literal as a float, add an F or f suffix. A float literal can be written with or without exponential notation, as follows:

float profits   = 36592.73;  // also 36592.73F
                 //  or 36592.73f
float atomicWeight = 1.54e-15;
float warpSpeed  = 3.21E3;

A double has a range of about 5.0 x 10–324 to 1.7 x 10308, and a precision of 15 to 16 digits. double literals may have the suffix D or d. It, too, may have literals expressed with or without exponential notation:

double vectorMagnitude  = 8.2e127;
double accumulatedVolume = 7982365.83658341;  
            // also 7982365.83658341D
            //  or 7982365.83658341d

A new type not seen in any other language is the decimal type. The decimal type has 28 or 29 digits of precision and can range from 1.0 x 10–28 to about 7.9 x 1028. decimal literals can be specified with an M or m suffix. The trade-off between decimal and double is precision versus range. The decimal is the best choice when precision is required, but choose a double for the greatest range. The decimal type is well suited for financial calculations, as shown in the following example:

decimal annualSales = 99873582948769876589348317.95;

TIP

Use the C# decimal type for greater precision in financial calculations.

Table 4 provides a quick lookup of the floating-point types.

Table 4 The Floating-Point Types

Type

Size(bits)

Precision

Range

float

32

7 digits

1.5 x 10–45 to 3.4 x 1038

double

64

15–16 digits

5.0 x 10–324 to 1.7 x 10308

decimal

128

28–29 decimal places

1.0 x 10–28 to 7.9 x 1028


A final word on literal suffixes: There are common suffixes for each literal type. Suffixes ensure that the literal is the intended type. This is good for documentation. However, the primary benefit is ensuring that your expressions are evaluated correctly; that is, the compiler will interpret float and decimal literals without suffixes as a double when evaluating an expression. To avoid the associated errors, use an appropriate literal suffix.

Struct Types

A struct is a value type. All of the types presented thus far fall into the value type category. Value types are variables that directly hold their data. They are allocated on the stack, which makes them very efficient for storing and retrieving information. Structs are containers that can hold a collection of other items. They provide a method for programmers to create their own value types.

Reference Types

There are four reference types in C#: classes, interfaces, delegates, and arrays. Reference types are library or user-defined types that are allocated on the heap. Being allocated on the heap means that reference types use more system resources. They are managed by a built-in garbage collector, which also manages their lifetimes. Classes may contain many other C# language members. They also define unique types.

Interfaces are used to expose the public attributes and behavior of classes. They have no implementations themselves. Whenever a class specifies an interface, a programmer knows, by the definition of that interface, that the class supports certain attributes and behavior. This way, a number of different classes can implement the same interface and be used in the same basic manner but provide their own unique behavior.

Delegates provide a type-safe way to dynamically reference class methods. When the exact method to implement won't be known until runtime, a delegate can be used to accept a reference to a method. Then whatever method is assigned to the delegate at runtime can be executed by calling the delegate to which the method was assigned. This is type-safe because a method must conform to the type specified in the delegate declaration before it is assigned to a delegate.

Arrays provide a method of storing multiple items of a specific type. Their interface represents a linear collection of data that can be referenced in sequence. Their power extends to providing specialized methods for managing their data. A C# array is a useful method of storing many items of the same type of data in a linear form.

Enumeration Types

The enum is a list of constant values. Elements of an enum are expressed in words rather than numbers, which makes it convenient for understanding the meaning of the value being used. Here's an example:

enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };

For Java Programmers

Java does not have an equivalent to an enum.

By default, the first element of an enum starts with 0, unless specified otherwise. Subsequent elements have a value of 1 greater than their predecessor, unless they also have a designated value. In the following example, Mon has the value 1, Tue is 2, Wed is 3, Thu is 4, and Fri is 5. Then after Sat is changed to 10, Sun becomes 11.

enum Weekday { Mon = 1, Tue, Wed, Thu, Fri, Sat = 10, Sun };

The type of enum elements may be sbyte, byte, short, ushort, int, uint, long, or ulong. Specify this with a colon and type specification after the name. Here's an example:

enum Month: byte
{
  January,
  February,
  March,
  April,
  May,
  June,
  July,
  August,
  September,
  October,
  November,
  December
};

For C++ Programmers

In C#, enums can be specified as sbyte, byte, short, ushort, int, uint, long, or ulong, but in C++ they are int.

String Type

The string type is a C# primary type. Its value is represented by a string of Unicode characters. There are two types of string literals. The first type may be any valid set of characters between two double quotes, including character escape sequences.

string thankYou = "Grazie!\a"; // Grazie! <ding>
string hello  = "Sa-waht dee\tkrahp!"; 
                // Sa-waht dee<tab>krahp! 

string kewl = "Das ist\nzehr\ngut!"; // Das ist
                   // zehr
                   // gut!

For C++ Programmers

A C++ string was originally the same as a normal C string, a pointer to a null-terminated array of characters. With the introduction of Standard C++, a C++ string now refers to the Standard Template Library (STL) string type. A C# string is a built-in type, and its representation is transparent.

The second type is a verbatim string literal. It's made by prefixing a string with an @. The difference between verbatim string literals and normal string literals is that the character escape sequences are not processed, but are interpreted as is. Because the double-quote escape sequence won't work, include two quotes side by side to include a single quote in a string. Verbatim string literals may span multiple lines, if needed. The following examples show various forms of the verbatim string literals.

string whoSaid = @"He said, ""She said.""";
         // He said, "She said."

string beerPlease = @"Een \'Duvel\', alstublieft!";
          // Een \'Duvel\', alstublieft!


string gettysburg = @"Four score and seven years ago
our fathers brought forth upon this continent
a new nation, conceived in liberty
and dedicated to the principle
that all people are considered equal...";

For C++ and Java Programmers

C# includes a special type of string literal called the verbatim string literal. It's useful for avoiding escape sequences in file paths and similar situations where decorations detract from the readability of the string literal.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020