DOTNET Programming Concepts Difference FAQs-1 1.Difference between hash table and arraylist S.

No 1 2 3 Arraylist Array List is a List Hash table Hash Table is a map

Here, we can only add items to Here, we can add data with the key the list Retrieving data using Arraylist is slower than Hashtable because If we want to find something in a arraylist we have to go through each value in arraylist. Retrieving by key in Hashtable is faster than retrieving in Arraylist because If we want to find something in a hashtable we dont have to go through each value in hashtable, instead search for key values and is faster.

2.Difference between Hash Table and Arrays S.No 1 2 3 Hash Table Array

Hash table stores data as Array stores only value name,value pair. To access value from hash In array, to access value , we need table, we need to pass name. to pass index number. We can store different type of In array ,we can store only similar data in hash table, say type of data. int,string etc.

3.Difference between Dictionary and Hashtable S.No 1 2 Dictionary Dictionary is a generic type In Dictionary we need to specify the types of both the key and the corresponding value.The value represents the actual object stored and the key represents a means to identify a particular object. Hashtable Hashtable is not generic type. Hashtable is a collection of name/value pairs that are organised on the basis of hash code of the key being specified.

3

In Dictionary public static Hashtable is thread safe for use by members are type safe but any multiple reader threads and a single

instance members are not type writing thread. safe. 4 We cannot use Dictionary with We can use Hashtable withWeb Web Services The reason is no Services web service standard supports generic standard. Dictionary is aster than Hashtable It is slower than dictionary because to because boxing is not required. retrieve a value we must cast it as its actual type, because it will be returned via object reference.

5

4.Difference between array and stack S.No 1 2 Array An array can dimensional be Stack multi- Stack is strictly one-dimensional

An array allows direct access With a stack, only the 'top' element to any of its elements is directly accessible; to access other elements of a stack, we must go through them in order, until we get to the one we want

5.Difference between Stack and Heap S.No 1 2 3 Stack Heap

Memory will be allocated at the Memory will be allocated at the run compile time. time. Here the memory is allocated by Here the memory is allocated by the the compiler. user. Memory will be allocated only in Memory will be allocated in sequential sequential locations. locations and non- sequential locations. The memory will also be deleted The memory must be deleted explicitly by the compiler. by the user. There is lot of chance of memory There is no chance of memory wastage. wastage if the memory is handled perfectly.

4 5

6.Difference between Array and ArrayList S.No 1 2 3 Array They are fixed length. ArrayList They are resizable and variable length can

They are compiled strong type They are flexible and collection. accommodate any data types.

Because arrays are of fixed size In arraylist lots of boxing and unboxing and strong type collection are done there for its performance is performance is faster. slower. Array is namespace in the System ArrayList is in the System.Collections namespace. Ex:ArrayList a_list=new ArrayList();

4 5

Ex:Char[] vowel=new Char[];

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/dotnet-programming-conceptsdifference.html Dotnet Programming Concepts Difference FAQs-2 1.Difference between for and foreach loop S.No For loop 1 Foreach loop

In case of for the variable In case of Foreach the of the loop is always be int variable of the loop while be only. same as the type of values under the array. The For loop executes the statement or block of statements repeatedly until specified expression evaluates to false. The Foreach statement repeats a group of embedded statements for each element in an array or an object collection.

2

3

There is need to specify We do not need to specify the loop bounds(Minimum, the loop bounds minimum or Maximum). maximum. example: example: using sytem; using sytem; class class1 class class1 { { static void Main() static void Main() { { int j=0; int j=0; for(int i=0; i<=10;i++) int[] arr=new int[] { {0,3,5,2,55,34,643,42,23}; j=j+1; foreach(int i in arr)

4

} Console.ReadLine(); } } }

{ j=j+1; } Console.ReadLine(); }

2. Difference between Covariance and Contravariance S.No Covariance 1 Converting from a broader type to a specific type is called co-variance.If B is derived from A and B relates to A, then we can assign A to B. Like A=B. This is Covariance. Co-variance is guaranteed to work without any loss of information during conversion. So, most languages also provide facility for implicit conversion. Contravariance Converting from a more specific type to a broader type is called contra-variance. If B is derived from A and B relates to A, then we can assign B to A. Like B= A. This is Contravariance. Contra-variance on the other hand is not guaranteed to work without loss of data. As such an explicit cast is required.

2

e.g. Converting from cat or dog to animal is called contrae.g. Assuming dog and cat variance, because not all inherits from animal, when features (properties/methods) you convert from animal of cat or dog is present in type to dog or cat, it is animal. called co-variance.

3

Example: class Fruit { } class Mango : Fruit { } class Program {

delegate T Func(); delegate void Action(T a); static void Main(string[] args) { // Covariance Func mango = () => new Mango(); Func fruit = mango; // Contravariance Action fr = (frt) => { Console.WriteLine(frt); }; Action man = fr; } } 4 Note: 1. Co-variance and contra-variance is possible only with reference types; value types are invariant. 2. In .NET 4.0, the support for co-variance and contravariance has been extended to generic types. No now we can apply co-variance and contra-variance to Lists etc. (e.g. IEnumerable etc.) that implement a common interface. This was not possible with .NET versions 3.5 and earlier.

3.Difference between IList and IEnumerable S.No IList 1 IList is used to access an element in a specific position/index in a list. IList is useful when we want to Add or remove items from the list. IList can find out the no of elements in the collection without iterating the collection. IList does not support filtering. IEnumerable IEnumerable is a forward only collection, it can not move backward and between the items. IEnumerable does not support add or remove items from the list. Using IEnumerable we can find out the no of elements in the collection after iterating the collection. IEnumerable supports filtering.

2

3

4

4.Difference between IEnumerable and IQueryable S.No IEnumerable 1 IEnumerable exists in System.Collections Namespace. IEnumerable is best to query data from inmemory collections like List, Array etc. While query data from database, IEnumerable execute select query on server side, load data inmemory on client side and then filter data. IQueryable IQueryable exists in System.Linq Namespace. IQueryable is best to query data from out-memory (like remote database, service) collections. While query data from database, IEnumerable execute select query on server side with all filters.

2

3

4

IEnumerable is suitable for IQueryable is suitable for LINQ to Object and LINQ LINQ to SQL queries. to XML queries. IEnumerable does not supports custom query. IEnumerable does not support lazy loading. Hence not suitable for paging like scenarios. Extension methods supports by IEnumerable takes functional objects. IQueryable supports custom query using CreateQuery and Execute methods. IQueryable support lazy loading. Hence it is suitable for paging like scenarios. Extension methods supports by IEnumerable takes expression objects means

5

6

7

expression tree. 8 IEnumerable Example MyDataContext dc = new MyDataContext (); IEnumerable list = dc.loyees.Where(p => p.Name.StartsWith("S")); list = list.Take(10); Generated SQL statements of above query will be : SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0 Note: In this query "top 10" is missing since IEnumerable filters records on client side

IQueryable Example MyDataContext dc = new MyDataContext (); IEnumerable list = dc.loyees.Where(p => p.Name.StartsWith("S")); list = list.Take(10); Generated SQL statements of above query will be : SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0]. [Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0 Note: In this query "top 10" is exist since IQueryable executes query in SQL server with all filters. 5.Difference between IEnumerable and IEnumerator S.No IEnumerable 1 IEnumerator

The IEnumerable interface IEnumerator provides two is a generic interface that abstract methods and a provides an abstraction for property to pull a particular

looping over elements. In addition to providing foreach support, it allows us to tap into the useful extension methods in the System.Linq namespace, opening up a lot of advanced functionality The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call. 2

element in a collection. And they are Reset(), MoveNext() and Current The signature of IEnumerator members is as follows: void Reset() : Sets the enumerator to its initial position, which is before the first element in the collection. bool MoveNext() : Advances the enumerator to the next element of the collection. object Current : Gets the current element in the collection

IEnumerable does not IEnumerator does remember remember the cursor state the cursor state i.e currently row which is iterating through IEnumerable is useful IEnumerator is useful when when we have only iterate we have to pass the iterator the value as parameter and has to remember the value

3

Reference: http://onlydifferencefaqs.blogspot.in/2012/07/dotnet-programming-conceptsdifference.html DOTNET Programming Concepts Difference FAQs-3 1.Difference between directCast and ctype in .NET S.No 1 2 DirectCast ctype

DirectCast is generally used to Ctype is generally used to cast cast reference types. value types. When you perform DirectCast Exceptions are not thrown while on arguments that don't match using ctype. then it will throw InvalidCastException. If you use DirectCast, you cannot convert object of one type into another. Type of the object at runtime should be same as the type that is specified in DirectCast. Consider the following example: Dim sampleNum as Integer Ctype can cast object of one type into another if the conversion is valid. Consider the following example: Dim sampleNum as Integer Dim sampleString as String sampleNum = 100 sampleString = CType(sampleNum, String)

3

Dim sampleString as String This code is legal and the Integer sampleNum = 100 100 is now converted to a string. sampleString = DirectCast(sampleNum, String) This code will not work because the runtime type of sampleNum is Integer, which is different from the specified type String. 4 To perform DirectCast between two different classes, the classes should have a relationship between them. Performance of DirectCast is better than ctype. This is because no runtime helper routines of VB.NET are used for casting. To perform ctype between two different value types, no relationship between them is required. If the conversion is legal then it will be performed. Performance wise, ctype is slow when compared to DirectCast. This is because ctype casting requires execution of runtime helper routines of VB.NET.

5

6

DirectCast is portable across Ctype is specific to VB.NET and it many languages since it is not is not portable. very specific to VB.NET

2.Difference between Convert.ToString() and object.ToString() S.No 1 Convert.ToString() object.ToString()

Convert.ToString(object) does object.ToString() give run time error not give any error in case of if object value is null object value is null

Example: object obj=null; string str=obj.ToTsirng(); //it will give run time error string str1=Convert.ToString(obj); // it will run, not give any error 3.Difference between String.Equals(string1,string2) and string1.Equals(string2) S.No 1 String.Equals(string1,string2 ) string1.Equals(string2)

String.Equals(string1,string2) If any of strings value is null, will not throw any error if any of string1.Equals(string2) will throw strings value is null runtime error

Example: try { string str1 = null;

string str2 = "abc"; bool chk; chk = str1.Equals(str2); // will give runtime error chk = String.Equals(str1, str2); //will not give any error } catch (Exception ex) { MessageBox.Show(ex.Message); } 4.Difference between catch(Exception objex) and catch() block S.No 1 catch(Exception objex) catch() block

catch(Exception objEx) will catch() will catch all types of catch only .net compliance exception i.e., both non-compliance exceptions . and .net compliance exceptions

5.Difference between "Convert" class and "Parse()" method S.No 1 Convert Class The Convert class is used to convert any value from any data type to another data type. This class consist of different methods for making conversions. Example: 1) char ch = Convert.ToChar("x"); -> string to character 2) float f = Convert.ToSingle(45); -> int to float 3) int x = Convert.ToInt(4.5f); -> float to int Parse() method The Parse method is used to convert only one string value to any other data type. Every data type is consisting of parse() method.

2

Example: 1) int x = int.Parse("600"); -> string to int 2) char ch = char.Parse("dnf"); -> string to character

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/dotnet-programming-conceptsdifference_8.html Difference Between ref and out parameters Difference between Ref and Out parameters S.No 1 Ref Out

Ref parameters are both input Out parameters are only for output and output - a value is passed - a value is passed out of a method into a method and a new value is passed out

2 3

An argument passed to ref out argument does not have to be parameter must be initialized explicitly initialized. Ref parameter may be written Out parameter must be written to to before returning from before returning from method method Ref parameter may be Out parameter may not be referenced inside method referenced inside method before before being written to being written to

4

Example for ref parameter : class Program { static void M1(ref int i) { i = 1; i += 1; } static void Main(string[] args) { int j=0; //Assigned M1(ref j); Console.Write(j); Console.Read(); } } The output is same: 2, but we have to assign the variable before used otherwise we will get an error. Example for out parameter : class Program { static void M1(out int i) { i = 1; i += 1; } static void Main(string[] args) { int j; //Not assigned M1(out j); Console.Write(j); Console.Read(); } }

} The output is : 2 Summary: 1. Both are treated differently @ runtime but same in the compile time, so can’t be overloaded. 2. Both are passed by references except ‘ref’ requires that the variable to be initialized before passed. Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-ref-andout.html Difference between Checked and Unchecked keywords in .NET Difference between Checked and Unchecked keywords in .NET S.No 1 Checked keyword The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions. i.e., Checked Key word is used to enforce the compiler to check the buffer overflow while operating arithmetic operators. Unchecked keyword The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions. i.e.,Unchecked Keyword is used to enforce the compiler to do not check that is there any buffer overflow or Not.

2

If there is any Buffer overflow If here any Buffer overflow is code will throw exception. occurred it will just truncate the value but not throw exception

Example for Checked keyword: static void Main(string[] args) { int _value1 = 1500056564; int _value2 = 1200046565; checked { // this Code will give exception BuferOverflow System.Console.WriteLine(_value1 * _value2); System.Console.Read(); } } Example for Unchecked keyword: static void Main(string[] args) { int _value1 = 1500056564;

int _value2 = 1200046565; unchecked { // This Code will Not Give Exception System.Console.WriteLine(_value1 * _value2); System.Console.Read(); } } Note: Both the Checked and Unchecked keywords control whether or not an exception will be thrown when an operation causes a number to overflow. Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-checkedand.html Difference between Clone and CopyTo methods in .NET Difference between Clone and CopyTo methods in .NET S.No 1 Clone Clone method returns a new array containing all elements copied from the array on which clone method is called. CopyTo CopyTo method copies elements of the array into an existing array. Elements will be copied into a specific index and its increments.

2

The array size of the Array size of the destination array destination array need not be has to be declared before mentioned. triggering copyTo method. If the array size declared is smaller to fit in elements, the array size will automatically grow based on the number of elements that are getting copied. Array size should be large enough to fit both existing array elements and the elements to be copied. If the size is smaller than the actual number of elements, then you will get the following error while performing CopyTo: "Destination array was not long enough. Check destIndex and length, and the array's lower bounds."

3

Here is an example of Clone method: class sampleClass{ public static void Main() { int[] array1 = new int[] {10,20,30}; int[] array2 = array1.Clone() as int[]; for(int i=0;iConsole.Write(array2[i]+" "); } }

} Output of this code will be: 10 20 30 Here is an example of CopyTo method: class sampleClass{ public static void Main() { int[] array1 = new int[] {10,20,30}; int[] array2 = new int[array1.Length]; array1.CopyTo(array2,0); for(int i=0;iConsole.Write(array2[i]+" "); } } } Output of this code will be: 10 20 30 Note: Both the Clone and CopyTo methods belong to System.Array namespace. Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-clone-andcopyto.html Difference between Int and Int32 Difference between Int and Int32 S.No 1 Int Int is a datatype which has following three types, Int16 Int32 Int64 Int32 Int32 is the alias of Int

Summary: Both int and int32 are same . So there is no real difference between them .To make it user friendly ,int32 is given alias name as int. Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-int-andint32.html Difference between Constructor and Destructor in C# Difference between Constructor and Destructor in C# S.No 1 Constructor It is a special type of method which will be executed Destructor It will be executed automatically while destroying object

automatically while creating an object 2 Constructor name must be same as class name with out return type Syntax: public class_name() { } These are overloadable These are used to initialize the variables to open files or database connection etc The name of destructor must be same as class name with a ~(tilde) prefix and with out return type and with out access specifiers Syntax: ~class_name() { } These are not overloadable It is used to de-allocate the memory

3

4 5

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-constructorand.html Difference between Boxing and Unboxing in C# Difference between Boxing and Unboxing in C# S.No 1 Boxing Meaning: Boxing is the process of converting a value type to the reference type. Type of Conversion: Implicit Conversion Example: int i = 1; object obj = i; //boxing Unboxing Meaning: Unboxing is the process of converting a reference type to value type. Type of Conversion: Explicit Conversion Example: object obj = 123; i = (int)obj ; // unboxing

2 3

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-boxing-andunboxing.html Difference between Implicit Conversion and Explicit Conversion Difference between Implicit Conversion and Explicit Conversion S.No 1 Implicit Conversion Implicit Conversion perform automatically in VB.NET, that Explicit Conversion In some cases we have to perform conversions , that is the compiler

is the compiler is taking care of the conversion and therefore does not require any special syntax in the source code.

does not automatically convert a type to another . These type of conversion is called Explicit conversion . An explicit conversion uses a type conversion keyword, eg.CType ,CInt ,CStr etc., With these conversion keywords we have to perform the Explicit Conversion

Example for Implicit Conversion: Dim K As Integer Dim Q As Double ' ... K = 432 ' Integer widens to Double, so Option Strict can be On. Q=K In the above example, Visual Basic .NET implicitly converts the value of K to singleprecision floating point before assigning it to Q. Example for Explicit Conversion: Dim K As Integer Dim Q As Double ' ... K = 432 ' Integer widens to Double, so Option Strict can be On. Q=K Q = Math.Sqrt(Q) ' Q had been assigned the value 432 from K. K = CInt(Q) ' K now has the value 21 (rounded square root of 432). In the above example, the CInt keyword converts the value of Q back to an integer before it is assigned to K Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-implicitconversion.html

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.