AgendaClasses
Objects
Adding Properties & Methods to a class
Constructors
Using Delegates
Create a Project Using Class          ClassesClasses are code definitions or blueprints for  its instances or simply objectspublic class Product{private string name;private decimal price;private string imageUrl;public string Name { get; set; }public decimal Price { get; set; }public string ImageUrl { get; set; }}
Adding Properties to the Classpublic string Name{get{return name;}set{name = value;}}public decimal Price{get{return price;}set{price = value;}}public string ImageUrl{get{return imageUrl;}set{imageUrl = value;}
Adding ConstructorA constructor is a method that automatically runs when a class object is first created. In C#, the constructor always has the same name as the name of the class. Unlike a normal method, the constructor doesn’t define any return type, not even void. public class Product{// (Additional class code omitted for clarity.)public Product(string name, decimal price){Name = name;Price = price;}public Product(string name, decimal price, string imageUrl){Name = name;Price = price;ImageUrl = imageUrl;}}
Adding a Methodpublic class Product{// (Additional class code omitted for clarity.)public string GetHtml(){string htmlString;htmlString = "<h1>" + name + "</h1><br />";htmlString += "<h3>Costs: " + Price.ToString() + "</h3><br />";htmlString += "<imgsrc='" + imageUrl + "' />";return htmlString;}}
      Creating ObjectsObjects are the instance of a class. They can be created by typing a keyword new, which means it grabs on to a piece of memory and creates the object there.Product saleProduct = new Product();// Optionally you could do this in two steps:// Product saleProduct;// saleProduct = new Product();saleProduct.Name = "Kitchen Garbage";saleProduct.Price = 49.99M;saleProduct.ImageUrl = "http://mysite/garbage.png";

Chapter 3 - part1

  • 1.
  • 2.
  • 3.
    Adding Properties &Methods to a class
  • 4.
  • 5.
  • 6.
    Create a ProjectUsing Class ClassesClasses are code definitions or blueprints for its instances or simply objectspublic class Product{private string name;private decimal price;private string imageUrl;public string Name { get; set; }public decimal Price { get; set; }public string ImageUrl { get; set; }}
  • 7.
    Adding Properties tothe Classpublic string Name{get{return name;}set{name = value;}}public decimal Price{get{return price;}set{price = value;}}public string ImageUrl{get{return imageUrl;}set{imageUrl = value;}
  • 8.
    Adding ConstructorA constructoris a method that automatically runs when a class object is first created. In C#, the constructor always has the same name as the name of the class. Unlike a normal method, the constructor doesn’t define any return type, not even void. public class Product{// (Additional class code omitted for clarity.)public Product(string name, decimal price){Name = name;Price = price;}public Product(string name, decimal price, string imageUrl){Name = name;Price = price;ImageUrl = imageUrl;}}
  • 9.
    Adding a Methodpublicclass Product{// (Additional class code omitted for clarity.)public string GetHtml(){string htmlString;htmlString = "<h1>" + name + "</h1><br />";htmlString += "<h3>Costs: " + Price.ToString() + "</h3><br />";htmlString += "<imgsrc='" + imageUrl + "' />";return htmlString;}}
  • 10.
    Creating ObjectsObjects are the instance of a class. They can be created by typing a keyword new, which means it grabs on to a piece of memory and creates the object there.Product saleProduct = new Product();// Optionally you could do this in two steps:// Product saleProduct;// saleProduct = new Product();saleProduct.Name = "Kitchen Garbage";saleProduct.Price = 49.99M;saleProduct.ImageUrl = "http://mysite/garbage.png";