04 C Advanced Features New
04 C Advanced Features New
Inheritance - introduction
• Syntax:
class subclass-Name : baseClass-Name
{
variable declaration;
method declaration
}
Inheritance - example
Characteristics of inheritance
• A derived class extends its direct base class. It can add new members to
those it inherits. However, it cannot change or remove the definition of an
inherited member
• A derived class can hide an inherited member
• A derived class can override an inherited member
• An instance of a class contains a copy of all instance fields declared in
the class & its base class
• Constructors are not inherited
Class Visibility
• Public
• Private
• Protected
• Internal
• Protected internal
Class members visibility
Accessibility Constraints
• At times we can have one base class and a number of different derived
classes
• The top-most base class simply acts as a base for others & is not useful
on its own
• We might not want to create an objects of such classes
• This can be done by making the class as abstract.
Abstract methods
• Write a program using Virtual and Override keyword that does the
following tasks.
• A virtual function Engine() that has basic properties of engine like Power
of engine, RPM, no of Cylinder etc.
• This function should be overridden in child class according to function.
Interface
• General Form:
interface InterfaceName
{
member declaration
}
• Like classes, interfaces can also be extended.
• That is, an interface can be sub interfaced from other interfaces
• General Form:
interface name2 : name1
{
members of name2
}
• An interface can not extend classes
Interface example
Abstract classes and interfaces
• An abstract class can use interface in the base class list
• Here the interface metrhods are implemented as abstract methods
• Example:
interface A
{
void Method();
}
Abstract class B : A
{
public abstract void Method();
}
• Here class B not impelemnted the interface method: it simply redeclares as a public
abstract method
• It is the duty of the class that derivies from B to override and implement the method
Nested classes
• When catching an exception of a particular class, all its inheritors are caught too
• Example:
try
{
// Do some work that raise an exception
}
catch(ArithmeticException)
{
// Handle the caught arithmetic exception
}
• Handles ArithmeticException and its successors DivideByZeroException and
OverlowException
Be Careful!
Handling all exceptions
• The construction:
try
{
//Do some work that cause the exception
}
finally
{
//this block will always execute
}
• Ensures execution of given block in all cases
• Used for execution of cleaning-up code, eg: releasing resources
try-finally example
Collections
Fast lookups are critical. The Dictionary type provides fast lookups
with keys to get values. With it we use keys and values of any type,
including ints and strings.
Dictionary - example
List
Arrays do not dynamically resize. The List type does. With List, you do not
need to manage the size on your own. This type is ideal for linear collections
not accessed by keys. It provides many methods and properties.
List - example
Queue
•Browsable
Should a property or event be displayed in the property window
•Serializable
Allows a class or struct to be serialized
•Obsolete
Compiler will complain if target is used
Generic types
• Link
Writing Generic class
• class SomeName<T> {
// T can be used inside class as
// parameter type
// return type
// property type
}
• Naming convention: The type parameter is usuall call ‘T’
Default values
• Sometimes you need to get a default value for a generic type parameter
• Example: T find(int id)
• If there is no element with the specified id we want to return the default value
• Default values
• Null: References types, like String, etc.
• O: int, etc.
• False: bool
• Default value table http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx
• Syntax
• default(T)
• T find(int id) { …. return default(T); }
Generic constraints
• In a generic class, all you can do to a variable of the type parameter, are the
operations defined in the class Object
• That is not much! ToString(), Equals(), etc.
• If you want more you must constrain the type parameter
• Generics constraints guarantees that the type has certain operations
• Example: GenericConstraints
• Syntax
• Class SomeName<T> where T : constraint { … }
• Class SomeName<T> : ISomeInterface where T : constraint { … }
• Different types of constraints
• http://msdn.microsoft.com/en-us/library/d5x73970.aspx
Generic Methods
• Concurrency
• Doing more than one thing at a time
• Multithreading
• A form of concurrency that uses multiple threads of execution
• Parallel Processing
• Doing lots of work by dividing it up among multiple threads that run
concurrenty
• Asynchronous Programming
• A form of concurrency that uses futures or callbacks to avoid
unncecesary threads
Concurrency: Benefits
• Multithreading is one form of concurrency, but certainly not the only one
• Each process has multiple threads in it, and each of those threads can be
doing different things simultaneously
• Each thread has its own independent stack but shares the same memory
with all the other threads in a process
Multithreading
• Parallel programming should be used any time you have a fair amount of
computation work that can be split up into independent chunks of work
• Parallel programming increases the CPU usage temporarily to improve
throughput; this is desirable on client systems where CPUs are often idle but is
usually not appropriate for server systems
• Parallel processing (or parallel programming) uses multithreading to maximize
the use of multiple processors
• Modern CPUs have multiple cores, and if there’s a lot of work to do, then it makes
no sense to just make one core do all the work while the others sit idle
• Parallel processing will split up the work among multiple threads, which can each
run independently on a different core
• Parallel processing is one type of multithreading, and multithreading is one type
of concurrency
Asynchronous Programming
• Foreground
• Foreground threads are those threads which keeps running until it
finishes his work even if the Main method thread quits its process
• Lifespan of foreground threads does not depends on main thread
• Background
• Background thread is just opposite of foreground thread here
background thread quits its job when main thread quits
• Here lifespan of background threads depends on main thread
Multithreading