Types of variables in c#

value and reference type             The c# support two major variable types, they are “Value type and Reference type”. Int, Float, Decimal , Char , etc are the value types and Classes, , Delegates , Interfaces , Arrays , etc are the Reference types.

Now we have seen c# has two different types of variable types. These types must have some difference so lets see how this value and reference type differs.

Value type and Reference type

I hope everyone knows, when ever we initialize a variable there is a memory allocation will happens, we should know this to know about the difference of value and reference types.

Value type

  • Value type variable store data directly into the memory location.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data will affect only to the variable we try to change , other variable remain the old value.
  • Value type data will be saved in Stack memory.

Reference type

  • Reference type variable does not save data directly to the memory location. it stores the reference of the actual data.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data affect every copy of the object.
  • Reference type data will be saved in Heap memory


using System;

class RefClass
    public int Value = 0;

class TestProgram

static void Main() {

    int valtype1 = 0;
    int valtype2 = valtype1;
    valtype2 = 999;
    RefClass refObj1 = new RefClass();
    RefClass refObj2 = refObj1;
    refObj2.Value = 999;
    Console.WriteLine("Value type values: {0}, {1}", valtype1, valtype2);
    Console.WriteLine("Reference type values: {0}, {1}", refObj1.Value, refObj2.Value);




Values: 0, 999
Refs: 999, 999

Value type

check the above example valtype2 is assigned from the variable valtype1 and changing the data valtype2. but in the output the change has been reflected only to the changed variable.

Reference type

check the above example refObj2 is assigned from the object refObj1 and changing the data inside the object refObj2. The change we made was in refObj2 but in the out put the change applied in the refObj2 reflected refObj1.

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC

The requested page cannot be accessed

The error message “The requested page cannot be accessed because the related configuration data for the page is invalid in MVC”  is occurred due to invalid code in the web.config file. Invalid code in the sense you may given a invalid tag like for <connectionString> you may given <constr>. it makes the error we should use the current xml tag names.

Please see the below examples

Actual tag code for connection string


you may be made mistake like


See the above example the ‘S’ letter for the two example is different because the web.config file xml tags are case sensitive. it should follow the visual studio rules to avoid errors.

In web.config xml file we should specify the tags currently according to the visual studio needs.

if anything wrong in the web.config file you will get the error like

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC


How to use switch statement in c# ?

Switch statement is used to check a variable with a set of conditions called cases. We can check a variable whether the value satisfies certain conditions.
For example below I have checked a variable is A, B or C. in some case we need to do some operations according to the check then we can go for the switch case.

Syntax for switch statement in c#

switch(expression_data) {
case condition1 :
break; /* optional */
case condition2 :
break; /* optional */

default : /* Default is optional */

Example for switch statement in c#

using System;
namespace FindVariable
class Program
static void Main(string[] args)
char character_variable = 'B';

switch (character_variable)
case 'A':
Console.WriteLine("Variable has value A");
case 'B':
Console.WriteLine("Variable has value B");
case 'C':
Console.WriteLine("Variable has value C");

Console.WriteLine("The variable not have the chartectors A,B and C");
Console.WriteLine("The variable has value{0}", character_variable);


Variable has value B
The variable has value B

How to concat 2 strings without using any inbuilt string functions?

String is a character array.Find the length of the first character array, and find the length of the second character array, using a loop concat both strings.

Sample C# code :

string variable1 = “String concat”;
string variable2 = “example.”;
int length1 = variable1.Length;
int length2 = variable2.Length;
char[] concat = new char[length1 + length2];
for (int i = 0; i < length1; i++)
concat[i] = variable1[i];
for (int i = 0; i < length2; i++)
concat[i + length1] = variable2[i];

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.

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


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


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


  • 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


  • 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

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.

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


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


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.


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.


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 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.


Refer for more OOPS Concepts

What is boxing and unboxing in c#?

In c# type system are mainly of 3 types

  • Value type
  • Reference type
  • Pointer type


The operation of converting a value type to  reference type is called BOXING

	int Val = 1;
 	Object Obj = Val; //Boxing
  • The first line we created a Value Type Val and assigned a value to Val
  • The second line , we created an instance of Object Obj and assign the value of Val to Obj.

From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.


The operation of converting a   reference type to  value type  is called UNBOXING

 	int Val = 1;
 	Object Obj = Val; //Boxing
 	int i = (int)Obj; //Unboxing
  • The first two line shows how to Box a Value Type .
  • The third line (int i = (int) Obj)  means extracting the value type from object
  • It means  converting value of a reference  type into a value of a Value type

This  operation is called Unboxing.