Anda di halaman 1dari 41

Unrestricted

Components of CLR

• Common Language Specification (CLS)


• Common Type System (CTS)
• Garbage Collection (GC)
• JIT
Class Example
public class Car
{
public string Brand;
private string vid;
private int gear;
Car(string brand, string vid, int gear)
{
this.Brand = brand;
this.vid = vid;
this.gear = gear;
}
public void Drive()
{ Console.WriteLine(“vroom”); }
}
First C# Program

using System;

class Hello {
static void Main( ) {
Console.WriteLine("Hello world");
Console.ReadLine(); // Hit enter to finish
}
}
Interfaces example
An interface defines a contract
An interface is a type
Contain definitions for methods,
public interface IDelete {
properties,
void Delete();
}
public class TextBox : IDelete { Interfaces provide no implementation
When a class or implements an
public void Delete() { ... }
interface it must provide the
} implementations
public class ImageBox : IDelete {
public void Delete() { ... }
}
OOP Object oriented programming–
Encapsulation

When classes are defined, programmers can specify that


certain methods or state variables remain hidden inside the
class.

These variables and methods are accessible from within the class,
but not accessible outside it.

The combination of collecting all the attributes of an object into a


single class definition, combined with the ability to hide some
definitions and type information within the class, is known as
encapsulation.
Access Modifiers

Public
Accessible anywhere
Protected
Accessible within its class and by derived class instances
Private
Accessible only within the body of the class
(Or anywhere if you use reflection)
Internal
Intuitively, accessible only within this program (more
specific definition here)
The default, but you should generally pick public or private
instead
Abstraction

Abstraction can be achieved using abstract classes in C#.


C# allows you to create abstract classes that are used to provide a partial class implementation of an
interface. Implementation is completed when a derived class inherits from it. Abstract classes contain
abstract methods, which are implemented by the derived class. The derived classes have more specialized
functionality.

The following are some of the key points −


•You cannot create an instance of an abstract class
•You cannot declare an abstract method outside an abstract class
•When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
Abstract Class
An abstract class can contain either abstract methods or non abstract methods. Abstract members do not
have any implementation in the abstract class, but the same has to be provided in its derived class.

Members marked as abstract must be implemented by non-abstract classes that derive from
the abstract class.

abstract class Shape class Square : Shape


{ {
public abstract int GetArea(); int side;
} public Square(int n)
{
this.side = n;
}
public override int GetArea()
{
return side * side;
}
}
Inheritance
Classes can inherit from each other, which means the inheriting
class gets all of the behavior of the inherited class, also known
as the base class.
class SavingsAccount : Account
class Account { {
method acctNum() {…} method rate() {…}
method balance() {…} }
method deposit() {…}
method withdraw()
{…}
} class CheckingAccount : Account
{
method withdraw() {…}
}
Polymorphism

Polymorphism can be static or dynamic.

Static polymorphism, the response to a function is determined at the


compile time. In dynamic polymorphism, it is decided at run-time.

Dynamic Polymorphism
C# allows you to create abstract classes that are used to provide
partial class implementation of an interface. Implementation is
completed when a derived class inherits from it. Abstract classes
contain abstract methods, which are implemented by the derived
class.
Static Polymorphism
The mechanism of linking a function with an object during
compile time is called early binding. It is also called static
binding. C# provides two techniques to implement static
polymorphism. They are −
•Function overloading
•Operator overloading

class Printdata {
void print(int i) { Console.WriteLine("Printing int: {0}", i ); }
void print(double f) { Console.WriteLine("Printing float: {0}" , f);
} void print(string s) { Console.WriteLine("Printing string: {0}",
s); }
}
Dynamic Polymorphism

class Shape { class Rectangle: Shape {


protected int width, height;
public Shape( int a = 0, int b = 0) {
public Rectangle( int a = 0, int b = 0):
width = a; height = b; } base(a, b) { }
public virtual int area() public override int area ()
{ { Console.WriteLine("Rectangle class area
Console.WriteLine("Parent class area :"); :"); return (width * height);
return 0;
}
}
} }

Anda mungkin juga menyukai