Foundation
Reference:https://www.sololearn.com/Play/CSharp
Inheritance
Base class
class Animal {
public int Legs {get; set;}
public int Age {get; set;}
}
Derived class
class Dog : Animal {
public Dog() {
Legs = 4;
}
public void Bark() {
Console.Write("Woof");
}
}
Invoke
static void Main(string[] args) {
Dog d = new Dog();
Console.WriteLine(d.Legs);
// Outputs 4
d.Bark();
//Outputs "Woof"
}
Protected
Public members may be accessed from anywhere outside of the class, while access to private members is limited to their class.
The protected access modifier is very similar to private with one difference; it can be accessed in the derived classes. So, a protected member is accessible only from derived classes.
class Person {
protected int Age {get; set;}
protected string Name {get; set;}
}
class Student : Person {
public Student(string nm) {
Name = nm;
}
public void Speak() {
Console.Write("Name: "+Name);
}
}
static void Main(string[] args) {
Student s = new Student("David");
s.Speak();
//Outputs "Name: David"
}
error
static void Main(string[] args) {
Student s = new Student("David");
s.Name = "Bob"; //Error
}
sealed
A class can prevent other classes from inheriting it, or any of its members, by using the sealed modifier.
sealed class Animal {
//somecode
}
class Dog:Animal{} //Error
Derived Class Constructor & Destructor
class Animal{
public Animal(){
Console.WriteLine("Animal created");
}
~Animal(){
Console.WriteLine("Animal deleted");
}
}
class Dog:Animal{
public Dog(){
Console.WriteLine("Dog created");
~Dog()
Console.WriteLine("Dog deleted");
}
}
static void Main(string[] args){
Dog d = new Dog();
}
Polymorphism
Virtual - Base
class Shape {
public virtual void Draw() {
Console.Write("Base Draw");
}
}
Override - Derived
class Circle : Shape {
public override void Draw() {
// draw a circle...
Console.WriteLine("Circle Draw");
}
}
class Rectangle : Shape {
public override void Draw() {
// draw a rectangle...
Console.WriteLine("Rect Draw");
}
}
Invoke
static void Main(string[] args) {
Shape c = new Circle();
c.Draw();
//Outputs "Circle Draw"
Shape r = new Rectangle();
r.Draw();
//Outputs "Rect Draw"
}
Abstract Classes
features
- An abstract class cannot be instantiated.
- An abstract class may contain abstract methods and accessors.
- A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
You cannot create objects of a class containing an abstract method, which is why the class itself should be abstract.
abstract class Shape {
public abstract void Draw();
}
class Circle : Shape {
public override void Draw() {
Console.WriteLine("Circle Draw");
}
}
class Rectangle : Shape {
public override void Draw() {
Console.WriteLine("Rect Draw");
}
}
static void Main(string[] args) {
Shape c = new Circle();
c.Draw();
//Outputs "Circle Draw"
}
Interfaces
An interface is a completely abstract class, which contains only abstract members.
All members of the interface are by default abstract, so no need to use the abstract keyword.
Interfaces can have public(by default),private and protected members.
It is common to use the capital letter I as the starting letter for an interface name.
Interfaces can contain properties, methods, etc. but cannot contain fields (variables).
When a class implements an interface, it must also implement, or define, all of its methods.
The term implementing an interface is used (opposed to the term “inheriting from”) to describe the process of creating a class based on an interface. The interface simply describes what a class should do. The class implementing the interface must define how to accomplish the behaviors.
The syntax to implement an interface is the same as that to derive a class:
public interface IShape {
void Draw();
}
class Circle : IShape {
public void Draw() {
Console.WriteLine("Circle Draw");
}
}
static void Main(string[] args) {
IShape c = new Circle();
c.Draw();
//Outputs "Circle Draw"
}
Note, that the override keyword is not needed when you implement an interface.
But why use interfaces rather than abstract classes?
A class can inherit from just one base class, but it can implement multiple interfaces!
Therefore, by using interfaces you can include behavior from multiple sources in a class.
To implement multiple interfaces, use a comma separated list of interfaces when creating the class: class A: IShape, IAnimal, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
string color = Console.ReadLine();
string equipment = Console.ReadLine();
Car car = new Car(color, equipment);
car.GetColor();
car.GetEquipment();
}
}
public interface IColor
{
void GetColor();
}
public interface IEquipment
{
void GetEquipment();
}
//implement IColor & IEquipment interfaces
public class Car:IColor,IEquipment
{
public string color;
public string equipment;
public Car(string color, string equipment)
{
this.color = color;
this.equipment = equipment;
}
//reimplement this method
public void GetColor()
{
Console.WriteLine("Color: {0}",color);
}
//reimplement this method
public void GetEquipment()
{
Console.WriteLine("Equipment: {0}",equipment);
}
}
}
Default Implementation
Default implementation in interfaces allows to write an implementation of any method. This is useful when there is a need to provide a single implementation for common functionality.
Let’s suppose we need to add new common functionality to our already existing interface, which is implemented by many classes. Without default implementation (before C# 8), this operation would create errors, because the method we have added isn’t implemented in the classes, and we would need to implement the same operation one by one in each class. Default implementation in interface solves this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
public interface IShape {
void Draw();
void Finish(){
Console.WriteLine("Done!"); //写了一个完整的Method
}
}
class Circle : IShape {
public void Draw() {
Console.WriteLine("Circle Draw");
}
}
static void Main(string[] args)
{
IShape c = new Circle();
c.Draw();
c.Finish();
}
}
}
We added the Finish() method with default implementation to our IShape interface and called it without implementing it inside the Circle class.
Methods with default implementation can be freely overridden inside the class which implements that interface.
Nested Classes
C# supports nested classes: a class that is a member of another class.
class Car {
string name;
public Car(string nm) {
name = nm;
Motor m = new Motor();
}
public class Motor {
// some code
}
}
Namespaces
Namespaces declare a scope that contains a set of related objects. You can use a namespace to organize code elements. You can define your own namespaces and use them in your program.
The using keyword states that the program is using a given namespace.
For example, we are using the System namespace in our programs, which is where the class Console is defined:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi");
}
}
}
Without the using statement, we would have to specify the namespace wherever it is used:
System.Console.WriteLine("Hi");
Practice
using System;
using System.Collections.Generic;
namespace Code_Coach_Challenge
{
class Program
{
static void Main(string[] args)
{
Draw pencil = new Draw();
Draw brush = new Brush();
Draw spray = new Spray();
pencil.StartDraw();
brush.StartDraw();
spray.StartDraw();
}
}
/*
Draw => "Using pencil"
Brush => "Using brush"
Spray => "Using spray"
*/
public interface IDraw
{
void StartDraw();
}
class Draw : IDraw
{
public virtual void StartDraw()
{
Console.WriteLine("Using pencil");
}
}
//inherit this class from the class Draw
class Brush:Draw
{
//implement the StartDraw() method
public override void StartDraw() {
Console.WriteLine("Using brush");
}
}
//inherit this class from the class Draw
class Spray:Draw
{
//implement the StartDraw() method
public override void StartDraw() {
Console.WriteLine("Using spray");
}
}
}