Monday, 8 October 2018

Ref and out key word.


Ref and out key word :-

Ref and out key word use to passing the value as reference to the function,If you want to pass a variable as ref parameter you must need to initialize it, before you pass it as ref parameter to any method. Ref keyword will pass parameter as a reference this means when the value of parameter is changed in called method it get reflected in calling method also.


class Refkeyword
{
static void Main()
{
int i; // variable need to be initialized
i = 3;
Refsamplemethod(ref i);
Console.WriteLine(i);
}
public static void Refsamplemethod (ref int val1)
{
  val1 += 10;
}
}


Properties of ref keyword :-
  • ·         Must be initialized.
  • ·         Always return the single value.
  • ·         Ref keyword used to pass an argument in a method.


Out Key word :-
If you want to pass a variable as out parameter you don’t need to initialize it before you pass it as out parameter to method. Out keyword also will pass parameter as a reference but here out parameter must be initialized in called method before it return value to calling method .





class OutKeyword
{
static void Main()
{
int i,j; // No need to initialize variable
Outsample(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
public static int Outsample(out int val1, out int val2)
{
val1 = 5;
val2 = 10;
return 0;
}
}







Properties of ref keyword :-
  • ·         No need to initialize.
  • ·         Return the multiple value.
  • ·         Out  keyword used to pass an argument in a method.


Note : Out and Ref work differently at run time but both are same at compile time. So, if you want to create two functions with the same names and same parameters but the only difference is Out and Ref keyword, then it will give a compile time error. We can say overloading is not possible in case of  the same functions and they have only one difference, i.e. Ref and Out keyword.


Tuesday, 2 October 2018

checked and unchecked in c#


Basically Checked and unchecked are operators of C#.
Checked and Unchecked operators enforce CLR ( Common Language Runtime ) to handle stack overflow control.

These operators checks code block which can cause overflow in an application.

Checked operator
unchecked operator


Checked

The checked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The checked statement:

checked block

The statement block that contains the expressions to be evaluated in a checked statement.

The checked operator:

checked (expression)

The expression to be evaluated in a checked context. Notice that the expression must be in parentheses ( ).



Unchecked

The unchecked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The unchecked statement :

unchecked block

The statement block that contains the expressions to be determined in an unchecked statement.

The unchecked operator :

unchecked (expression)

The expression to be determined in an unchecked context. Notice that the expression must be in parentheses ( ).

Example
namespace Checked_Uncheckedkeyword
{

    class Program
    {

        public short a = 40000;
        public short b = 20000;
        public short c;
        public int Add()
      {
            try
            {
                c = checked((short)(a + b));

            }
            catch (System.OverflowException e)
            {

                System.Console.WriteLine(e.ToString());

            }
            return c;
        }
        public int Mul()
        {
            try
        {
                checked
                {
                    c = (short)(a * b);
                }
            }
            catch (System.OverflowException e)
            {
                System.Console.WriteLine(e.ToString());
            }
            return c;
        }
        public int Add_Unchecked()
        {
            try
            {
                c = unchecked((short)(a + b));

            }
            catch (System.OverflowException e)
            {

                System.Console.WriteLine(e.ToString());
            }
            return c;
        }
        public int Mul_Unchecked()
        {
            try
            {
                unchecked
                {
                    c = (short)(a * b);
                }
            }
            catch (System.OverflowException e)
          {
                System.Console.WriteLine(e.ToString());
            }

            return c;

        }

        static void Main(string[] args)
        {
            Program p = new Program();
            // For checked
            Console.WriteLine("Checked output value is: {0}", p.Add());
            Console.WriteLine("Checked output value is: {0}", p.Mul());
            // For Unchecked
            Console.WriteLine("Checked output value is: {0}", p.Add_Unchecked());

            Console.WriteLine("Checked output value is: {0}", p.Mul_Unchecked());
            Console.ReadKey(true);
        }
    }
}



Wednesday, 9 November 2016

Different Types of Temporary Tables

Different Types of Temporary Tables

SQL Server provides two types of temp tables based on the Scope and Behavior of the table.
  • Local Temp Table
  • Global Temp Table

Local Temp Table

Local temp tables are only available to the current connection for the user; and they are automatically deleted when the user disconnects from instances. Local temporary table name is stared with ("#") sign.

Global Temp Table

Global Temporary tables name starts with  ("##"). Once this table has been created by a connection, like a permanent table it is then available to any user by any connection. It can only be deleted once all connections have been closed.

Creating Temporary Table in SQL Server 2008

As I have already discussed, there are two types of temporary tables available. Here I am going to describe each of them.

Local Temporary Table

The syntax given below is used to create a local Temp table in SQL Server 2008:
CREATE TABLE #LocalTmpTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))
The above script will create a temporary table in tempdb database. We can insert or delete records in the temporary table similar to a general table like:
insert into #LocalTmpTable values ( 1, 'Dhirendra','Noida');
Now select records from that table:
select * from #LocalTmpTable
After execution of all these statements, if you close the query window and again execute "Insert" or "Select" Command, it will throw the following error:
Msg 208, Level 16, State 0, Line 1
Invalid object name '#LocalTmpTable'.
This is because the scope of Local Temporary table is only bounded with the current connection of current user.

Global Temporary Table

The scope of Global temporary table is the same for the entire user for a particular connection. We need to put "##" with the name of Global temporary tables. Below is the syntax for creating a Global Temporary Table: 
CREATE TABLE ##NewGlobalTmpTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))
The above script will create a temporary table in tempdb database. We can insert or delete records in the temporary table similar to a general table like:
insert into ##NewGlobalTmpTable values ( 1, 'Dhirendra','Noida');
Now select records from Globle temp table:
select * from ##NewGlobalTmpTable

Wednesday, 10 June 2015

Introduction of WCF

Introduction of  WCF

There are a number of existing Technology to building distributed applications. These are includes Web services, .NET Remoting, Message Queuing (MSMQ) . Windows Communication Foundation (WCF) unifies these into a single framework for building and consuming services. Microsoft originally introduced WCF as part of the .NET Framework 3.0 and has continued to enhance it for the .NET Framework 3.5 and Visual Studio 2008.

In this Section , you’ll see how to use WCF to build distributed applications. This first tutorial introduces the basics of building, hosting and calling a WCF service. Along the way, you will learn the basics of WCF, including the role of endpoints, which consists of addresses, bindings and contracts. You’ll also see a variety of techniques you can use for controlling the behavior of your services. (These tutorials assume that you have Visual Studio 2008 installed, along with the .NET Framework 3.5. You can choose to work in either Visual Basic or C#--the steps listed here call out specific differences between the languages, when necessary.)
WCF is a unified programming model for building service-oriented applications. Before you build a WCF service, you will explore what that means.

Wednesday, 3 June 2015

Basic Interview Questions in ASP.NET,C#.NET,SQL Server,.NET Framework


Difference between stored procedure and function in Sql Server.
1) Procedure can return zero or n values whereas function can return one value which is mandatory.
2) Procedures can have input, output parameters for it whereas functions can have only input parameters.
3) Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
4) Functions can be called from procedure whereas procedures cannot be called from function.
5) Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
6) We can go for transaction management in procedure whereas we can't go in function.
7) Procedures cannot be utilized in a select statement whereas function can be embedded in a select statement.
 Major Difference between in Abstract and Interface.
Abstract Class:

-Abstract class provides a set of rules to implement next class
-Rules will be provided through abstract methods
-Abstract method does not contain any definition
-While inheriting abstract class all abstract methods must be override
-If a class contains at least one abstract method then it must be declared as an “Abstract Class”
-Abstract classes cannot be instantiated (i.e. we cannot create objects), but a reference can be created
-Reference depends on child class object’s memory
-Abstract classes are also called as “Partial abstract classes”
-Partial abstract class may contain functions with body and functions without body
-If a class contains all functions without body then it is called as “Fully Abstract Class” (Interface)