chapter 3 part2

13
Agenda Value Types and Reference Types Out Type • Inheritance Static members Understanding Namespaces and Assemblies Casting Objects Partial Classes • Generics

Upload: application-developer

Post on 05-Dec-2014

497 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Chapter 3  part2

Agenda

• Value Types and Reference Types

• Out Type

• Inheritance

• Static members

• Understanding Namespaces and Assemblies

• Casting Objects

• Partial Classes

• Generics

Page 2: Chapter 3  part2

Value Type and Reference TypeThree types of method parameters :

• Value• Reference• Out

int num = 10;ProcessNumber(ref num);Console.WriteLine(num);

private void ProcessNumber(ref int number){number *= 2;}

Page 3: Chapter 3  part2

Out Type Parameter

int num = 10;int doubled, tripled;ProcessNumber(num, out doubled, out tripled);

private void ProcessNumber(int number, out int doubled, out int tripled){doubled = number * 2;tripled = number * 3;}

Page 4: Chapter 3  part2

Inheritancepublic class TaxableProduct : Product

{private decimal taxRate = 1.15M;public decimal TotalPrice{get{return (Price * taxRate);}}

public TaxableProduct(string name, decimal price, string imageUrl) :base(name, price, imageUrl){ }}

Page 5: Chapter 3  part2

Static Members

public class TaxableProduct : Product{// (Additional class code omitted for clarity.)private static decimal taxRate = 1.15M;

// Now you can call TaxableProduct.TaxRate, even without an object.public static decimal TaxRate{get{ return taxRate; }set{ taxRate = value; }}}

Page 6: Chapter 3  part2

NamespacesEvery piece of code in .NET exists inside a class

type. In turn, every class type exists inside a namespace.

Without namespaces, these types would all be grouped into a single long and messy list.

namespace MyApp{public class Product{// Code goes here.}}

Page 7: Chapter 3  part2

Using Namespacesnamespace MyCompany

{namespace MyApp{

public class Product{ //code goes here

}}

}

using MyCompany.MyApp;

Consider the situation without importing a namespace:

MyCompany.MyApp.Product salesProduct = new MyCompany.MyApp.Product();

Page 8: Chapter 3  part2

AssembliesAssemblies are the physical files that contain compiled code. Typically, assembly files have the extension .exe if they are stand-alone applications or .dll if they’re reusable components.

An assembly can contain multiple namespaces. Conversely, more than one assembly file can contain classes in the same namespace.

Technically, namespaces are a logical way to group classes. Assemblies, however, are a physical package for distributing code.

Often, assemblies and namespaces have the same names. For example, you’ll find the namespace System.Web in the assembly file System.Web.dll.

However, this is a convenience, not a requirement. For example, the basic types in the System namespace come from the mscorlib.dll assembly.

Page 9: Chapter 3  part2

Casting Objects• Product

TaxableProduct

TaxableProduct t1 = new TaxableProduct("Kitchen Garbage", 49.99M, "garbage.jpg");

// Cast the TaxableProduct reference to a Product reference.Product p1 = t1;

// This code generates a compile-time error.decimal TotalPrice = theProduct.TotalPrice;

Correct way is :Product p1 = new TaxableProduct();TaxableProduct t1 = (TaxableProduct) p1;

Page 10: Chapter 3  part2

Casting Objects

This is wrong :Product p1 = new Product();TaxableProduct t1 = (TaxableProduct) p1;

Solution is check whether p1 is TaxableProduct then cast it

if (p1 is TaxableProduct){TaxableProduct t1 = (TaxableProduct) p1;}

Another way to type cast is use as keywordTaxableProduct t1 = p1 as TaxableProduct;

Page 11: Chapter 3  part2

Partial Classes

Partial classes give you the ability to split a single class into more than one C# source code (.cs) file.

Partial class behaves the same as a normal class. This means every method, property, and variable you’ve defined in the class is available everywhere, no matter which source file contains it.

When you compile the application, the compiler tracks down each piece of the Product class and assembles it into a complete unit. It doesn’t matter what you name the source code files, so long as you keep the class name consistent.

Example:Product class can be placed in two .cs file Product1.cs and Product2.cs

Page 12: Chapter 3  part2

GenericsGenerics allow you to create classes that are parameterized by type.ArrayList products = new ArrayList();// Add several Product objects.products.Add(product1);products.Add(product2);products.Add(product3);// Notice how you can still add other types to the ArrayList.products.Add("This string doesn't belong here.");

// Instead create the List for storing Product objects.List<Product> products = new List<Product>();Now you can add only Product objects to the collection:// Add several Product objects.products.Add(product1);products.Add(product2);products.Add(product3);// This line fails. In fact, it won't even compile.products.Add("This string can't be inserted.");

Page 13: Chapter 3  part2

Summary

At its simplest, object-oriented programming is the idea that your code should be organized into separate classes.

If followed carefully, this approach leads to code that’s easier to alter, enhance, debug, and reuse.