Sie sind auf Seite 1von 39

.

NET Fundamentals
Strongly Typed Object Oriented
Programming Environment
Type  Class  Object

"In C# a type is defined by a class,


while the individual instances of a
class are known as objects." Jesse
Liberty
Types and Types in .NET
What is a type?
• Every variable has a type
• A type defines the variables general
properties and behaviors
• A type can be complex like a form class or
simple like an integer.
• Sometimes a type is tangible, like a button in
a window
• Sometimes a type abstract, like a data table or
a thread
Types in .NET
Previously, each programming
language represented data types in its
own way. Now, the common type
system provides every language in
Visual Studio .NET with a consistent
set of data types.
In addition, every data type supports a
minimum set of methods. Because all
languages use the same library of types,
you can call one language from another
without having to convert the type or the
call conventions.
The .NET run-time environment is
designed to be safe and secure.
The .NET run-time environment
enforces strict rules to guarantee the
safety of types.
type-safe code
• Type-safe code is code that accesses types only in
well-defined, allowable ways.
• Type-safe code only accesses memory at fixed
offsets corresponding to actual field members.
• Code that accesses memory at arbitrary offsets
outside the range of memory that belongs to that
object's publicly exposed fields, it is not type-safe.
.NET and type-safe code
• The Common Language Specification defines a set of
programmatically verifiable rules.
• These rules govern the interoperation of types that are
authored in different programming languages.
• These rules also establish requirements for Common
Language Specification compliance.
• Visual Studio .NET languages such as Microsoft
Visual Basic .NET and Microsoft Visual C# .NET
comply with the Common Language Specification.
Type Fundamentals

• All objects in .NET ultimately derive from


System.Object
• This means that every object of every type has a
minimum set of methods
The Public Methods
• Equals - Determines whether two objectinstances
are equal.
• GetHashCode - Serves as a hash function for a
particular type, suitable for use in hashing
algorithms and data structures like a hash table.
• ToString - Returns a string that represents the
current object.
• GetType - Gets the type of the current instance.
• ReferenceEquals - Determines whether the
specified object instances are the same instance.
The Protected Methods
• Methods only seen by derived classes.
• MemberWiseClone - Creates a shallow copy of
the current object. Creates a new instance of the
object and sets the new object’s fields to be
identical to this object’s fields. Returns a
reference to the new object.
• Finalize - Allows an object to attempt to free
resources and perform other cleanup operations
before the object is reclaimed by garbage
collection.
Microsoft .NET supports two
kinds of data types

• Value types. Types that are allocated in a


stack or inline in a structure.

• Reference types. Types that are allocated in


a heap.
Value Types
• Value types store the data directly on the stack.
(simply, a last in first out list).
• You access this data directly.
• To create a copy of the value that is assigned,
you can assign a value type to a variable.
• Value types are not inheritable.
• They are implicitly derived from the
System.ValueType class, which derives from
System.Object.
Value Types

Value types include:


• primitives
• enums
• structs
Primitives
Primitives are the foundation of data types. Primitives are
the lowest types available.
You can identify primitives through keywords, which are
aliases for predefined types in the System namespace. For
example, the int or int32 data type is an alias for the
System.Int32 object.

Because all data types are derived from System.Object,


primitives are actually objects with a set of members that are
available for each type. For example, the int32 data type has
a member named MaxValue.
Primitive Types

- byte - short - int


- long - single - double
- decimal - bool - DateTime
- char - string

Of these primitive types, only the string type is a


reference type. All of the other primitive types are
value types.
Type  Class  Object

"In C# a type is defined by a class,


while the individual instances of a
class are known as objects." Jesse
Liberty
In C# Everything happens within
a Class!
class
• A class is a data structure that may contain data members
(such as constants and variables), function members (such
as methods, properties, indexers, operators, events, and
constructors), and nested types.
• Class types support inheritance. Inheritance is a
mechanism whereby a derived class can extend and
specialize a base class.
• Derived classes inherit and can extend the properties and
the methods of the base class. Derived classes can also
override inherited methods with new implementations.
class
Classes are declared using the keyword class. It takes the
following form::
[attributes] [modifiers] class identifier [:base-list] { class-
body }[;]
where:
– attributes (Optional)
– modifiers (Optional) The allowed modifiers are new,
abstract, sealed, and the four access modifiers.
– identifier The class name.
– base-list (Optional) A list that contains the one base class
and any implemented interfaces, all separated by commas.
– class-body Declarations of the class members.
Common Class Members
• fields, which are the variables of the class.
• methods, which implement the
computations and actions that can be
performed by the class.
• properties, which define named
characteristics associated with reading and
writing those characteristics.
Fields
• A field is a member that represents a
variable associated with an object or class.
• Fields maintain class state
Methods
• A method is a member that implements a
computation or action that can be performed by
an object or class.
• Choose a name for your method based on the
following guidelines.
– Use verbs or verb phrases to name methods
– The first letter in the identifier and the first
letter of each subsequent concatenated word
are capitalized.
Method Arguments
• Appear as local variables within the method
• Method arguments are private by default
Properties
(get and set accessor)
get and set accessor
• Provides protection for variables in a class
• Define the variable as private
• Use a get and set accessor to access the variable
• The accessor of a property contains the
executable statements associated with getting
(reading or computing) or setting (writing) the
property. The accessor declarations can contain a
get accessor, a set accessor, or both.
Example
using System;
public class anyClass {
private string name;
public string Name {
get {
return name;
}
set {
name = value;
}
}
}
get accessor
• The body of the get accessor is similar to that of a method.
It must return a value of the property type. The execution
of the get accessor is equivalent to reading the value of the
field.
• When you reference the property, except as the target of
an assignment, the get accessor is invoked to read the
value of the property.
• The get accessor must terminate in a return or throw
statement, and control cannot flow off the accessor body.
set accessor
• The set accessor is similar to a method that returns void. It
uses an implicit parameter called value, whose type is the
type of the property. In the following example, a set
accessor is added to the Name property:
• When you assign a value to the property, the set accessor
is invoked with an argument that provides the new value.
For example:
• It is an error to use the implicit parameter name (value) for
a local variable declaration in a set accessor.
• When accessing a property using the set accessor, preserve
the value of the property before you change it. This will
ensure that data is not lost if the set accessor throws an
exception.
accessors notes
• A property is classified according to the accessors used as
follows:
– A property with a get accessor only is called a read-
only property. You cannot assign a value to a read-only
property.
– A property with a set accessor only is called a write-
only property. You cannot reference a write-only
property except as a target of an assignment.
– A property with both get and set accessors is a read-
write property.
• In a property declaration, both the get and set accessors
must be declared inside the body of the property.
accessors notes continued
• It is a bad programming style to change the state of the
object by using the get accessor. For example, the
following accessor produces the side effect of changing
the state of the object each time the number field is
accessed.
public int Number { get { return number++; //
Don't do this } }
• The get accessor can either be used to return the field
value or to compute it and return it. For example:
public string Name { get { return name != null ?
name : "NA"; } }
• In the preceding code segment, if you don't assign a value
to the Name property, it will return the value NA.
Constructors
• Methods called whenever an object is instantiated
• Before the constructor is called, the object points
to undifferentiated memory.
• After the constructor is called, the object points to
valid instance.
• If a constructor is not defined the CLR creates one
for you.
• Member variables are initialized to default values.
• Constructor method name is the same as the class
name.
Constructor (continued)
• If you provide an overloaded constructor, you must
provide a default constructor, even if it does
nothing.
• Can call the base class constructor using the base
keyword.
• Base class constructor always uses the most derived
type.
• Constructors have no return type
• Typically declared public
• Arguments are defined just like any other method
Type  Class  Object

"In C# a type is defined by a class,


while the individual instances of a
class are known as objects."
What is an object?
• A class is an abstract model.
• An object is the concrete realization or instance
built on the model specified by the class.
• An object is created in the memory using the
keyword 'new' and is referenced by an identifier
called a "reference".
Creating an Object
MyClass myObjectReference = new MyClass();

• In the above, a instance of class MyClass is created


with the variable name of myObjectReference.
The new operator
• Allocates memory for the object from the
managed heap
• Initializes the object’s overhead members (the
CLR uses these to manage the object).
– The object’s pointer to the type’s method table.
– A SyncBlockIndex (used to manage access to
the object by multiple thread).
• Calls the type’s instance constructor, passing any
parameters specified. Most languages call the
base class constructor, but this is not mandated
by the CLR.

Das könnte Ihnen auch gefallen