Microsoft .NET Fundamentals

53
Microsoft .NET Fundamentals

description

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. - PowerPoint PPT Presentation

Transcript of Microsoft .NET Fundamentals

Page 1: Microsoft .NET Fundamentals

Microsoft .NET Fundamentals

Page 2: Microsoft .NET Fundamentals

Introduction

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

Page 3: Microsoft .NET Fundamentals

Facilities

• Class hours• Building hours• Parking• Restrooms• Meals• Phones• Messages• Smoking• Recycling

Page 4: Microsoft .NET Fundamentals

Microsoft Certification Program

http://www.microsoft.com/learning/

Exam number Exam Title

98-372 Microsoft .NET Fundamentals

Page 5: Microsoft .NET Fundamentals

About This Course

• Audience• Course Prerequisites• Course Objectives

Page 6: Microsoft .NET Fundamentals

Course OutlineModules Description

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

Page 7: Microsoft .NET Fundamentals

Remote Lab Environment - Demo

Student Machine• Computer name: DJ-xxx

C:\Visual Studio

SQL2012Rackspace Network

Virtual Labs

Page 8: Microsoft .NET Fundamentals

Understanding Object Oriented Programming

Lesson 1

Page 9: Microsoft .NET Fundamentals

Module Overview

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

Page 10: Microsoft .NET Fundamentals

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)

Page 11: Microsoft .NET Fundamentals

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

Page 12: Microsoft .NET Fundamentals

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.

Page 13: Microsoft .NET Fundamentals

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

Page 14: Microsoft .NET Fundamentals

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

Page 15: Microsoft .NET Fundamentals

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.

Page 16: Microsoft .NET Fundamentals

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

Page 17: Microsoft .NET Fundamentals

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

Page 18: Microsoft .NET Fundamentals

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.

Page 19: Microsoft .NET Fundamentals

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

Page 20: Microsoft .NET Fundamentals

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(); } }

Page 21: Microsoft .NET Fundamentals

Practice: Create an Object

• Create an instance of the Rectangle class• Execute the GetArea Method• Page 5

Page 22: Microsoft .NET Fundamentals

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.

Page 23: Microsoft .NET Fundamentals

Property Example

public class Person { private string _firstName;

public string FirstName { get { return _firstName; } set { _firstName = value; } } }

Page 24: Microsoft .NET Fundamentals

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

Page 25: Microsoft .NET Fundamentals

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

Page 26: Microsoft .NET Fundamentals

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.

Page 27: Microsoft .NET Fundamentals

Static Member Example

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

Page 28: Microsoft .NET Fundamentals

Practice: Create a Static member

• Add a Static Method to the Rectangle class• Modify the Console Application to call the Static Method• Page 10

Page 29: Microsoft .NET Fundamentals

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 modifier Description

public Access is not restricted.

private Access is restricted to the containing class.

protected Access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class.

internal Access 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

Page 30: Microsoft .NET Fundamentals

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.

Page 31: Microsoft .NET Fundamentals

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.

Page 32: Microsoft .NET Fundamentals

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

Page 33: Microsoft .NET Fundamentals

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

Page 34: Microsoft .NET Fundamentals

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

Page 35: Microsoft .NET Fundamentals

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

Page 36: Microsoft .NET Fundamentals

Sealed Classes

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

Page 37: Microsoft .NET Fundamentals

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

Page 38: Microsoft .NET Fundamentals

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.

Page 39: Microsoft .NET Fundamentals

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

Page 40: Microsoft .NET Fundamentals

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.

Page 41: Microsoft .NET Fundamentals

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.

Page 42: Microsoft .NET Fundamentals

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.

Page 43: Microsoft .NET Fundamentals

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.

Page 44: Microsoft .NET Fundamentals

Polymorphism - Example

• Consider the following set of classes

Page 45: Microsoft .NET Fundamentals

Polymorphism - Example

Page 46: Microsoft .NET Fundamentals

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.

Page 47: Microsoft .NET Fundamentals

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

Page 48: Microsoft .NET Fundamentals

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.

Page 49: Microsoft .NET Fundamentals

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.

Page 50: Microsoft .NET Fundamentals

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.

Page 51: Microsoft .NET Fundamentals

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

Page 52: Microsoft .NET Fundamentals

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

Page 53: Microsoft .NET Fundamentals

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