1 / 98

.NET Fundamentals

.NET Fundamentals. November 20, 2003 Week 3. Class Agenda – November 20, 2003. Questions / Homework? Types (Intrinsic and Reference), Classes, Objects Class Exercise (Demo  derived classes Class Exercise Procedural vs Object Oriented Programming Class Exercise Homework Assignment.

elijah
Télécharger la présentation

.NET Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET Fundamentals November 20, 2003 Week 3

  2. Class Agenda – November 20, 2003 Questions / Homework? Types (Intrinsic and Reference), Classes, Objects Class Exercise (Demo  derived classes Class Exercise Procedural vs Object Oriented Programming Class Exercise Homework Assignment

  3. Questions / Homework ? • Common Email List • Homework ?? • Review public/private and static in class this week? • Move last class from Thursday 12/18 to Monday 12/15? 

  4. Course Schedule

  5. Strongly Typed Object Oriented Programming Environment

  6. 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

  7. Types and Types in .NET

  8. 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

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. .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.

  14. 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

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. Value Types • Value types include: • primitives • enums • structs

  20. 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.

  21. 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.

  22. enumerators The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char.

  23. enum example In this example, an enumeration, Days, is declared. Two enumerators are explicitly converted to int and assigned to int variables. enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; int x = (int) Days.Sun; int y = (int) Days.Fri;

  24. structs • The struct data type inherits from the System.ValueType class. • A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. • A struct is like a light weight class.

  25. structs The .NET structure data type is very similar to a class data type except that a structure data type is a value type (whereas a class data type is a reference type). For increased efficiency, you may want to use structure data types if you do not need the overhead of maintaining a reference pointer. However, because a structure data type is a value type, it is not garbage collected.

  26. Reference types • Reference types store the data on the managed heap and store a pointer to the data on the stack • You access the data in reference types through the reference pointer • Are collected by the garbage collector when they are no longer in use. • Are passed by reference. • Can be extended by inheritance. • Can specify Finalizers. • Reference pointer is type safe. • A variable of reference type always contains a reference to a value of that type or a null reference.

  27. Reference types include the following data types • String • Array • Class • Interface • Delegate

  28. String • .NET String data types are invariant. • Because String data types are read-only after initialization, you cannot directly modify their content. • The String variable contains a pointer to memory that contains the actual data. Any modification to the string deallocates the current memory block and allocates a new memory block for the new value

  29. StringBuilder NOTE: If the cost of deallocating and reallocating string greatly affects performance, you can use the StringBuilder class in Visual Studio .NET. You will notice performance benefits at approximately 300 string concatenations.

  30. Array • Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime. • All array types implicitly inherit from the System.Array class, which inherits from System.Object. Arrays are allocated on the managed heap. • If all of the dimensions of the array are zero, the array has a length of zero, and the array is empty. • In an array reference types are initialized to null, value types are initialized to the default value for their type (i.e. int members are initialized to zero). You can initialize arrays during declaration. • By inheriting from System.Array, each array reference type automatically inherits a set of System.Array methods and properties such as Rank, Length, GetLength, and GetUpperBound.

  31. 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.

  32. Interface • An interface is a contract. • The class or a structure that implements the interface must adhere to the contract. • The contract specifies the members that must be supplied by the class that implements the interface. • The interface is a list of functions. The interface contains methods, properties, and events. • An interface provides no implementation itself.

  33. Delegate • The delegate reference type is central to the programming model of the common language runtime. • Delegates are classes that hold references to procedures. • The Delegate reference provides a managed, type-safe function pointer, which is the delegate type. • A delegate is derived from the System.Delegate class. • Delegates are the basis for events. • The .NET event model uses delegates to bind events to the methods that are used to handle them. The delegate allows other classes to register for event notification by specifying a handler method. When the event occurs, the delegate calls its bound method.

  34. Boxing and Unboxing In some cases, you may want to treat an instance of a value type like an instance of a reference type. Boxing provides this mechanism.Boxing converts a value type to a reference type by performing the following steps: Allocates memory on the managed heap to store the value. Copies the value to the managed heap. Stores the reference to the data (address of the object) on the stack. Unboxing converts an instance of a reference type back to its original value type by returning a pointer to the data within a boxed subject. The pointer does not include the usual overhead that is associated with a true object.

  35. Boxing • Here’s what happens when you box a value type: • Memory is allocated from the managed heap to hold the value type plus the overhead members. • The value type’s fields are copied to the newly allocated memory. • The address of the object is returned. • Note that a whole new object is returned – manipulating the original value type will not change the boxed reference. int value = 123; object o = value; // box int into an object box int value2 = (int) o; // unbox into value2

  36. Unboxing • Here’s what happens when a reference type is unboxed • If the reference is null, a NullReferenceException is thrown • If the reference does not refer to an object that is a boxed value of the desired value type, an InvalidCastException is thrown • A pointer to the value type is returned, which is frequently copied immediately to another value type. int value = 123; object o = value; // box int into an object box long value2 = (long) o; // this throws a cast exception

  37. Casting There are two types of casting: Implicit casting. Implicit casting is transparent to users. The compiler automatically converts from one data type to another. The predefined, implicit conversions always succeed and never cause an error. Explicit casting. Explicit casts are called in the code. Explicit casts cannot guarantee success and may lose information if you cast from a larger type to a smaller type.

  38. 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

  39. In C# Everything happens within a Class!

  40. 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.

  41. class Classes are declared using the keyword class. It takes the following form:: [attributes] [modifiers] classidentifier [: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.

  42. 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.

  43. Instance versus Static Members • Instance members are unique to each object instance and referenced by the object reference. • Static members are limited to one copy, associated with the class type, being referenced by the class type.

  44. Fields • A field is a member that represents a variable associated with an object or class. • Fields maintain class state

  45. 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.

More Related