Multiple inheritance with interfaces
December 26, 2010 Leave a comment
A useful link on the same.
My tekno illogical symposium…
December 26, 2010 Leave a comment
This post is to understand how does an exception propagates through a method stack. Consider the following code:
static void Main(string[] args)
{
var testObject = new Test4TryCatch();
testObject.M1();
}
public void M1()
{
try
{
Console.WriteLine("Line in M1 before calling M2");
M2();
Console.WriteLine("Line in M1 after callig M2 and after exception");
}
catch (Exception e)
{
throw new Exception("Exception in M1", e);
}
finally
{
Console.WriteLine("Line in M1 finally");
}
}
public void M2()
{
try
{
Console.WriteLine("Line in M2 before exception");
throw new Exception("Explicit exception for M2 try block");
Console.WriteLine("Line in M2 after exception");
}
catch (Exception)
{
throw;
}
finally
{
Console.WriteLine("Line in M2 finally");
}
}
In the above written snippet, following would be the order of execution upon calling M1 from Main:
1. M1′s first line – “Line in M1 before calling M2″ would output
2. M2′s first line – “Line in M2 before exception” would output
3. An exception with message “Explicit exception for M2 try block” would be thrown from M2′s try block
4. Same exception re-thrown by M2′s catch block
5. M2′s finally block gets executed – “Line in M2 finally”
6. The controls passes to M1′s catch block which hides the M2 exception by creating a new exception with message “Exception in M1″ as well as an inner exception containing the M2 exception object (e).
7. Ultimately, M1′s finally block gets executed – “Line in M1 finally”.
The important points to remember are:
1. Finally of all methods would be executed
2. Exception thrown can be different from what actually occurred as it depends on the outermost catch block’s handling behavior.
July 7, 2010 Leave a comment
Both of them are used for storing text (series of characters) but have the following differences:
In SQL Server 2000, varchar has a maximum limit of 8000 characters and nvarchar has 4000 characters only (remember it needs double the space needed by a varchar, hence the storage capacity becomes half). In fact a given row in a SQL Server 2000 table cannot exceed 8000 characters in size.
Beyond 8000, one had to go with Text and Ntext data types. However, in SQL Server 2005, the max keyword has been introduced which replaces the Text/NText fields by maximizing the capacity of varchar/nvarchar.
By using varchar(max)/nvarchar(max) the storage limit reaches upto 2^31 bytes of data.
HTH!
July 7, 2010 Leave a comment
An RDBMS like SQL Server contains many objects. Some of these objects with respect to SQL Server 2005 have been briefly described below.
The database itself
The highest-level object within a DBMS is certainly the database itself, its a collection of various other objects such as tables, views, stored procedures, etc.
When SQL Server 2005 is loaded for the first time, it contains the following four system databases:
• master DB – This is the most critical system database present and cannot be deleted. It contains a special set of system tables which keep track of the system as a whole, example each new database created on the server has a corresponding entry in the sysdatabases table in the master db.
• model DB – As the name suggests, its contains the model (or the template) which would be used for any new database created on the server. If one modifies it, then every database would contain the modified characteristic. Its a required database and cannot be deleted.
• msdb DB – This is the database where SQL Agent process stores any system-related tasks. Example if there is any scheduled task like backing up of a database or a sproe execution scheduled, the msdb would have entries relating to them.
• tempdb DB – One of the most interesting system databases, it contains all the temporary objects created while using the SQL Server DBMS. Whenever a large complex query which requires creating interim tables is executed, the interim tables are cretaed in tempdb, and same is the case for any user created temporary table in a query, irrespective of the current database. One more point to note is that this is the only database which is created from scratch every time the SQL Server is started. Kinda cool trivia!
Transaction Log
This is the first point of storage where every database change gets stored, yeah you read it correct, although the data is read from the database file, while writing/updating data, changes do not go directly to the database file rather they get stored in the transaction log and get propagated to the database file when database issues a checkpoint.
The database file has a random access arrangement, but the log is serial in nature thus making the database tracking done in an orderly manner.
Tables
The fundamental object within the database, it contains the domain data (columns) and entity data (rows) in a tabular format. Each tables also contains metadata which tells the type of data allowed in the table.
Indexes
An index is one of those database objects which exists only within the framework of a table or view. It helps in speeding the lookup of information present in a table, just like an index at the back of a book does.
There are two types of indexes:
• Clustered Indexes – There can be only one clustered index on a table and data present in the table is ‘physically’ sorted according to this index. A real world example of this index type would be the page numbers of a book.
• Non-clustered Indexes – These are created to improve performance of frequently used queries not covered by the clustered index. The maximum number of non-clustered indexes that can be created an a table is 999. A real world example would be the index section at the back of a book.
Triggers
This object also exists only within the framework of a table. A trigger is a piece of logical code that is automatically executed each time a certain action occurs such as inserts, updates or deletes on a table. Usually they are used for checking the consistency of the data addition to a table.
Constraints
This object also exists only within the framework of a table. They as the name suggest help in limiting the data in a table to meet certain conditions. Both triggers and constraints help in maintaining data integrity.
Stored Procedures
sprocs are generally an ordered series of T-SQL statements bundled together into a single unit. They have several advantages over individual statements:
• Less network traffic required to run the code as sprocs are called using their short names instead of the entire query text.
• sprocs are ‘pre-optimized’ and ‘pre-compiled’ thus saving time each time they’re run.
• They encapsulate a process/functionality, thus hiding the complexity as well as securing the code
• Can be called from other sprocs, thus making them reusable to a limited extent.
UDFs
User-Defined Functions (UDFs) are similar to sprocs except that they:
• Can return a value of most SQL Server data types.
• Can’t have “side effects”, i.e. they can’t do anything outside their scope, like changing tables or sending e-mails.
UDFs are similar to functions/methods in VB/C# as they take in parameter (s) and return a value, however every parameter being passed is passed by value.
Full-Text Catalogs
These are mappings of data that speed the search for specific blocks of text within columns that have full-text searching enabled. These do not get updated automatically when a database changes.
Source of reference: Beginning SQL Server 2005 Programming by Robert Vieira
July 4, 2010 Leave a comment
This is one of the most common interview questions asked and ironically is answered a lot badly. I will try to explain the differences from my understanding.
1. An abstract class cannot be instantiated independently from its derived class. However its constructor can be called by the derived class. But in case of an interface, it cannot be instantiated and in fact cannot contain any constructor/finalizer.
2. While creating an abstract class, method’s implementation can be provided which the derived class may override, however no implementation is present in an interface member’s declaration. All the implementation of an interface members must be provided by the deriving classes.
3. Abstract classes are more extensible as they do not break any version compatibility when a non-abstract method is added to an abstract class, but an interface would always break code-version compatibility upon modification as the deriving classes won’t compile until additional implementations.
4. Abstract classes can contain data stored in fields whereas interfaces cannot store any data, although properties can be defined but without any implementation.
5. Deriving from an abstract class would use up the subclass’s one and only base class option, however by implementing interfaces it can still inherit from a class.
When we talk about abstract classes we are defining characteristics of an object type, specifying what an object is but in the case of an interface we define a capability and we bond to provide that capability, we are talking about establishing a contract about what the object can do.
Hope this helps in understanding further about the differences in abstract classes and interfaces, and where and when to use whom.
July 4, 2010 Leave a comment
As interfaces create a contract with the class which implements it, updating or modifying the interface would change the contract as well, thus breaking the version compatibility.
Consider an example:
interface IBase
{
int Add(int num1, int num2);
}
public class Derive1 : IBase
{
#region IBase Members
public int Add(int num1, int num2)
{
throw new NotImplementedException();
}
#endregion
}
Now, if one more method is added in the IBase interface, the class Derive1 would also need to be updated to include an implementation for the newly added method. Thus, breaking the code as Derive1 would not compile without the new implementation.
However, if a new interface is created which inherits the IBase interface and also declares the new method, classes which choose to include the new functionality can implement the new interface and remaining classes can still implement the IBase interface thereby maintaining the version compatibility.
July 4, 2010 Leave a comment
While implementing an interface member in a derived class, there are two variations – Explicit member implementation and Implicit member implementation.
Explicit member implementation
An explicit member implementation includes the interface name as prefix to the method implementation. When an interface member is implemented explicitly it is accessible only by casting to the implemented interface.
Implicit member implementation
An interface member when implemented without the interface name prefix in the deriving class is an implicit implementation.
For example:
interface IListable
{
// Returns the value of each column in the row
string[] ColumnValues
{
get;
}
}
interface IComparable
{
// Compares the object parameter and results with an integer value
int CompareTo(object obj);
}
public class Contact : IListable, IComparable
{
#region IListable Members
// An explicit interface member implementation
string[] IListable.ColumnValues
{
get { throw new NotImplementedException(); }
}
#endregion
#region IComparable Members
// An implicit interface member implementation
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
#endregion
}
public class User
{
public void Method()
{
string[] values;
object obj = 1;
int result;
Contact contact1, contact2;
contact1 = new Contact();
contact2 = new Contact();
// Error: Unable to call ColumnValues() directly
// on a contact.
// values = contact1.ColumnValues;
// For calling an explicit member,
// first cast to IListable
values = ((IListable)contact1).ColumnValues;
// Whereas, an implicit member can be called
// directly from the derived class object
result = contact2.CompareTo(obj);
}
}
When to use which?
One more significant point about explicit implementation is that in multi-level inheritance, the method implementation if explicit should be prefixed with the interface name where the method is declared, i.e. a derived interface would not work and will show a compile-time error.
July 4, 2010 Leave a comment
Interfaces in C# are another way of achieving polymorphism apart from inheritance (via abstract classes
). They create a contract which requires the classes implementing the interface to provide implementation of the methods defined in the interface.
July 4, 2010 Leave a comment
July 4, 2010 Leave a comment
This post would be a collection of useful links to websites/portal I visit regularly to gain information on .NET and its related technologies.