Chapter 1 – Type Fundamentals

SAMS CSHARP 4.0 (P.20)
Default using namespaces that VS includes in each file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

Assembly modifiers:
Public – public to all
Private – accessible to code in the same type
Internal – accessible to code in the same assembly
Protected – Assessible to code in the same type or derived type
Protected Internal – assessible to code in the same assembly or a derived class in another assembly.

If not specify: default is Internal

Class example:
public class Vertex3d

{

//fields

private double _x; // good practice to declare as private

private double _y;

private double _z;

//properties

public double X

{ get { return _x; } set { _x = value; } } // value is the implied argument (passed from the code)

public double Y

{get { return _y; } set { _y = value; }}

public double Z

{get { return _z; }set { _z = value; }}

//method

public void SetToOrigin()

{

X = Y = Z = 0.0;

}

}

e.g.,
Vertex3d v = new Vertex3d();

v.X = 13.0;

Use Auto-Implemented Properties

This

class MyClass
{
private int _field = 0;
public int Field
{ get { return _field; } set { _field = value; } }
}

can also be written as:

class MyClass
{
public int Field {get; set;}
//must initialize value in constructor now
public MyClass()
{
this.Field = 0;
}
}

Define Static Members

Define data or methods that apply to the TYPE, not individual instances.
public class Vertex3d
{…
public static Vertex3d Add(Vertex3d a, Vertex3d b)
{
Vertex3d result = new Vertex3d();
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
return result;
}
}

The static method is called like in this example:

Vertex3d a = new Vertex3d(0,0,1);
Vertex3d b = new Vertex3d(1,0,1);
Vertex3d sum = Vertex3d.Add(a, b);

Add a Constructor

Automatic initialization of new objects of the class.
A special method, called a constructor, with the same name as the class, with no return type. A constructor runs when a type is created; it is never called directly.

class Vertex3d
{
public Vertex3d()
{
_x = _y = _z = 0.0;
}

public Vertex3d(double x, double y, double z)
{
this._x =x;
this._y = y;
this._z = z;
}
}

Initialize Properties at Construction

Initialize class properties when the variable is created, even if the constructor does not provide the arguments to do so.

class Person{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}

Person p = new Person() { Id = 1, Name = “Ben”, Address = “Redmond, WA” };

Use const and readonly

const and readonly are sed to define data that does not change. Difference:
constmust be defined at declaration
readonly – can be set at declaration, or constructor, but nowhere else.

Reuse Code in Multiple Constructors

The this keyword will call other constructors within the same class

public class Vertex3d
{
public Vertex3d(double x, double y, double z)
{
this._x = x;
this._y = y;
this._z = z;
}

public Vertex3d(System.Drawing.Point point)
:this(point.X, point.Y, 0)
{

}

}

Derive from a Class
Use inheritance to reuse the base class and new functionality
use : ParentclassName

Call a Base Class Constructor
You have a child class and want its constructor to call a specific base class constructor



Override a Base Class’s method or Property

Or, from the Derived class, you can call base.DoSomthing()
to call base class’s method.

Over Non-Virtual Methods and Properties

Base class’s functionality isn’t declared as virtual, but you want to override it anyway.

Create An Interface

An interface doesn’t specify what something is, but rather some behavior.

Implement Interfaces

Click this and it will generate all the implementations for you.

Implement Multiple Interfaces

If both Interface has a method that is the same name (stop()), then in the implementation you need to specify which interface (at least one of them), e.g.,

void Irecordable.Stop() {

}

Here is how to call these two methods:

AudioFile file = new AudioFile();
file.Stop();//calls the IPlayable version
((IRecordable)file).Stop();//calls the IRecordable version

Create a Struct

structure is very similar to a class; cannot be derived from; sealed. Must be initialized in constructor.

Create an Anonymous Type

use var to create anonymous types that contain properties that you define inline.

Prevent Instantiation with an Abstract Base Class

Don’t want anyone to instantiae the base class directly. Use ABSTRACT.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: