|
|
Contents
Overview 1
Common Type System 2
Naming Variables 9
Using Built-in Data Types 15
Compound Assignment 18
Increment and Decrement 20
Creating User-Defined Data Types 24
Converting Data Types 28
Lab 3: Creating and Using Types 32
Review 36
Module 3: Using Value-
Type Variables
This course is based on the prerelease Beta 1 version of Microsoft? Visual Studio .NET.
Content in the final release of the course may be different from the content included in this
prerelease version. All labs in the course are to be completed with the Beta 1 version of
Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
ó 2001 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, Visual
Basic, Visual C++, Visual #, Visual Studio, Windows, and Windows Media are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Module 3: Using Value-Type Variables 1
Overview
n Common Type System
n Naming Variables
n Using Built-in Data Types
n Creating User-Defined Data Types
n Converting Data Types
All applications manipulate data in some way. As a C# developer, you need to
understand how to store and process data in your applications. Whenever your
application needs to store data temporarily for use during execution, you store
that data in a variable. Before you use a variable, you must define it. When you
define a variable, you reserve some storage for that variable by identifying its
data type and giving it a name. After a variable is defined, you can assign
values to that variable.
In this module, you will learn how to use value-type variables in C#. You will
learn how to specify the type of data that variables will hold, how to name
variables according to standard naming conventions, and how to assign values
to variables. You also will learn how to convert existing variables from one data
type to another and how to create your own variables.
After completing this module, you will be able to:
n Describe the types of variables that you can use in C# applications.
n Name your variables according to standard C# naming conventions.
n Declare a variable by using built-in data types.
n Assign values to variables.
n Convert existing variables from one data type to another.
n Create and use your own data types.
2 Module 3: Using Value-Type Variables
u Common Type System
n Overview of CTS
n Comparing Value and Reference Types
n Determining Base Types
n Comparing Built-in and User-Defined Value Types
n Simple Types
Every variable has a data type that determines what values can be stored in the
variable. C# is a type-safe language, meaning that the C# compiler guarantees
that values stored in variables are always of the appropriate type.
The Common Language Runtime includes a Common Type System (CTS) that
defines a set of built-in data types that you can use to define your variables. In
this section, you will learn how the CTS works so that you can choose the
appropriate data types for your variables. You also will see examples of valuetype
variables, including simple data types.
Module 3: Using Value-Type Variables 3
Overview of CTS
n CTS Supports Object-Oriented and Procedural
Languages
n CTS Supports Both Value and Reference Types
Reeffeerreennccee TTyyppee
TTyyppee
VVaalluuee TTyyppee
When you define a variable, you need to choose the right data type for your
variable. The data type determines the allowable values for that variable, which,
in turn, determine the operations that can be performed on that variable.
CTS
CTS is an integral part of the Common Language Runtime. The compilers,
tools, and the runtime itself share CTS. It is the model that defines the rules that
the runtime follows when declaring, using, and managing types. CTS
establishes a framework that enables cross-language integration, type safety,
and high-performance code execution.
C# defines several categories of variables. In this module, you will learn about
two kinds:
n Value-type variables
n Reference-type variables
4 Module 3: Using Value-Type Variables
Comparing Value and Reference Types
n Value Types:
n Directly contain their
data
n Each has its own
copy of data
n Operations on one
cannot affect another
n Reference Types:
n Store references to their
data (known as objects)
n Two reference variables
can reference same object
n Operations on one can
affect another
Value Types
Value-type variables directly contain their data. Each value-type variable has its
own copy of the data, so it is not possible for operations on one variable to
affect another variable.
Reference Types
Reference-type variables contain references to their data. The data for
reference-type variables is stored in an instance. It is possible for two referencetype
variables to reference the same object, so it is possible for operations on
one reference variable to affect the object referenced by another reference
variable.
For more information about reference types, see Module 8, “Using Reference-
Type Variables,” in Course 2124A, Introduction to C# Programming for the
Microsoft .NET Platform (Prerelease). |
|