Artificial intelligence in the post-deep learning era
Chapter 3 part2
1. • Value Types and Reference Types
• Out Type
• Inheritance
• Static members
• Understanding Namespaces and Assemblies
• Casting Objects
• Partial Classes
• Generics
2. Three types of method parameters :
• Value
• Reference
• Out
int num = 10;
ProcessNumber(ref num);
Console.WriteLine(num);
private void ProcessNumber(ref int number)
{
number *= 2;
}
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. public 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)
{}
}
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. 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 grouped
into a single long and messy list.
namespace MyApp
{
public class Product
{
// Code goes here.
}
}
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. Assemblies 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.
9. • 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;
10. 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 keyword
TaxableProduct t1 = p1 as TaxableProduct;
11. 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
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 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.");
13. 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.