Friday, December 26, 2014

Abstract-Classes

using System;

public abstract class Animal
{
    public abstract string GetName();
    abstract public int Speed { get; }
}



using System;

public class Cheetah : Animal
{
    public override int Speed
    {
        get
        {
            return 100;
        }
    }

    public override string GetName()
    {
        return "cheetah";
    }
}


using System;

public class Turtle : Animal
{
    public override int Speed
    {
        get
        {
            return 1;
        }
    }

    public override string GetName()
    {
        return "turtle";
    }
}


using System;

class AbstractClasses
{
    static void Main()
    {
        Turtle turtle = new Turtle();
        Console.WriteLine("The {0} can go {1} km/h ",
            turtle.GetName(), turtle.Speed);
        Cheetah cheetah = new Cheetah();
        Console.WriteLine("The {0} can go {1} km/h ",
            cheetah.GetName(), cheetah.Speed);

        System.Collections.Generic.List<int> l;
        System.DateTime dt;
    }
}


OUTPUT

The turtle can go 1 km/h

The cheetah can go 100 km/h

Polymorphism

using System;

abstract class Figure
{
    public abstract double CalcSurface();
}


using System;

class Circle : Figure
{
    public double Radius { get; set; }
   
    public override double CalcSurface()
    {
        return Math.PI * this.Radius * this.Radius;
    }
}

using System;

class Rectangle : Figure
{
    public double Width { get; set; }
    public double Height { get; set; }
   
    public override double CalcSurface()
    {
        return this.Width * this.Height;
    }
}

using System;

class Square : Figure
{
    public double Size { get; set; }
   
    public override double CalcSurface()
    {
        return this.Size * this.Size;
    }
}


using System;

class Polymorphism
{
    static void Main()
    {
        Figure[] figures = new Figure[] {
            new Square() { Size = 3 },
            new Circle() { Radius = 2 },
            new Rectangle() { Width = 2, Height = 3 },
            new Circle() { Radius = 3.5 },
            new Square() { Size = 2.5 },
            new Square() { Size = 4 },
        };

        foreach (Figure figure in figures)
        {
            Console.WriteLine("Figure = {0} surface = {1:F2}",
                figure.GetType().Name.PadRight(9,' '),
                figure.CalcSurface());
            Console.ReadLine();
        }
    }
}

OUTPUT

Figure = Square    surface = 9.00

Figure = Circle    surface = 12.57

Figure = Rectangle surface = 6.00

Figure = Circle    surface = 38.48

Class-Diagrams

using System;

struct Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y) : this()
    {
        this.X = x;
        this.Y = y;
    }
}

using System;

abstract class Shape
{
    protected Point Position { get; set; }

    public Shape(Point position)
    {
        this.Position = position;
    }
}


using System;

struct Color
{
    public byte RedValue { get; set; }
    public byte GreenValue { get; set; }
    public byte BlueValue { get; set; }

    public Color(byte redValue, byte greenValue, byte blueValue)
        : this()
    {
        this.RedValue = redValue;
        this.GreenValue = greenValue;
        this.BlueValue = blueValue;
    }
}




using System;

interface ISufraceCalculatable
{
    float CalculateSurface();
}




using System;

class Square : Shape, ISufraceCalculatable
{
    private float Size { get; set; }

    public Square(float size, int x, int y)
        : base (new Point(x,y))
    {
        this.Size = size;
    }

    public float CalculateSurface()
    {
        return this.Size * this.Size;
    }
}



using System;

class Rectangle : Shape, ISufraceCalculatable
{
    private float Width { get; set; }
    private float Height { get; set; }

    public Rectangle(float width, float height, int x, int y)
        : base (new Point(x,y))
    {
        this.Width = width;
        this.Height = height;
    }

    public float CalculateSurface()
    {
        return this.Width * this.Height;
    }
}


using System;

class FilledSquare : Square
{
    private Color Color { get; set; }

    public FilledSquare(float size, int x, int y, Color color)
        : base(size, x, y)
    {
        this.Color = color;
    }
}





Inheritance And Accessibility

using System;

public class Creature
{
    protected string Name { get; private set; }

    public Creature(string name)
    {
        this.Name = name;
    }

    private void Talk()
    {
        Console.WriteLine("I am creature ...");
    }

    protected void Walk()
    {
        Console.WriteLine("Walking, walking, walking ....");
    }
}


using System;

public class Mammal : Creature
{
    public int Age { get; set; }

    public Mammal(string name, int age) : base(name)
    {
        this.Age = age;
    }

    public void Sleep()
    {
        Console.WriteLine("Shhh! {0} is sleeping!", this.Name);
    }
}



using System;

public class Dog : Mammal
{
    public string Breed { get; private set; }

    public Dog(string name, int age, string breed) : base(name, age)
    {
        this.Breed = breed;
    }

    public void WagTail()
    {
        Console.WriteLine("{0} is {1} wagging his {2}-aged tail ...",
            this.Name, this.Breed, this.Age);
        //this.Walk(); // This will successfully compile - Walk() is protected
        //this.Talk(); // This will not compile - Talk() is private
        //this.Name = "Doggy"; // This will not compile - Name.set is private
    }
}


using System;

class InheritanceAndAccessibility
{
    static void Main()
    {
        Dog joe = new Dog("Sharo", 6, "labrador");
        joe.Sleep();
        Console.WriteLine("Breed: " + joe.Breed);
        Console.ReadLine();
        joe.WagTail();

     }
}

OUTPUT

Shhh! Sharo is sleeping!
Breed: labrador

Simple-Inheritance

using System;

public class Mammal
{
    public int Age { get; set; }

    public Mammal(int age)
    {
        this.Age = age;
    }
   
    public void Sleep()
    {
        Console.WriteLine("Shhh! I'm sleeping!");
    }
}


using System;

public class Dog : Mammal
{
    public string Breed { get; set; }

    public Dog(int age, string breed) : base(age)
    {
        this.Breed = breed;
    }

    public void WagTail()
    {
        Console.WriteLine("Tail wagging...");
    }
}

using System;

class SimpleInheritance
{
    static void Main()
    {
        Dog joe = new Dog(8, "Labrador");
        joe.Sleep();
        joe.WagTail();
        Console.WriteLine("Joe is {0} years old dog of breed {1}.",
            joe.Age, joe.Breed);
        Console.ReadLine();
    }
}



OUTPUT

Shhh! I'm sleeping!
Tail wagging...
Joe is 8 years old dog of breed Labrador.


Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...