530 likes | 677 Views
Microsoft .NET Fundamentals. Introduction. Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations for the course. Facilities. Class hours Building hours Parking Restrooms Meals Phones Messages Smoking Recycling.
E N D
Introduction • Name • Company affiliation • Title/function • Job responsibility • Database & Developer experience • Your expectations for the course
Facilities • Class hours • Building hours • Parking • Restrooms • Meals • Phones • Messages • Smoking • Recycling
Microsoft Certification Program http://www.microsoft.com/learning/
About This Course • Audience • Course Prerequisites • Course Objectives
C:\ Remote Lab Environment - Demo Rackspace Network Visual Studio SQL2012 Virtual Labs Student Machine • Computer name: DJ-xxx
Module Overview • What is Object Oriented Programming • Classes vs Objects • Encapsulation • Abstraction • Inheritance • Polymorphism • Interfaces • Namespaces • Class Libraries
What is Object Oriented Programming? • Object-oriented programming is a programming technique that makes use of objects to abstract actions and logic. • Represent code in the form of real world objects with States and Behaviors • State: What is the objects current status (width, height) • Behavior: What can the object do (Place Order, Delete Customer, Save)
Object State and Behavior • State • Propertiesdescribe the objects state (Make, Model, Color, Current Mileage, Current Speed) • Behavior • Methods specify an objects behaviors (Accelerate, Brake, Turn) • Events typically notify other objects that a method (behavior) has been executed/completed
Objects defined by Classes • A class defines a blueprint for an object. • A class defines how the objects should be built and how they should behave. • An object is also known as an instance of a class.
Defining a C# Class public class Person { private string _firstName; private string _lastName; public string FirstName { get { return _firstName; } set { _firstName = value; } } }
Demonstration – Define a Class • Create the Person class with code • Create the Customer class using the class designer • Create the Employee class using the class designer
Methods • A method is a block of code containing a series of statements. • A method defines the actions or operations supported by a class. • When a method’s return type is void, a return statement with no value can be used. • A method is defined by specifying: • Access level • Return type, • Name of the method, and • Optional list of parameters • A block of code enclosed in braces.
Method Example • The InitFields method takes two parameters and uses the parameter values to respectively assign the data fields firstNameand lastName. • If a return statement is not used, as in the InitFields method, the method will stop executing when it reaches the end of the code block. public void InitFields(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; }
Practice: Create a Simple Class • Create a Console Application • Add a class named Rectangle • Add length and width Private Fields • Add a Constructor • Add the GetAreaMethod Note: Practice begins on Page 3
Constructors • Constructors are special class methods that are executed when a new instance of a class is created. • Constructors are used to initialize the data members of the object. • Constructors must have exactly the same name as the class, and they do not have a return type. • Multiple constructors, each with a unique signature, can be defined for a class.
Constructor Example public class Person { private string _firstName; private string _lastName; public Person(string firstName, string lastName, string address) : this() { _firstName = firstName; _lastName = lastName; _address = address; } }
Creating Objects • Objects need a template that defines how they should be built. • All objects created from the same template look and behave in a similar way. class Program { static void Main(string[] args) { BusinessRules.Personobjp = new BusinessRules.Person("Antoine", "Victor", "123 Elm Street"); Console.WriteLine(objp.FirstName + " " + objp.LastName + " " + objp.Address + " " + objp.City); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
Practice: Create an Object • Create an instance of the Rectangle class • Execute the GetAreaMethod • Page 5
Properties • Class members accessed like data fields but contain code like a method. • A property has two accessors, get and set. • Get accessor: Used to return the property value • Set accessor: Used to assign a new value to the property.
Property Example public class Person { private string _firstName; public string FirstName { get { return _firstName; } set { _firstName = value; } } }
Practice: Create a Property • Add Length and Width Properties to the Rectangle Class • Modify the Console application to set Length and Width Properties • Run the Console Application Note: Practice begins on page 6
The this Keyword • The this keyword is a reference to the current instance of the class. • You can use the this keyword to refer to any member of the current object. public class Person { private string _firstName; private string _lastName; public Person(string firstName, string lastName, string address) : this() { this._firstName= firstName; this._lastName= lastName; this._address= address; } }
Static Members • The static keyword is used to declare members that do not belong to individual objects but to a class itself. • When an instance of a class is created, a separate copy is created for each instance field, but only one copy of a static field is shared by all instances. • A static member cannot be referenced through an instance object. Instead, a static member is referenced through the class name.
Static Member Example class Program { static void Main(string[] args) { Console.WriteLine(Person.UserType); } public class Person { public static string UserType { get { return “Person”; } } }
Practice: Create a Static member • Add a Static Method to the Rectangle class • Modify the Console Application to call the Static Method • Page 10
Encapsulation • Encapsulation is a mechanism to restrict access to a class or class members in order to hide design decisions that are likely to change. • Access modifiers control where a type or type member can be used.
Abstraction • Abstraction to hide complexity by exposing only that which is necessary for interaction with an object. • To expose simple method names that allow object interaction without exposing the internal functionality of the class • Abstraction and Encapsulation are complementary concepts.
Inheritance • Inheritance is an OOP feature that allows you to develop a class once, and then reuse that code over and over as the basis of new classes. • The class whose functionality is inherited is called a base class. • The class that inherits the functionality is called a derived class • A derived class can also define additional features that make it different from the base class. • Unlike classes, the structs do not support inheritance.
Inheritance Example public class Person { public string Name{ get; protected set; } } public class Customer : Person { public Customer(string name) { Name = name; } public string GetName() { return Name; } }
Practice: Creating Derived Classes • Create a Polygon class • Add Width and Length Properties to the Polygon class • Remove Length and Width Properties from the Rectangle class • Edit the Rectangle class to inherit from the Polygon class • Run the Console Application Note: Practice begins on page 12
Abstract Classes • Lowest Common Denominator • Common member definitions for all derived classes • Often provide incomplete implementation. • Abstract / Base classes may not be instantiated directly • To instantiate an abstract class • Inherit from it creating a new class • Complete its members implementation. • Instantiate the class • Set Properties • Execute Methods
Abstract Class Example public abstract class Person { public string Name{ get; protected set; } } public class Customer : Person { public Customer(string name) { Name = name; } public string GetName() { return Name; } }
Sealed Classes • Provide complete functionality • Cannot be used as base classes. • Use the sealed keyword • Represent the leaf level of the object hierarchy
Sealed Class Example public abstract class Person { public string Name{ get; protected set; } } public sealed class Customer : Person { public Customer(string name) { Name = name; } public string GetName() { return Name; } }
Inheriting from Object • The Object class is the ultimate base class of all the classes in the .NET Framework. • All classes in the .NET Framework inherit either directly or indirectly from the Object class.
Demonstration – Inherit a Base class • Add the abstract keyword to the Person class • Inherit the Person class in the Customer class • Add the sealed keyword to the Customer class
Casting • In C#, you can cast an object to any of its base types. • All classes in the .NET Framework inherit either directly or indirectly from the Objectclass. • Assigning a derived class object to a base class object doesn’t require any special syntax: • Assigning a base class object to a derived class object must be explicitly cast: • At execution time, if the value of o is not compatible with the Rectangle class, the runtime throws a System.InvalidCastException.
The is Operator • To avoid runtime errors such as the InvalidCastException, the is operator can be used to check whether the cast is allowed before actually performing the cast. • Here, the runtime checks the value of the object o. The cast statement is only executed if o contains a Rectangleobject.
The as Operator • The as operator is similar to the cast operation but, in the case of as, if the type conversion is not possible, null is returned instead of raising an exception. • At runtime, if it is not possible to cast the value of variable o to a rectangle, a value of null is assigned to the variable r. No exceptions will be raised.
Polymorphism • Polymorphism is the ability of derived classes to share common functionality with base classes but still define their own unique behavior. • Polymorphism allows the objects of a derived class to be treated at runtime as objects of the base class. When a method is invoked at runtime, its exact type is identified, and the appropriate method is invoked from the derived class.
Polymorphism - Example • Consider the following set of classes
The override and new Keywords • The override keyword replaces a base class member in a derived class. • The new keyword creates a new member of the same name in the derived class and hides the base class implementation.
Demonstration – Define polymorphic method • Add the Create method to the Person class • Override the Create method in the Customer class • Override the Create method in the Employee class
Interfaces • Interfaces are used to establish contracts through which objects can interact with each other without knowing the implementation details. • An interface definition cannot consist of any data fields or any implementation details such as method bodies. • A common interface defined in the System namespace is the IComparable namespace. This is a simple interface defined as follows: • Each class that implements IComparable is free to provide its own custom comparison logic inside the CompareTo method.
Namespaces • A namespace is a language element that allows you to organize code and create globally unique class names. • The .NET Framework uses namespaces to organize all its classes. • The System namespace groups all the fundamental classes. • The System.Data namespace organizes classes for data access. • The System.Web namespace is used for Web-related classes.
Class Libraries • A class library is a collection of functionality defined in terms of classes, interfaces and other types that can be reused to create applications, components, and controls. • Namespace enables you to organize classes into logical grouping. A namespace can span over one or more assemblies. • An assemblyspecifies which code goes into which file on the disk. • When packaging code, you should carefully plan to package related functionality together and unrelated functionality in separate assemblies.