• Value Types and Reference Types• Out Type• Inheritance• Static members• Understanding Namespaces and Assemblies• Casting...
Three types of method parameters :• Value• Reference• Outint num = 10;ProcessNumber(ref num);Console.WriteLine(num);privat...
int num = 10; int doubled, tripled; ProcessNumber(num, out doubled, out tripled);private void ProcessNumber(int number, ou...
public class TaxableProduct : Product{private decimal taxRate = 1.15M;public decimal TotalPrice{get{return (Price * taxRat...
public class TaxableProduct : Product{// (Additional class code omitted for clarity.)private static decimal taxRate = 1.15...
Every piece of code in .NET exists inside a class type.In turn, every class type exists inside a namespace.Without namespa...
namespace MyCompany   {     namespace MyApp     {           public class Product           { //code goes here           } ...
Assemblies are the physical files that contain compiled code.Typically, assembly files have the extension .exe if they are...
•                            Product                         TaxableProductTaxableProduct t1 = new TaxableProduct("Kitchen...
This is wrong :Product p1 = new Product();TaxableProduct t1 = (TaxableProduct) p1;Solution is check whether p1 is TaxableP...
Partial classes give you the ability to split a single class into more thanone C# source code (.cs) file.Partial class beh...
Generics allow you to create classes that are parameterized by type.ArrayList products = new ArrayList();// Add several Pr...
At its simplest, object-oriented programming is the idea thatyour code should be organized into separate classes.If follow...
Upcoming SlideShare
Loading in …5
×

Chapter 3 part2

366 views
278 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
366
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 3 part2

  1. 1. • Value Types and Reference Types• Out Type• Inheritance• Static members• Understanding Namespaces and Assemblies• Casting Objects• Partial Classes• Generics
  2. 2. Three types of method parameters :• Value• Reference• Outint num = 10;ProcessNumber(ref num);Console.WriteLine(num);private void ProcessNumber(ref int number){number *= 2;}
  3. 3. 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;}
  4. 4. public class TaxableProduct : Product{private decimal taxRate = 1.15M;public decimal TotalPrice{get{return (Price * taxRate);}}public TaxableProduct(string name, decimal price, stringimageUrl) :base(name, price, imageUrl){}}
  5. 5. 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; }}}
  6. 6. Every 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 groupedinto a single long and messy list.namespace MyApp{public class Product{// Code goes here.}}
  7. 7. namespace 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();
  8. 8. Assemblies are the physical files that contain compiled code.Typically, assembly files have the extension .exe if they are stand-aloneapplications or .dll if they’re reusable components.An assembly can contain multiple namespaces. Conversely, more thanone 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. Forexample, you’ll find the namespace System.Web in the assembly fileSystem.Web.dll.However, this is a convenience, not a requirement. For example, thebasic types in the System namespace come from the mscorlib.dllassembly.
  9. 9. • Product TaxableProductTaxableProduct 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;
  10. 10. This is wrong :Product p1 = new Product();TaxableProduct t1 = (TaxableProduct) p1;Solution is check whether p1 is TaxableProduct then cast itif (p1 is TaxableProduct){TaxableProduct t1 = (TaxableProduct) p1;}Another way to type cast is use as keywordTaxableProduct t1 = p1 as TaxableProduct;
  11. 11. Partial classes give you the ability to split a single class into more thanone C# source code (.cs) file.Partial class behaves the same as a normal class. This means everymethod, property, and variable you’ve defined in the class is availableeverywhere, no matter which source file contains it.When you compile the application, the compiler tracks down each pieceof the Product class and assembles it into a complete unit. It doesn’tmatter what you name the source code files, so long as you keep theclass name consistent.Example:Product class can be placed in two .cs file Product1.cs and Product2.cs
  12. 12. Generics 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 doesnt 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 wont even compile.products.Add("This string cant be inserted.");
  13. 13. At its simplest, object-oriented programming is the idea thatyour code should be organized into separate classes.If followed carefully, this approach leads to code that’seasier to alter, enhance, debug, and reuse.

×