PART I
C h a p t e r 8 :
A C l o s e r L o o k a t M e t h o d s a n d C l a s s e s
211
PART IPART I
Static Classes
Beginning with C# 2.0, you can declare a class
static
. There are two key features of a
static
class. First, no object of a
static
class can be created. Second, a
static
class must contain only
static
members. A
static
class is created by modifying a class declaration with the keyword
static
, shown here.
static class
class
-
name
{ // ...
Within the cla ss, all members must be explicitly specified as
static
. Making a class
static
does not automatically make its members
static
.
static
classes have two primary uses. First, a
static
class is required when creating
an
extension method,
which is a new feature added by C# 3.0. Extension methods relate
mostly to LINQ, and a discussion of extensions methods is found in Chapter 19. Second,
a
static
class is used to contain a collection of related
static
methods. This second use is
demonstrated here.
The following example uses a
Do'stlaringiz bilan baham: |