Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations.

Similar presentations


Presentation on theme: "Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations."— Presentation transcript:

1 Microsoft.NET Fundamentals

2 Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations for the course

3 Facilities Class hours Building hours Parking Restrooms Meals Phones Messages Smoking Recycling

4 Microsoft Certification Program http://www.microsoft.com/learning/ Exam numberExam Title 98-372Microsoft.NET Fundamentals

5 About This Course Audience Course Prerequisites Course Objectives

6 Course Outline ModulesDescription Lesson 1 Understanding Object Oriented Programming Lesson 2 Understanding Data Types and Collections Lesson 3 Understand Events and Exceptions Lesson 4 Understand Code Compilation and Deployment Lesson 5 Understanding Input/Output (IO) Classes Lesson 6 Understanding Security Lesson 7 Understanding Rackspace APIs

7 Remote Lab Environment - Demo Student Machine Computer name: DJ-xxx C:\ Visual Studio SQL2012 Rackspace Network Virtual Labs

8 Understanding Object Oriented Programming Lesson 1

9 Module Overview What is Object Oriented Programming Classes vs Objects Encapsulation Abstraction Inheritance Polymorphism Interfaces Namespaces Class Libraries

10 Objectives Skills/ConceptsMTA Exam Objectives Understanding Object Oriented Programming Understand Object Oriented Programming Concepts in the.NET Framework (2.2) Understanding Encapsulation Understand Object Oriented Programming Concepts in the.NET Framework (2.2) Understanding Inheritance Understand Object Oriented Programming Concepts in the.NET Framework (2.2) Understanding Polymorphism Understand Object Oriented Programming Concepts in the.NET Framework (2.2) Understanding Interfaces Understand Object Oriented Programming Concepts in the.NET Framework (2.2) Understanding Namespaces Understand.NET Class Hierarchies (2.1) Understand.NET Namespaces (2.3) Understanding and Creating Class Libraries Understand and Create Class Libraries (2.4)

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

12 Object State and Behavior State Properties describe 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

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

14 Defining a C# Class public class Person { private string _firstName; private string _lastName; public string FirstName { get { return _firstName; } set { _firstName = value; } public class Person { private string _firstName; private string _lastName; public string FirstName { get { return _firstName; } set { _firstName = value; }

15 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

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

17 Method Example The InitFields method takes two parameters and uses the parameter values to respectively assign the data fields firstName and 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; } public void InitFields(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; }

18 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 GetArea Method Note: Practice begins on Page 3

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

20 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; } public class Person { private string _firstName; private string _lastName; public Person(string firstName, string lastName, string address) : this() { _firstName = firstName; _lastName = lastName; _address = address; }

21 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.Person objp = 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(); } class Program { static void Main(string[] args) { BusinessRules.Person objp = 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(); }

22 Practice: Create an Object Create an instance of the Rectangle class Execute the GetArea Method Page 5

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

24 Property Example public class Person { private string _firstName; public string FirstName { get { return _firstName; } set { _firstName = value; } } public class Person { private string _firstName; public string FirstName { get { return _firstName; } set { _firstName = value; } }

25 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

26 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; } 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; }

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

28 Static Member Example class Program { static void Main(string[] args) { Console.WriteLine(Person.UserType); } public class Person { public static string UserType { get { return “Person”; } } class Program { static void Main(string[] args) { Console.WriteLine(Person.UserType); } public class Person { public static string UserType { get { return “Person”; } }

29 Practice: Create a Static member Add a Static Method to the Rectangle class Modify the Console Application to call the Static Method Page 10

30 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. Access modifierDescription publicAccess is not restricted. privateAccess is restricted to the containing class. protectedAccess is restricted to the containing class and to any class that is derived directly or indirectly from the containing class. internalAccess is restricted to the code in the same assembly. protected internal \ A combination of protected and internal—that is, access is restricted to any code in the same assembly and only to derived classes in another assembly

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

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

33 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; } public class Person { public string Name{ get; protected set; } } public class Customer : Person { public Customer(string name) { Name = name; } public string GetName() { return Name; }

34 Inheritance - Example

35 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

36 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

37 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; } 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; }

38 Sealed Classes Provide complete functionality Cannot be used as base classes. Use the sealed keyword Represent the leaf level of the object hierarchy

39 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; } 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; }

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

41 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

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

43 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 Rectangle object.

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

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

46 Polymorphism - Example Consider the following set of classes

47 Polymorphism - Example

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

49 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

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

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

52 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 assembly specifies 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.

53 Demonstrations – Add classes to Namespace Add the Person class to the Base Namespace Add the Customer and Employee class to the Person Namespace

54 Recap Objects Classes, methods, properties, delegates, events Namespaces Static members Values and References Encapsulation Access Modifiers Inheritance Abstract and sealed classes Casting, is and as operators Polymorphism Override and new keywords Interfaces Namespaces and class libraries

55 Lab 1 – Create Person classes Create the Business Rules Class Library Project Create the Person abstract base class Create the Employee class that inherits from the Person class Create the Customer class that inherits from the Person class


Download ppt "Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations."

Similar presentations


Ads by Google