You are on page 1of 23

Classes and Structs

Lecturer: NGUYEN Van Hieu


nvhieuqt@dut.udn.vn
Contents
v Classes
v Structs
v Constructor
v Destructor
v Fields
v Methods
v Properties
v Indexers
v Nested Types
Contents of Classes or Structs
class C {
…fields, constants.. // for object-oriented programming
…methods…
…constructors, deconstructions…

…properties… // for component-based programming


…events…

…indexers… // for amenity


…overloaded operators…

…nested types(classes, interfaces, delegates, structs, enums)…


}
Classes

u Objects are allocated on the heap


u Objects must be created with new
u Classes can inherit from one other class (single
code inheritance).
u Classes can implement multiple
interfaces(multiple type inheritance)
Structs

u Objects are allocated on the stack


u Can be allocates with new
v Point p; // fields of p are not yet initialized
v Point q = new Point();
u Fields must not be initialized at their declaration
v struct Point { int x = 0; // compilation error }
u Can neither inherit nor be inherited, but can implement
interfaces
Visibility Modifiers
u public: visible where the declaring namespace is known
v Members of interfaces and enumerations are public by default.
v Types in a namespace (classes, structs, interfaces, enums,
delegates) have default visibility internal
u private: only visible in declaring class or struct
v Members of classes and structs are private by default (fields,
methods,…)
u Example
Fields and Constants
Static Fields and Constants
u Belong to a class, not to an object
Methods
u Example
Static Methods
u Example
Method Overloading
u Methods of a class may have the same name
v If they have different numbers of parameters, or
v If they have different parameter types, or
v If they have different parameter kinds(value, ref/out)
u Example

u Calls: int i; long n; short s;


v F(i); F(‘a’); F(i,n); F(n,s); // F(long, int)
v F(i,s);// error
v F(i,i);// error
Constructors for Classes
u Example

u Constructors can be overloaded


u A constructor may call another constructor
with this.
u Before a constructor is called, fields are
possibly initialized,
Default Constructor
u If no constructor was declacred in a class,
the compiler generates a parameterless
default constructor:

u If a constructor was declared, no default


constructor is generated:
Constructor for Structs
u Example

u For every struct the compiler generates a parameterless


default constructor (even if there are other constructors).
u The default constructor zeroes all fields.
u Programmers must not declare a parameterless
constructor for structs
Destructors
u Example

u No public and private.


u Called for an object before it is removed by
the garbage collector.
Properties
u Example

u Used as “smart fields”


Properties
u get or set can be omitted

u Why are properties a goog idea?


v Interface and implementation of data may differ.
v Allows read-only and write-only fields.
v Can validate a field when it is assigned.
Indexers
u Operator for indexing a collection

u MonthlySales sales = new MothlySales();


u …
u Console.WriteLine(sales[1] + sales[“Feb”]);
Overloaded Operators
u Static method for implementing a operator

u Use
v Fraction a = new Fraction(1,2);
v Fraction b = new Fraction(3,4);
v Fraction c = a + b; //c.x == 10, c.y ==8
u The following operators can be overloaded
v Arithmetic: +, -, *,/ %,++, --
v Realational: ==, !=, <, >, <=, >=
v Bit operators: &, |, ^
v Other: !,~,>>,<<, true, false
u Must return value
Conversion Operators
u Implicit conversion
v long = int;
u Explicit conversion
v int = (int )long
u Conversion operators for custom types

u Use
v Fraction f = 3; //f.x ==2, f.y ==1
v Fraction l = (int)f; // I = 3
Nested Types
u

u Inner class can access all members of the outer class (even
private members)
u Outer class can access only public members of the inner class.
u Other class can access an inner class only if it is public.
u Nested class can also be structs, enums, delegates.
Thank you

You might also like