What is the difference between Managed code and Unmanaged code in .Net?

Managed code Vs Unmanaged code

Any code running under CLR(Common Language Runtime)is called Managed code and opposite is called unmanaged code.

Managed Code

When a programmer write code in .Net languages such as C# or VB, he is writing managed code. Because any code which executes under CLR is called managed code. Managed code is subjected to certain rules and has so many benefits. But the rules are so easily for a programmer as the compiler must generate an MSIL(Microsoft Intermediate Language) file which target CLR and use the .Net class libraries. While following certain rules, there are so many benefits like, modern memory management, the ability to mix languages(Use of VB library in C# programming or vice versa), more security support, version controlling and a clean way of software components interaction. This article is about Managed code Vs Unmanaged code

Unmanaged Code

Just opposite to managed code is called unmanaged code. Unmanaged code does not execute under CLR. We cannot write unmanaged code in C# or VB., because the run under CLR. But in windows, prior to the creation of .Net framwork, it uses unmanaged code. For Example, COM components, COM+ Services and Win32 API are examples for unmamaged codes. So it is possible to interact between managed code and unmanaged code in windows.

The view ‘Index’ or its master was not found or no view engine supports the searched locations

Problem

The view ‘Index’ or its master was not found or no view engine supports the searched locations. The following locations were searched:

  • ~/Views/Home/Index.aspx
  • ~/Views/Home/Index.ascx
  • ~/Views/Shared/Index.aspx
  • ~/Views/Shared/Index.ascx
  • ~/Views/Home/Index.cshtml
  • ~/Views/Home/Index.vbhtml
  • ~/Views/Shared/Index.cshtml
  • ~/Views/Shared/Index.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Solution

            The problem will occurs when we trying to access an action in the controller.  Consider the below action

public class HomeController : Controller
{
           public ActionResult Index()
           {
                 return View();
            }
}

The action index returns a view. Then it will check the corresponding view file in the following locations 

~/Views/Home/

 ~/Views/Shared/

Not only the location, it will check corresponding view with the action name that is Index is in our case, with following extensions

  • .aspx
  • .ascx
  • .aspx
  • .ascx
  • .cshtml
  • .vbhtml
  • .cshtml
  • .vbhtml

In the folder home or shared should contain a file with any of the extension above. 

After creating the view in the specified folder the controller will get the correct view, then view will be shown in the page and we can avoid this error. This is one of the error we face when we start mvc projects. But is a basic thing in mvc in .net, it will understood this with in some days after starting study of mvc in .net.

 

Explain asp.net page life cycle?

Asp.net page life cycle passes through some series of process during their life cycle. Following are the stages

Begin request

  • This event will show that it’s a new request
  • Object and variables are initialized in this stage
  • This event is raised on every request

Authenticate request

  • .net is ready to authenticate the user in this stage
  • Authentication code are inserted here
  • This request confirms the authentication

Authorize request

  • net is ready to authorize the user in this stage
  • Custom Authorization code can be done here
  • This request confirms the authorization of the user

Resolve request cache

  • Output cache directive is commonly used in asp.net for caching
  • After authorize request is finished, authorize will calls the cache request to serve the request.
  • During runtime asp.net can load data from cache if needed.
  • Any type of cache activity code can be written here

Map request handler

  • Checks the extension of the file
  • Checks the handler of the request

Acquire request state

  • Ready to acquire session state variables/information

Process request

  • Logic execution is done at this stage

Init

  • Control’s id is set at this stage
  • Dynamically add controls at this stage
  • Read control properties

Load

  • Controls are fully loaded at this stage
  • All the values are restored in this stage
  • “Ispostback” is used to avoid unnecessary state change

Render

  • Method of page and page objects
  • Html changes can be mad lastly in this section
  • This method generates client side html and scripts to display

Unload

  • This is to clean up the code like closing connection, closing opened files
  • Page objects are unloaded from the memory

Release request state

  • Save or update session variables are done at this stage

Update request cache

  • Update cache before end of the process

End request

  • Last stage end of the request

 

                               Image shows the page life cycle of an event in GET and POST condition

What is ADO.NET and its features?

ADO.NET 

  • It is one of the major component of .Net Framework.
  • It is designed to connect to databases like Oracle, MySQL, SQL Server, MS Access etc. and work with data stored in them

In ADO.net some namespaces are used to access database they are

  • Data – Contains all generic data access classes
  • Data.Common – Contains classes which are shared / overridden by data providers
  • Data.OleDb – OLE DB provider classes used to access database such as Oracle, MySQL, SQL Server and MS Access.
  • Data.SqlClient – Contains classes for SQL Server
  • Data.SqlTypes – Contains SQL Server data types

Different layers of ADO.Net

  • Database Access Layer
  • Business Logic Layer
  • Presentation Layer

Features of ADO.Net 

  • It provides some in built classes to connect databases like mysql,sql,oracle etc,
  • In built classes to do operations like insert,update,delete and select.
  • Integration is very tight with XML
  • Disconnected Data architecture for better performance

New features in ADO.NET Entity Framework

  • Presistence Ignorace
  • Lazy Loading
  • Self Tracking Entities
  • Model-First Development
  • Model-Defined Functions

Web form events available in ASP.NET?

The Web Form Events in the sequence of their execution –

  • Page_Init
    • First step in page life cycle.
    • This event will raise whenever page initialized.
    • All the controls in the page have been initialized.
    • This event can be used to read or initialize control properties.
  • Page_Load
    • This event occurs only after Page_Init event.
    • This event also will raise for every postback operation.
    • All control properties are loaded with information recovered from view state and control state.
  • Page_PreRender
    • Perform any updates before the output is rendered.
    • Any changes made to the state of the control in the prerender phase can be saved.
  • While changes made in the rendering phase are lost.
  • Page_PreRender is the last event you have a chance to handle prior to the page’s state being rendered into HTML.
  • Page_Unload
    • This event is used for cleanup code.
    • This event occurs for each control and then for the page.
    • Instances of classes, in other words objects.
    • Closing opened files.
    • Closing database connections.

Interface in OOPS

Introduction

Interface is introduced to achieve multiple inheritance. Multiple classes cannot be inherited in C#, because classes have definitions for members, so the compiler will get confused with members which to invoke.

Ex:

    public class A
    {
        public int Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
    }
    public class B
    {
        public int Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
    }
    public class C : A, B
    {
        // not possible in C#.
    }
    public class D
    {
        public void main()
        {
            A a = new A();
            B b = new B();
            C c = new C();

            // will return 12 (5.55+6.55=12.10, So while converting .10 will be ejected bcz less than .50)
            int x = a.Add(5.55, 6.55);

            // will return 13(5.55 will be taken as 6 bcz .55 is greater than .50 also 6.55 will be taken as 7 So 6 + 7 = 13) not possible in C#
            int y = b.Add(5.55, 6.55);

            // in this case the compiler cannot take Decision on which method to invoke, because Both A and B have Add and function differently.
            int z = c.Add(5.55, 6.55);
        }
    }

To avoid such situations, interface is used. In interface members does not have definition, the must have methods and properties can be declared. So the inheriting class can define the member as required. As in class the default Access modifier for the interface is internal. But the default Access modifier of the interface member is not like in class. In interface. The default Access modifier is PUBLIC, but in class it is PRIVATE.Interface

Implicit Implementation

    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }
    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        public int Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
    }

This is implicit implementation of interfaces. Even if both interfaces have “Add” method, single method definition will satisfy both interfaces.

    class D
    {
        void main()
        {
            iA ia = new C();
            // will point to “Add” method in class C.
            ia.Add(5.55, 6.55);

            iB ib = new C();
            // will point to “Add” method in class C.
            ib.Add(5.55, 6.55);

            C c = new C();
            // will point to “Add” method in class C.
            c.Add(5.55, 6.55);
        }
    }

So there is no confusion about pointing method.

Explicit Implementation

Even if you need to implement each interface method separately, it can be done by explicit implementation. But explicitly implemented method cannot be invoked directly from derived class. Because we cannot specify any access modifiers for explicitly implemented methods. It will be private by default. It can be either accessed by.

  1. Another method in derived class
  2. Creating object for interface type.
ANOTHER METHOD IN DERIVED CLASS
    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }

    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        // Explicit implementation of “iA” interface method
        int iA.Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
        // Explicit implementation of “iB” interface method
        int iB.Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
        // Exposing “iA” interface method through another public method
        public int AddBeforeConverting(double a, double b)
        {
            iA ia = this;
            return ia.Add(a, b);
        }

        // Exposing “iB” interface method through another public method
        public int AddAfterConvertion(double a, double b)
        {
            iB ib = this;
            return ib.Add(a, b);
        }
    }

Here we use different method names, so no confusion.

CREATING OBJECT FOR INTERFACE TYPE
    interface iA // internal interface
    {
        int Add(double a, double b); // public method.
    }

    interface iB
    {
        int Add(double a, double b);
    }
    class C : iA, iB
    {
        // Explicit implementation of “iA” interface method
        int iA.Add(double a, double b)
        {
            return System.Convert.ToInt32(a + b);
        }
        // Explicit implementation of “iB” interface method
        int iB.Add(double a, double b)
        {
            int x = System.Convert.ToInt32(a);
            int y = System.Convert.ToInt32(b);
            return x + y;
        }
    }
    class D
    {
        void main()
        {
            iA ia = new C();
            // will return 12 as it convert after addition
            ia.Add(5.55, 6.55);

            iB ib = new C();
            // will return 13 as it convert numbers before addition
            ib.Add(5.55, 6.55);

            C c = new C();
            // Cannot use like it, method won’t be available in the object.
            c.Add(5.55, 6.55);
        }
    }

Interface

  • Interface is defined with the keyword “interface”.
  • Interfaces are used in design level.
  • Object for interface cannot be created directly. But it can be created by calling derived class constructor(as did with abstract class).
  • Interfaces only can have declaration, but not definition

Ex:

Public interface iA
{
    String Father; // field is not allowed in interface
    String Name{get;set;} // property is allowed in interface
    Int Add(int a, int b);
}
  • Member fields cannot be used in interface.
  • Access modifiers cannot be used with interface members.
  • All the interface members must be implemented in the derived classes.

After reading Abstract class and Interface, most of the people will have a confusion on why we have both abstract class and interface, and where to use them, because both have many similar features. It has specific reasons to have both together.

  • When we have a normal base class to inherit, then we cannot inherit another abstract class too.
    class A
    {
        private string _name;
        public string GetName()
        {
            return _name;
        }
    }
    abstract class B
    {
        public abstract void SetName(string Name);
    }

    interface iC
    {
        void SetName(string Name);
    }
    class D : A, B
    {
        // This is not possible, because multiple inheritance on class is Not possible in C#
            }
    class D : A, iC
    {
        //This is possible. Here only one class is inherited and another one is interface
    }

In the above situation we need to use interface instead of abstract class.

  • When we are in need of some method declarations and some methods with method definition, then we have only one choice of Abstract class.(some function in a fixed manner and some function according to the consumer requirement on implementation).

There may be more reasons, so please refer the internet also. As said above, the abstract class and interface are used in the design level. So it is mostly used in design patterns.(if you wish to learn more about design patterns, can refer to internet) For more references about OOPS: MSDN – OOPS(C#)

Refer for more OOPS Concepts

Wcf

WCF                                                             

  • Microsoft has introduced WCF  for inter process communication. WCF let us to establish communication channels using
  • TCP
  • HTTP
  • MSMQ
  • IPC
  • Peer Network
  • Namespace “System.ServiceModel” is used for WCF.

Different ways to host WCF Service are

  • IIS
  • Self-Hosting
  • WAS

In WCF modes of communication between server and client are

  • One-Way
  • Request-Reply
  • Callback

Security features used in WCF 

  • Integrity
  • Confidentiality
  • Authentication
  • Authorization

The essential components of WCF

  • Service class
  • End point
  • Hosting Environment

How WCF works?

WCF will follow – “Software and Service Model”, in which all essential components are defined as services and this will be used by client program.

Difference between classic web services (ASMX) and WCF

Classic web service known as ASMX were using SOAP protocol for sending and receiving the messages over network and over HTTP protocol whereas WCF allows the communication to happen over any transport protocol.

Abstract classes in OOPS

Abstract Classes

  • Abstract classes are defined with the keyword ‘abstract’.
  • They are used in design level.
  • Object cannot be created to abstract classes directly. But object can be created to type of abstract class by calling derived class constructor
Abstract Class
Every animal have its specific type just like Wolf, Gold Fish, Dog etc. So assume as No object for Animal class, but inheriting Animal class to any other type of animal we can get the base behavior of animal in those type of animals.

Ex:

        abstract class A
        {
            public abstract int Add(int a, int b);
        }

        class B : A
        {
            public override int Add(int a, int b)
            {
                return a + b;
            }
        }

        class C
        {
            public C()
            {
                A a = new A();  // it is not possible, because there is no constructor for abstract class.
                A a = new B();  // This is possible, because B is not abstract class, so It has it’s default constructor.
                a.Add(10, 20);  // will return 30.
            }
        }
  • Abstract classes can have abstract methods and properties as well as non abstract members. But a normal class cannot contain abstract members.
  • An abstract member should be implemented in its derived classes.
  • An abstract method or property will not be having it’s definition/implementation in abstract class.
  • It is not mandatory to have at least single abstract member in an abstract class. We can have defined methods only in an abstract class.

Ex:

        public abstract class A
        {
            public string Add(string a, string b)
            {
                return a+b; // A normal method
            }
            public static int Add(int x, int y, int z)
            {
                return x + y + z;
            }
        }

Abstract class can have a normal method, but it cannot be used without inheritance. Because object cannot be created for abstract class. At the same time a static method can be used in abstract class and it can be used without inheritance. Because static members doesn’t need object to invoke.

From the above example, “public string Add(string a, string b)” method need inheritance to be invoked at the same time “Public static int Add(int x, int y, int z)” method can be used directly with class name

Ex:

        public abstract class A
        {
            // Abstract property.
            public abstract string FirstName { get; set; }
            // Abstract method.
            public abstract int Add(int a, int b);
            public string Add(string a, string b)
            {
                return a + b; // A normal method
            }
            public static int Add(int x, int y, int z)
            {
                return x + y + z;
            }
        }

        public class B : A
        {
            private string _FirstName;
            public override string FirstName
            {
                get { return _FirstName; }
                set { _FirstName = value; }
            }
            public override int Add(int a, int b)
            {
                return a + b;
            }
        }
        public class C
        {
            public C()
            {
                A b = new B();

                b.Add("Mathew ", "Markose"); //will return “Mathew Markose” as result
                A.Add(10, 20, 30); // will return 60 as result
            }
        }

Refer for more OOPS Concepts

what is javascript function?

      Javascript function is grouping a set of codes to perform particular operations. we will invoke or execute the javascript function according to our needs.

Javascript function syntax

            The javascript function can be created by using the keyword function followed by specifying function name then the arguments inside the parentheses , then we can open the curly brackets and can write function definition inside the curly brackets.

function function_name(parameter_1,parameter_2, ……….)
{
// function_definition
}

  • function is a keyword used to define functions.
  • function_name is the name we will define for the function, any name we can use and it is user defined. the function name can include letters, numbers and underscore.
  • parameter_1, parameter_2 are the parameters list. we can specify multiple parameters by separating by the comma.
  • function_definition is the code block , the code we need to execute when the function is invoked.

Example of javascript function

// The function returns the sum of number1 and number2
function AddTwoNumbers(number1,number2)
{
return number1+number2;
}

above you can see basic javascript example for finding the sum of two numbers.

  • function is the keyword.
  • AddTwoNumbers is the function name.
  • number1 and number2 are the parameters.
  • “return number1+number2” , add two number and return the sum.

How to call javascript function

        we can call javascript function by just specifying the function name and the arguments inside the parentheses.

Javascript function calling example

var sum=AddTwoNumbers(40,60);

  • “AddTwoNumbers” is the method name
  • “40,60” are the parameter values.
  • “sum” is just a variable used to store the function returning value

Finally after executing the above function (“var sum=AddTwoNumbers(40,60);”) we will get 100 as result in the “sum” variable.

Access Modifiers in C#

Access Modifiers / Access Specifiers are keywords used in object oriented programming to restrict access to classes or class members like methods and variables. It is a part of programming syntax to achieve the encapsulation of components. There are different kinds of Access Modifiers with different kind of behaviors. Access Modifiers in C# are

  • Public
  • Private
  • Internal
  • Protected
  • Protected Internal
  • Sealed

Public

It can be accessed from anywhere as object or for inheritance. In C# public is default for interfaces

Private

It is only accessible inside the same class. When an object of the class is created, private methods and variables won’t be accessible. In the case of inheritance, private properties and methods are inherited, but won’t be accessible. Nothing can be created as private direct under namespace. Which means a class direct under namespace cannot be given private modifier. Only nested class can be assigned as private. In C# private is default for class members.

Internal

This is just like public, but scope only inside the assembly. Outside the assembly, It won’t be available for inheritance as well as for object. In C#, internal is default for a class direct under namespace.

Protected

It is only available for inheritance, will not be accessible from object of the class. Which means, a protected member can be used only inside the child class not directly from parent class. Scope for inheritance is anywhere:- accessible from outside of the assembly also possible. A class direct under namespace cannot be created as protected.

Protected Internal

This is the union of Protected and Internal. It can be accessed from object only inside the assembly, but accessible for inheritance globally. As protected, no element direct under namespace can be created as protected internal.

Sealed

Sealed modifier is used to protect the class or class member from being inherited. A class with sealed modifier cannot be inherited, but it can inherit another class. But in case of class members, it can only be used to overridden members. Which means a new member cannot be create as sealed. So it protect class members fro multi level inheritance.

    class A
    {
        String name;
    }
    sealed class B : A
    {
        int Age; //this is possible B can inherit A
    }
    class C : B
    {
        DateTime DOB;   // This will return error on compile time, bcz, sealed class B cannot be inherited.
    }

Summary

Refer for more OOPS Concepts