ASP.Net MVC 5 – Attribute Based Routing Part 3

Overriding a Route Prefix

We can override a Route prefix applied at controller level by using “~” character with Route attribute

[RoutePrefix("Sports")]

public class HomeController : Controller

{

[Route("{category}")]

public ActionResult SomeAction(string category)

{

ViewBag.category = category;

return View();

}

[Route("~/Games/{category}/edit")]

public ActionResult SomeMoreAction(string category)

{

ViewBag.category = category;

return View();

}

}

Continue reading

ASP.Net MVC 5 – Attribute Based Routing Part 5

Route Constraints

We can restrict the parameters used in routes via Route constraints. There are many route constraints provided by Asp.net mvc and you can create your custom constraints as well:

Examples:

//Sports/SomeAction/category

[Route("{category:int}",Name = "Test")]

public ActionResult SomeAction(int category)

{

ViewBag.category = category;

return View();

}

Continue reading

OOPS in C# – Interface polymorphism Part 5

Scenario:-
Different implementations for both interface and class.

    public interface MyInterface1
    {
        int foo();
    }

    public interface MyInterface2
    {
        int foo();
    }
    public class MyClass : MyInterface1, MyInterface2
    {
        int a = 10;
        #region MyClass Members

        public int foo()
        {
            return a;
        }

        #endregion

        #region MyInterface1 Members

        int MyInterface1.foo()
        {
            return 11;
        }

        #endregion

        #region MyInterface2 Members

        int MyInterface2.foo()
        {
            return 12;
        }

        #endregion
    }



In this case

            MyClass obj = new MyClass();
            MyInterface1 obj2 = (MyInterface1)obj;
            MyInterface2 obj3 = (MyInterface2)obj;
            obj.foo(); //Returns 10
            obj2.foo(); //Returns 11
            obj3.foo(); //Returns 12

OOPS in C# – Class vs Struct

Class v/s Struct
1.) Class can have default constructor. Struct can only have parametrized constructors.
2.) Class is a reference type whereas Struct is a value type.
3.) Struct can’t inherit or implement whereas class can both implement interfaces and inherit interfaces.

We will start by simple interface implementation. Let’s create an interface with one method.

    public interface MyInterface
    {
        int foo();

    }

Now let’s first have a look at a simple class

    public class MyClass
    {
        int a;
    }



Continue reading

OOPS in C# – Interface polymorphism Part 4

Scenario:-
Interface polymorphism with multiple interfaces having same method.

    public interface MyInterface1
    {
        int foo();
    }

    public interface MyInterface2
    {
        int foo();
    }
    public class MyClass : MyInterface1, MyInterface2
    {
        int a = 10;

        #region MyInterface1 Members

        public int foo()
        {
            return a;
        }

        #endregion
    }
           MyClass obj = new MyClass();
            MyInterface1 obj2 = (MyInterface1)obj;
            MyInterface2 obj3 = (MyInterface2)obj;
            obj.foo(); //Returns 10 
            obj2.foo(); //Returns 10
            obj3.foo(); //Returns 10

OOPS in C# – Interface polymorphism Part 3

Scenario :-
Lets now discuss explicit implementation of interface be taking below example. In explicit implementation functions are qualifies with parent interface names as below:-

    public interface MyInterface
    {
        int foo();
    }

    public class MyClass : MyInterface
    {
        int a = 10;

        #region myinterfacse Members

        int MyInterface.foo()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

Now there will be some errors in code of scenario 10 as below

   MyClass obj = new MyClass();
            MyInterface obj2 = (MyInterface)obj;
            obj.foo(); //Compile time error.No class member foo
            obj2.foo(); //Returns 10

OOPS in C# – Interface polymorphism Part 2

Scenario :-

Private Implementation of Interface member.
Remember OOPS is a bit tricky and if you write MyClass definition as below then

 

    public interface MyInterface1
    {
        int foo();
    }
    public class MyClass : myinterfacse
    {
     int a=10;
     int foo()
      {
       return a;
      }
    }

It will give a compile time error.
Error 1 ‘WindowsFormsApplication3.Program.MyClass’ does not implement interface member ‘WindowsFormsApplication3.Program.myinterfacse.foo()’. ‘WindowsFormsApplication3.Program.MyClass.foo()’ cannot implement an interface member because it is not public.

OOPS in C# – Interface polymorphism Part 1

Scenario 10:-
First we will discuss implicit implementation of interface.

    public interface MyInterface
    {
        int foo();
    }

    public class MyClass : MyInterface
    {
        private int a = 10;

        public int foo()
        {
            return a;
        }
    }
    MyClass obj = new MyClass();
    MyInterface obj2 = (MyInterface) obj;
    obj.foo(); //Returns 10
    obj2.foo(); //Returns 10

Here foo is acting both as a class member as well as interface member.

OOPS in C# – Struct

Struct in .Net:-

1.) Struct are value types.
2.) Struct in .Net are sealed i.e. they cannot be inherited.
3.) The members of a Struct are by default private and not public.
4.) Struct can have Main method also just like a class.
5.) Struct can even have a constructor but Struct can only have parametrized constructors. In early days Struct had default constructors but they were re-moved due to some reasons known only to creators ? Continue reading

OOPS In C# – Interface

Interfaces:-
In layman’s terms we can say that interface is a pure abstract class with all methods abstract and no data members.
Interview definition:-
Interface is a contract between two parties. The contract is that all the parties implementing interfaces must implement all the methods declared in interfaces.
Abstract class V/s Interfaces:-


1.) Abstract class can contain data members. Interfaces can’t.
2.) Abstract class can contain non abstract methods too. Interfaces can’t.
3.) Default access specifier is abstract class is private but we can have other access specifiers also. Interfaces methods are by default public; you can’t apply any access specifier; not even public.
4.) In interfaces members are by default abstract you can’t even specify abstract keyword.
5.) Abstract class can implement interfaces. Interfaces can’t inherit classes but can implement other interfaces also.
6.) You can inherit from a single base class but you can implement multiple interfaces.
7.) Abstract classes are created using abstract keyword. Interfaces are created using interface keyword. Continue reading

OOPS In C# – Inheritance Part 3

Scenario:-
Function present in both base class and child class. This concept is called Method hiding. See the sample code below:

    public class MyClass
    {
        public void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public new void foo()
        {
            Console.WriteLine("Child version of foo");
        }
    }



Continue reading

OOPS In C# – Inheritance Part 4

Scenario:-
Function present in both base class and child class but this time virtual in base class. See the sample code below:

    public class MyClass
    {
        public virtual void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public new void foo()
        {
            Console.WriteLine("Child version of foo");
        }
    }





Continue reading

OOPS in C# – Polymorphism Part 1

Polymorphism means one thing exists in many forms of its own with each form having different implementation of its own.
Polymorphism can be compile time or run-time.

Compile time polymorphism includes:-
1.) Function Overloading
2.) Operator Overloading

Function Overloading:-
   Function overloading depends on:-

1.) Variable Number of parameters to function
2.) Order of parameters to function
3.) Data type of parameters
Function overloading doesn’t depend on:-
1.) Return type of function
2.) Name of parameters Continue reading

OOPS in C# – Polymorphism Part 3

6.) Overloading in case of inheritance

public class MyBaseClass
    {
        public void foo(int i)
        {
            Console.WriteLine("The is one parameter function which takes 
int as parameter and value of i is {0}."
, i); } } public class MyChildClass : MyBaseClass { public void foo(string s) { Console.WriteLine("The is one parameter function which takes
string as parameter and value of s is {0}."
, s); } }

Continue reading