Posts

Showing posts with the label C#

Difference between array, arraylist, List, Hashtable, Dictionary and SortedList in c#

Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performance that a list, a list would be far more flexible since you don't need to know the required size initially. Array - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insertion, retrieve and speed. ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET List - one of my favorites - can be used with generics, so you can have a strongly typed array, e.g. List . Other than that, acts very much like ArrayList. Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs. Dictionary - same as above only strongly typed via generics, such a...

Difference between array, arraylist, List, Hashtable, Dictionary and SortedList in c#

Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performance that a list, a list would be far more flexible since you don't need to know the required size initially. Array - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insertion, retrieve and speed. ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET List - one of my favorites - can be used with generics, so you can have a strongly typed array, e.g. List . Other than that, acts very much like ArrayList. Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs. Dictionary - same as above only strongly typed via generics, such a...

Difference between IEnumerable VS IQueryable in c#

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation. Let’s see both the features and take the advantage of both the features to boost your LINQ Query performance. IEnumerable IEnumerable exists in System.Collections Namespace. IEnumerable can move forward only over a collection, it can’t move backward and between the items. IEnumerable is best to query data from in-memory collections like List, Array etc. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. IEnumerable is suitable for LINQ to Object and LINQ to XML queries. IEnumerable supports deferred execution. IEnumerable doesn’t supports custom query. IEnumerable doesn’t support lazy loading. H...

Difference between DataTable.Copy() Vs. DataTable.Clone() in C#

Image
Two major methods of DataTable in C#, One is Copy() and the other one is Clone() . Though these two sound identical but there are huge differences between these two. There are two things to copy or clone of a DataTable . These are structure and data . Copy and Clone are playing with these two. Let us create a DataTable first. DataTable dt = new DataTable(); dt.Columns.Add( " Id" ); dt.Columns.Add( " Name" ); dt.Columns.Add( " Email" ); dt.TableName = " MasterTable" ; // insert into DataTable dt.Rows.Add( " 1" , " Arka" , " arka@gmail.com" ); dt.Rows.Add( " 2" , " Anusua" , " anu@gmail.com" ); dt.Rows.Add( " 3" , " Sayantani" , " sayantani@gmail.com" );     DataTable.Copy() returns a DataTable with the structure and data of the DataTable . // Creating another DataTable to copy DataTable dt_copy = new DataTable(); dt.TableName = " CopyTable...

What is Common Language Runtime or CLR

As part of Microsoft's .NET Framework, the Common Language Runtime ( CLR ) is programming that manages the execution of programs written in any of several supported languages , allowing them to share common object-oriented class es written in any of the languages . CLR handles the compilation and execution of .NET programs. CLR uses JIT(Just in time)  and compiles the IL code to machine code and then executes.  Below are the list of responsibilities of Common Language Run-time. Garbage Collection Code Verification Code Access Security Intermediate language -to-native translators and optimizer’s

What is HashTable?

A Hashtable is a collection of key-value pairs. Entries in this are instance of DictionaryEntry type.  It implements IDictionary, ISerilizable, IDeserializable collback interface.

What is BitArray?

The BitArray collection is a composite of bit values. It stores 1 or 0 where 1 is true and 0 is false.  This collection provides an efficient means of storing and retrieving bit values.

What is ArrayList in c sharp?

ArrayList is a dynamic array. Elements can be added and removed from an arraylist at the runtime.  In this elements are not automatically sorted.

What is Array in C sharp?

An array is a collection of related instance either value or reference types. Array posses an immutable structure in which the number of dimensions and size of the array are fixed at instantiation. C# Supports Single, Mult dimensional and Jagged Array. Single Dimensional Array: it is sometimes called vector array consists of single row. Multi-Dimensional Array: are rectangular & consists of rows and columns. Jagged Array: also consists of rows & columns but in irregular shaped (like row 1 has 3 column and row 2 has 5 column)

What is a Constructor in c#?

It is the first method that are called on instantiation of a type. It provides way to set default values for data before the object is available for use. Performs other necessary functions before the object is available for use.

What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text.  Strings are immutable, so each time it’s being operated on, a new instance is created

What is an abstract class in C#

A class that cannot be instantiated. A concept in C++ known as pure virtual method.  A class that must be inherited and have the methods over-ridden.  Essentially, it’s a blueprint for a class without any implementation.

Benefits and Limitation of using Cookies?

Following are benefits of using cookies for state management :- No server resources are required as they are stored in client. They are light weight and simple to use Following are limitation of using cookies :- Most browsers place a 4096-byte limit on the size of a cookie,although support for 8192-byte cookies is becoming more common in the new browser and client-device versions available today. Some users disable their browser or client device’s ability to receive cookies, thereby limiting the use of cookies. Cookies can be tampered and thus creating a security hole. Cookies can expire thus leading to inconsistency. Below is sample code of implementing cookies :- Request.Cookies.Add(New HttpCookie(“name”, “user1”))  

Difference between dataset and datareader in c#

Following are some major differences between Dataset and Datareader DataReader provides forward-only and read-only access to data , while the DataSet object can hold more than one table (in other words more than one rowset) from the same data source as well as the relationships between them. Dataset is a disconnected architecture while datareader is connected architecture. Dataset can persists contents while datareader can not persist contents , they are forward only.

C#.net interview questions on strings

Will the following code compile and run? string str = null; Console.WriteLine(str.Length); The above code will compile, but at runtime System.NullReferenceException will be thrown. How do you create empty strings in C#? Using string.empty as shown in the example below. string EmptyString = string.empty; What is the difference between System.Text.StringBuilder and System.String? 1. Objects of type StringBuilder are mutable where as objects of type System.String are immutable. 2. As StringBuilder objects are mutable, they offer better performance than string objects of type System.String. 3. StringBuilder class is present in System.Text namespace where String class is present in System namespace. How do you determine whether a String represents a numeric value? To determine whether a String represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you...

override the ToString() method

Why should you override the ToString() method? All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below. using System; public class MainClass { public static void Main() { int Number = 10; Console.WriteLine(Number.ToString()); } } In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method. If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class. using System; public class Customer { public string FirstName; public string LastName; } public class MainClass { public static void Main() { Customer C = new Customer(); C.FirstName = "David"; C.LastName = "Boon"; Console.WriteLine(C....

C# Interview Questions on value types and reference types

What are the 2 types of data types available in C#? 1. Value Types 2. Reference Types If you define a user defined data type by using the struct keyword, Is it a a value type or reference type? Value Type If you define a user defined data type by using the class keyword, Is it a a value type or reference type? Reference type Are Value types sealed? Yes, Value types are sealed. What is the base class from which all value types are derived? System.ValueType Give examples for value types? Enum Struct Give examples for reference types? Class Delegate Array Interface What are the differences between value types and reference types? 1. Value types are stored on the stack where as reference types are stored on the managed heap. 2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap. 3. There is no heap allocation or garbage collection overhead for value-type variables. As ...

C# Interview Questions on Properties

What are Properties in C#. Explain with an example? Properties in C# are class members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods. In the example below _firstName and _lastName are private string variables which are accessible only inside the Customer class. _firstName and _lastName are exposed using FirstName and LastName public properties respectively. The get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. The value keyword is used to define the value being assigned by the set accessor. The FullName property computes the full name of the customer. Full Name property is readonly, because it has only the get accessor...

C# Interview Questions on structs

Will the following code compile? using System; public class Example { static void Main() { TestStruct T = new TestStruct(); Console.WriteLine(T.i); } } public struct TestStruct { public int i=10; //Error: cannot have instance field initializers in structs } No, a compile time error will be generated stating "within a struct declaration, fields cannot be initialized unless they are declared as const or static" Can a struct have a default constructor (a constructor without parameters) or a destructor in C#? No Can you instantiate a struct without using a new operator in C#? Yes, you can instantiate a struct without using a new operator Can a struct inherit from another struct or class in C#? No, a struct cannot inherit from another struct or class, and it cannot be the base of a class. Can a struct inherit from an interface in C#? Yes Are structs value types or reference types? Structs are value types. What is the base type from which all structs inherit directly? All structs i...

C# Interview Questions on polymorphism

Explain polymorphism in C# with a simple example? Polymorphism allows you to invoke derived class methods through a base class reference during run-time. An example is shown below. using System; public class DrawingObject { public virtual void Draw() { Console.WriteLine("I am a drawing object."); } } public class Triangle : DrawingObject { public override void Draw() { Console.WriteLine("I am a Triangle."); } } public class Circle : DrawingObject { public override void Draw() { Console.WriteLine("I am a Circle."); } } public class Rectangle : DrawingObject { public override void Draw() { Console.WriteLine("I am a Rectangle."); } } public class DrawDemo { public static void Main() { DrawingObject[] DrawObj = new DrawingObject[4]; DrawObj[0] = new Triangle(); DrawObj[1] = new Circle(); DrawObj[2] = new Rectangle(); DrawObj[3] = new DrawingObject(); foreach (DrawingObject drawObj in DrawObj) { drawObj.Draw(); } } } When can a derived class override ...

C# Interview Questions on Inheritance

What are the 4 pillars of any object oriented programming language? 1. Abstraction 2. Inheritance 3. Encapsulation 4. Polymorphism Do structs support inheritance? No, structs do not support inheritance, but they can implement interfaces. What is the main advantage of using inheritance? Code reuse Is the following code legal? class ChildClass : ParentClassA, ParentClassB { } No, a child class can have only one base class. You cannot specify 2 base classes at the same time. C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance. What will be the output of the following code? using System; public class BaseClass { public BaseClass() { Console.WriteLine("I am a base class"); } } public class ChildClass : BaseClass { public ChildClass() { Console.WriteLine("I am a child class"); } static void Main() { ChildClass CC = new ChildClass(); } } Output: I am a base clas...