09 July 2014

Abstract Class

In dynamic polymorphism (over riding) the object of class reused with polymorphism method which has called only in the runtime. The decision about function execution is made at run time. Method overloading, method overriding, hiding comes under this approach.


The mechanism of linking a function with an object at run time is called dynamic or late binding.


C# uses two approaches to implement dynamic polymorphism,

  1. Abstract Classes

  2. Virtual Function


A class can be consumed from other classes in two different approaches,

  1. Inheritance (Inherit & Consume).

  2. By creating the object and consume.


Abstract class: The class under which we declared abstract methods is known as abstract class and should be declared with abstract modifier.


An abstract class can contain abstract members as well as non-abstract members.


A Method without any method body is known as an Abstract method. It contains only the declaration of the method, it should be declared by using the abstract modifier. This incomplete (abstract methods) must be implemented in a derived class.


An abstract class cannot be instantiated directly. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited. If it is so, it is useless.


An abstract method is implicitly a virtual method. This is accomplished by adding the keyword abstract before the return type of the method. An abstract member cannot be static.


The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.


Use abstract classes when you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes. As it simplifies versioning, this is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)


So, abstract class defines a common base class for a family of types with a default behavior


For e.g. Again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods/ properties as abstract so they could be overridden by child classes.


An Abstract class can

  • Had instance variables (like constants and fields), constructors and destructor.

  • Can inherit from another abstract class or another interface.


An Abstract class cannot

  • Inherited by structures.

  • Support multiple inheritances.



  1. The concept of abstract method is nearly related with the concept of method overriding. Where in overriding parent class declared a method as virtual and child class re-implements that method by using the override keyword.

  2. In case of abstract method parent class method is abstract which has to be implemented by the child class by using the override keyword only.

  3. The method overriding re-implemented/overriding the method is optional in virtual methods. Where as in abstract methods implementing/overriding the method is mandatory.


abs n vir

04 July 2014

Difference between Response.Redirect() and Server.Transfer()

Response.Redirect() is used to navigate the user request between multiple webservers
whereas Server.Transfer() is used to navigate the user request within the webserver.


Response.Redirect() will not hide the Destination url address.
Server.Transfer() will hide the destination url address


Viewstate and hiddenfields data is collapsed in both cases of redirect or transfer


If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page which you can’t do with Response.Redirect, Instead you can use querystrings


Server.Transfer sends a request directly to the web server and the web server delivers the response to the browser. So it is faster since there is one less roundtrip. but again it all depends on your requirement.


Response.Redirect can be used for both .aspx and HTML pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.


Both Response.Redirect and Server.Transfer have the same syntax like:
Response.Redirect("login.aspx");
Server.Transfer("login.aspx");

Logic for two tables mismatched columns

create table staging (clientid int primary key,addressdetails varchar(250));
insert into staging
select 100,'hyderbad,india' union all
select 101,'banglore,india' union all
select 102,'banglore,india'

create table oltp (client_id int primary key,address_details varchar(250));
insert into oltp
select 104,'newyork,usa' union all
select 105,'chicago,usa' union all
select 106,'washington,usa'

select * from oltp where client_id in (select client_id from staging)
o/p:
It returns all 2nd table rows.. instead of raising error that column-name is not existing in table


It will fetch all the values from outer query as inner query referring the same column from outer query .If a column is referenced in a subquery that does not exist in the table referenced by the subquery's FROM clause, but exists in a table referenced by the outer query's FROM clause, the query executes without error. SQL Server implicitly qualifies the column in the subquery with the table name in the outer query.

With ties clause in sqlserver

SELECT TOP(6) WITH TIES COL
FROM (
SELECT 1 COL UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 4 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) A
ORDER BY COL












Answer:



1,2,3,3,4,4,4



Explanation:



Using TOP with "WITH TIES" give all matching values with the last TOP (n) rows in ORDER BY columns. In simple way, if you will specify top 3 then it will give result 1,2,3,3 as there are two same value in that column.

For top 5, it will give the result as 1,2,3,3,4,4,4 because top 5th value is 4. It will find same value in the column used in ORDER BY until the last row of the table.

That's why top 6 returned 1,2,3,3,4,4,4 as top 6th is 4 so it will look for value 4 in entire COL column values. If matches are found, then it will include all those as well.

Refs: http://msdn.microsoft.com/en-IN/library/ms189463(v=sql.90).aspx
http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/

Difference between a Function and a Stored Procedure

UDF can be used inline in SQL statements anywhere in the WHERE/HAVING/SELECT section, whereas Stored procedures cannot be. UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables. Inline UDF’s can be thought of as views that take parameters.




  • Procedure can return zero or n values whereas scalar-function can return one value which is mandatory.

  • Procedures can have input/output parameters for it whereas functions can have only input parameters.

  • Procedure allows select as well as DML statement in it whereas function allows only select statement in it.

  • Functions can be called from procedure whereas procedures cannot be called from function.

  • Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.

  • We can go for transaction management in procedure whereas we can't go in function.

  • Procedures cannot be utilized in a select statement whereas function can be embedded in a select statement.

  • UDF can have upto 1023 input parameters, Stored Procedure can have upto 21000 input parameters


Functions are computed values and cannot perform permanent environmental changes to SQL Server (i.e. no INSERT or UPDATE statements allowed). It returns error like “Invalid use of a side-effecting operator 'INSERT' within a function.”


Sometimes we face a question, why we can't execute stored procedure inside a function?
Answer:
1. Stored Procedure may contain DML statements.
2. Function can't contain DML statements.
So executing Function inside stored procedure will never break rule 1.
But executing stored procedure inside function may break rule no 2.

Get continuous sequence numbers in sql sever

with d as
(
select 1 as a
union all
select a+1 from d where a<10
)
select * from d --option (maxrecursion 0)

Output:
a
1
2
3
4
5
6
7
8
9
10

03 July 2014

Difference between hashtable and dictionary

1. HashTable will successfully return null for a non-existent item, whereas the Dictionary will throw an error.
2. Dictionary is faster than a Hashtable because there is no boxing and unboxing.
3. In Dictionary only public static members are thread safe, but all the members in a Hashtable are thread safe.
4. Dictionary is a generic type which means we can use it with any data type.
public void MethodHashTable()
{
Hashtable objHashTable = new Hashtable();
objHashTable.Add(1, 100); // int
objHashTable.Add(2.99, 200); // float
objHashTable.Add('A', 300); // char
objHashTable.Add("4", 400); // string

lblDisplay1.Text = objHashTable[1].ToString();
lblDisplay2.Text = objHashTable[2.99].ToString();
lblDisplay3.Text = objHashTable['A'].ToString();
lblDisplay4.Text = objHashTable["4"].ToString();
// ----------- Not Possible for HashTable ----------
//foreach (KeyValuePair<string, int> pair in objHashTable)
//{
// lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
//}
}

public void MethodDictionary()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
//dictionary.Add(1, -2); // Compilation Error

foreach (KeyValuePair<string, int> pair in dictionary)
{
lblDisp.Text = pair.Value + " " + pair.Key;
}
}

Web Application vs Web site

What is Web Application?


Web app’s are server-side app’s, which follow open standard protocols like http, ftp, smtp, xml…


Every web app that we develop is universally accessible, because of their open standard support. In order to develop a web application, we have to setup our server. The setup includes installing a server product called web-server. Web server is a software which runs as a service & which is responsible to maintain one or more websites.


Application that develop with open standards and which are hosted in a public network are called web applications. Any device and any OS can request web app and get results.


Today most of the app development is web development, because of accessibility. Any user who logs on to this public network can access web application no need of installing any software for accessing this type of app’s, in other words web app’s are universal marked by the http protocols, because they are ruled by w3c rules.


web application or web app is a client–server software application which the client (or user interface) runs in a web browser. web applications uses HTML and JavaScript, which are supported by a variety of web browsers.


It's choice of the people can go for web application or website we cannot say that which one is better because both is having advantages and disadvantages. Check below details for webaplication and website


Web Application




  1. If we create any class files / functions those will be placed anywhere in the applications folder structure and it is precomplied into one single DLL.

  2. In web application we have chance of select only one programming language during creation of project either C# or VB.NET.

  3. Whenever we create Web Application those will automatically create project files (.csproj or .vbproj).

  4. We need to pre-compile the site before deployment.

  5. If we want to deploy web application project we need to deploy only .aspx pages there is no need to deploy code behind files because the pre-compiled dll will contains these details.

  6. If we make small change in one page we need to re-compile the entire sites.

  7. Web applications primarily allow the user to perform actions. ex: google, yahoo


WebSite




  1. If we create any class files/functions those will be placed in ASP.NET folder (App_Code folder) and it's compiled into several DLLs (assemblies) at runtime.

  2. In website we can create pages in multi programming languages that means we can create one page code in C# and another page code in vb.net.

  3. Web Sites won’t create any .csproj/.vbproj files in project

  4. No need to recompile the site before deployment.

  5. We need to deploy both .aspx file and code behind file.

  6. If we make any code changes those files only will upload there is no need to re-compile entire site

  7. Websites are primarily informational. ex: cnn.com

Readonly vs Const variables in C#

ROvsCON

Nullable datatype in c#

int? ins = null;
ins = 4;
if(ins.HasValue)
{
int a = ins.Value;
}

Diff b.w declaring a variable and defining a variable in c#

Void getall(string s) // delaring variable
{
String k;  // define variable takes memory even with value or not
}

Find largest value of an array.

int[] values = new int[] {1,3,5,7,9,10,8,6,4,2};

int maxValue = int.MinValue;

foreach(int i in values) {

maxValue = i > maxValue ? i : maxValue;

}

Console.WriteLine("Largest = {0}", maxValue); // Outputs "Largest = 10"

AccessSpecifier Defaults




































































Default



Permitted declared accessibilities



namespace



public



none (always implicitly public)



enum



public



none (always implicitly public)



interface



public



none



class



private



All¹



struct



private



public, internal, private²



delegate



private



All¹



constructor



protected



All¹



interface member



public



none (always implicitly public)



method



private



All¹



field



private



All¹



user-defined operator



none



public (must be declared public)



¹ All === public, protected, internal, private, protected internal

² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

Current Directory in Windows Apps

File.AppendAllText(Path.Combine(Directory.GetCurrentDirectory(), "logfileAutoResults.txt"), log);

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Apppaths.xml")

Differences between an Abstract class and an Interface

1. Abstract classes can have implementations for some of its members, but the interface can't have implementation for any of its members.


2. Interfaces cannot have fields where as an abstract class can have fields.


3. An interface can inherit from another interface only and cannot inherit from an abstract class, where as an abstract class can inherit from another abstract class or another interface.


4. A class can inherit from multiple interfaces at the same time, where as a class cannot inherit from multiple classes at the same time.


5. Abstract class members can have access modifiers where as interface members cannot have access modifiers.


Another common C# Interview Question, that is commonly asked is, When do you choose interface over an abstract class or vice versa?
A general rule of thumb is, If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. So, when you have an interface, you can move your implementation to any class that implements the interface. Where as, when you have an abstract class, you can share implementation for all derived classes in one central place, and avoid code duplication in derived classes.

Boxing vs UnBoxing

Boxing and Unboxing is a  essential concept in .Net’s type system. With Boxing and Unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object. The basic differences are :-























 BoxingUnboxing
Definition:Boxing is the process of converting a value type to the reference type.Unboxing is the process of converting a reference type to value type

.
Type of Conversion:Implicit ConversionExplicit Conversion

 
Example:int i = 221;

object obj = i; //boxing
object obj = 213;

i = (int)obj ; // unboxing