CSharp Language Pocket Reference.pdf
(
322 KB
)
Pobierz
file://H:\WINNT\Temp\~hhB947.htm
C# Language Pocket Reference
C# is a programming language from Microsoft that is designed specifically to target the .NET Framework.
Microsoft's .NET Framework is a runtime environment and class library that dramatically simplifies the development of
modern, component-based applications.
Microsoft has shown an unprecedented degree of openness in C# and the .NET Framework. The key specifications for
the C# language and the .NET platform have been published, reviewed, and ratified by an international standards
organization called the European Computer Manufacturers Association (ECMA). This standardization effort has led to a
Shared Source release of the specification called the Shared Source CLI (
http://msdn.microsoft.com/net/sscli/
), as well as
to Open Source implementations of .NET called DotGNU Portable .NET (
http://www.dotgnu.org
) and Mono
(
http://www.go-mono.com
). All three implementations include support for C#.
This book is a quick-reference manual to the C# language as of version 1.0 of the .NET Framework. It lists a concise
description of language syntax and provides a guide to other areas of the .NET Framework that are of interest to C#
programmers.
The purpose of this quick reference is to aid readers who need to look up some basic detail of C# syntax or usage. It is
not intended to be a tutorial or user guide, and at least a basic familiarity with C# is assumed. If you'd like more in-depth
information or a more detailed reference, please see
Programming C#
by Jesse Liberty and
C# in a Nutshell
by Drayton,
Albahari, and Neward (both O'Reilly, 2002).
1.1 Identifiers and Keywords
Identifiers are names programmers choose for their types, methods, variables, etc. An identifier must be a whole word
that is essentially made up of Unicode characters starting with a letter or an underscore, and it may not clash with a
keyword. As a special case, the
@
prefix may be used to avoid a clash with a keyword, but is not considered part of the
identifier. For instance, the following two identifiers are equivalent:
C# identifiers are case-sensitive; however, for compatibility with other languages, you should not differentiate public or
protected identifiers by case alone.
Here is a list of C# keywords:
abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
virtual
void
while
C# Language Pocket Reference
Pagina 1 di 87
1.2 Fundamental Elements
A C# program is best understood in terms of three basic elements:
Functions
Perform an action by executing a series of statements. For example, you may have a function that
returns the distance between two points or a function that calculates the average of an array of
values. A function is a way of manipulating data.
Data
Values that functions operate on. For example, you may have data holding the coordinates of a
point or data holding an array of values. Data always has a particular type.
Types
A set of data members and function members. The function members are used to manipulate the
data members. The most common types are classes and structs, which provide a template for
creating data. Data is always an instance of a type.
1.3 Value and Reference Types
All C# types fall into the following categories:
•
Value types (struct, enum)
•
Reference types (class, array, delegate, interface)
The fundamental difference between the two main categories is how they are handled in memory. The following sections
explain the essential differences between value types and reference types.
1.3.1 Value Types
Value types directly contain data, such as the
int
type (which holds an integer) or the
bool
type (which holds a
true
or
false
value). The key characteristic of a value type is a copy made of the value that is assigned to another
value. For example:
using System;
class Test {
static void Main ( ) {
int x = 3;
int y = x; // assign x to y, y is now a copy of x
x++; // increment x to 4
Console.WriteLine (y); // prints 3
C# Language Pocket Reference
Pagina 2 di 87
}
}
1.3.2 Reference Types
Reference types are a little more complex. A reference type defines two separate entities: an object and a reference to
that object. This example follows the same pattern as our previous example, except that the variable
y
is updated here,
while
y
remained unchanged earlier:
using System;
using System.Text;
class Test {
static void Main ( ) {
StringBuilder x = new StringBuilder ("hello");
StringBuilder y = x;
x.Append (" there");
Console.WriteLine (y); // prints "hello there"
}
}
This is because the
StringBuilder
type is a reference type, while the
int
type is a value type. When we declared
the
StringBuilder
variable, we were actually doing two different things, which can be separated into these two
lines:
StringBuilder x;
x = new StringBuilder ("hello");
The first line creates a new variable that can hold a reference to a
StringBuilder
object. The second line assigns a
new
StringBuilder
object to the variable. Let's look at the next line:
StringBuilder y = x;
When we assign
x
to
y
, we are saying "make
y
point to the same thing that
x
points to." A reference stores the address
of an object. (An address is a memory location, stored as a 4-byte number.) We're actually still making a copy of
x
, but
we're copying this 4-byte number as opposed to the
StringBuilder
object itself.
Let's look at this line:
x.Append (" there");
This line actually does two things. It first finds the memory location represented by
x
, and then it tells the
StringBuilder
object that lies at that memory location to append "
there
" to it. We could achieve exactly the
same effect by appending "
there
" to
y
, because
x
and
y
refer to the same object:
y.Append (" there");
A reference may point to no object by assigning the reference to
null
. In this code sample, we assign
null
to
x
, but
C# Language Pocket Reference
Pagina 3 di 87
we can still access the same
StringBuilder
object we created via
y
:
using System;
using System.Text;
class Test {
static void Main ( ) {
StringBuilder x;
x = new StringBuilder ("hello");
StringBuilder y = x;
x = null;
y.Append (" there");
Console.WriteLine (y); // prints "hello there"
}
}
1.3.2.1 Value and reference types side-by-side
A good way to understand the difference between value and reference types is to see them side-by-side. In C#, you can
define your own reference types or your own value types. If you want to define a simple type such as a number, it makes
sense to define a value type, in which efficiency and copy-by-value semantics are desirable. Otherwise, you should
define a reference type. You can define a new value type by declaring a struct, and define a new reference type by
defining a class.
To create a value-type or reference-type instance, the constructor for the type may be called using the
new
keyword. A
value-type constructor simply initializes an object. A reference-type constructor creates a new object on the heap and
then initializes the object:
// Reference-type declaration
class PointR {
public int x, y;
}
// Value-type declaration
struct PointV {
public int x, y;
}
class Test {
static void Main( ) {
PointR a; // reference type
C# Language Pocket Reference
Pagina 4 di 87
a = new PointR( );
PointV b; // value type
b = new PointV( );
a.x = 7;
b.x = 7;
}
}
At the end of the method, the local variables
a
and
b
go out of scope, but the new instance of a
PointR
remains in
memory until the garbage collector determines it is no longer referenced.
Assignment to a reference type copies an object reference, while assignment to a value type copies an object value:
...
PointR c = a;
PointV d = b;
c.x = 9;
d.x = 9;
Console.WriteLine(a.x); // Prints 9
Console.WriteLine(b.x); // Prints 7
}
}
As shown in this example, an object on the heap can be pointed to by multiple variables, whereas an object on the stack
or inline can only be accessed via the variable with which it was declared.
Inline
means that the variable is part of a
larger object; i.e., it exists as a data member or an array member.
1.3.2.2 Boxing and unboxing value types
So that common operations can be performed on both reference and value types, each value type has a corresponding
hidden reference type. This is created when it is assigned to an instance of
System.Object
or to an interface. This
process is called
boxing
. A value type may be cast to the "object" class (the ultimate base class for all value types and
reference types) or to an interface it implements.
In this example, we box and unbox an
int
value type to and from its corresponding reference type:
class Test {
static void Main ( ) {
int x = 9;
C# Language Pocket Reference
Pagina 5 di 87
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin