Monday, December 29, 2008
What's New in the .NET Framework 3.5
Many new classes and features are provided by the .NET Framework 3.5. This latest version layers on top of the prior version. The .NET Framework 2.0 (and SP1) added many new base classes, interfaces, generics, and more. The interim release (.NET Framework 3.0) that shipped with Windows Vista layered in support for Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and Windows Workflow Foundation (WF). This latest version continues to build on that release. The 3.5 version the .NET Framework includes LINQ, a revision to ASP.NET, the Add-In Framework, SQL Synch Services, and more.
ASP.NET—
The .NET Framework 3.5 includes many new enhancements for the ASP.NET web developers. The System.Web namespace that backs ASP.NET includes many new classes and controls. For example, the framework now directly supports AJAX programming with the ScriptManager and UpdatePanel controls. There is also a new control for displaying data called ListView, a data-source object called LinqDataSource for working with LINQ data, and a DataPager object for controlling how records are paged in your application.
LINQ—
We’ve mentioned LINQ a few times already; it’s worth noting, however, that LINQ is built into the .NET Framework. This includes the System.Linq namespace that defines standard LINQ query operators and types. The System.Data.Linq namespace holds the connection between databases and the LINQ subsystem. There are more LINQ-related namespaces too. These include System.Data.Linq.Mapping for handling the O/R mapping between SQL and LINQ and System.Xml.Linq for working between XML and the LINQ subsystem. Of course, many of the controls in the framework have also been updated to work with LINQ.
Add-In Framework—
The System.AddIn namespace is new to the .NET Framework 3.5. It provides classes and methods for developers looking to build applications that can be extended based on a common add-in framework. For example, the AddInStore class allows for the discovery and management of add-ins. The framework also provides versioning, isolation, activation, and sandboxing. If you are building a new application and hope to allow for add-ins, you want to dig in deeper on this namespace.
ClickOnce Improvements—
ClickOnce application deployment continues to improve in this latest version of .NET. This includes the capability to deploy an application from multiple locations and third-party branding on your ClickOnce deployment dialogs.
Windows Communication Foundation (WCF)—
The new System.ServiceModel encapsulates what is known as WCF. With it you can easily create service-based applications that work across multiple protocols, transports, and message types. WCF is a major component of .NET 3.5, “Embedding Workflow in Your Applications.” However, some highlights included in WCF are the following: expose and consume RSS and ATOM feeds with the System.ServiceModel.Syndication namespace; communicate with AJAX operations using the JavaScript Object Notation (JSON) data format built into WCF’s System.Runtime.Serialization.Json namespace; and use the WCF web programming model to create REST (representational state transfer) services to communicate directly across HTTP (without SOAP). In addition, WCF contains the new identity management system called CardSpace.
Windows Presentation Foundation (WPF)—
WPF provides a new presentation technology for Windows applications. This technology is spread throughout the System.Windows namespace and includes support for creating Windows applications based on XAML, XBAP, vector graphics, and both 2D and 3D scenarios.
Windows Workflow Foundation (WF)—
The System.Workflow namespace first introduced in .NET 3.0 has been extended (and integrated into Visual Studio) in the 3.5 release. WF allows you to create both sequential and state-driven workflows for your applications, host them, persist them, and more. WF is also now integrated with WCF. Therefore, you can easily expose and call a workflow as a WCF service.
Tracing and Diagnostics—The System.Diagnostics namespace contains the new EventSchemaTraceListener class to allow for cross-domain, cross-thread, crosscomputer, end-to-end, lock-free logging and tracing.
Pipe Streams—The new System.IO.Pipes namespace provides support for both named and anonymous pipe communication. With it, you can write code that communicates at the pipe level across processes and across computers. For example, the NamedPipeServerStream class can be used to allow read and write communication across a named pipe. The NamedPipeClientStream provides client support for communicating with pipe servers.
Threading Improvements—There is a new class called ReaderWriterLockSlim in the System.Threading namespace that provides better performance and reduced deadlock scenarios.
Time Zone Additions—There are two new types that help you work with applications that need to understand multiple time zones. These classes are System.DateTimeOffset and TimeZoneInfo. The DateTimeOffset structure represents an exact point in time. The offset indicates how the time differs from UTC (Universal Coordinated Time). You use this new class when you need precision and date/time arithmetic. The TimeZoneInfo class is a welcome enhancement that represents a date and time in a given time zone. You can use this class to reliably represent the same date and time in any other time zone. In addition, you can use the class to create custom time zones if needed.
Peer-to-Peer Networking Support—The .NET Framework finally has its own peer-to-peer networking support. This can be found in the System.Net.PeerToPeer namespace. With it, you can create an application that works without a server and instead communicates from one client (peer) to another (similar to Microsoft’s Groove application). Application scenarios supported by this new namespace include tracking where peers are (online or offline), what they might be doing, interacting (messaging) with peers, managing peer contacts, discovering new peers, and more.
Sync Services for ADO.NET—Shipping with Visual Studio 2008 is Microsoft’s Sync Services. With it you can build an application that works both online and offline. These types of applications are referred to as occasionally connected applications (OCA). You use Sync Services (and its related tools) to indicate which data should be available when a user is offline. When connected, the Sync Services works to synchronize user changes with database changes.
The Sync Services for ADO.NET is part of the Microsoft.Synchronization.Data namespace. Notice that this is not part of System and therefore not a principal part of the .NET Framework but an add-on library from Microsoft. The ADO.NET services are actually a part of what is called the Microsoft Sync Framework. This framework provides synchronization services for data (ADO.NET), file and folders (the File
System), and RSS/ATOM feeds (FeedSync).
Windows Vista Support—The Windows Forms engine has been updated to give your applications Vista support. This means that upgrading your applications will give them that Vista appearance. In addition, you now have access to the common dialogs (System.Windows.Forms.FileDialog) in Vista. For example, the OpenFileDialog and SaveFileDialog support the same interface you see in Windows Vista for opening and saving files.
.NET Compact Framework—A new version of the .NET Compact Framework also ships with 3.5. This framework is used on distributed, mobile devices such as SmartPhones and Windows Mobile/CE devices. This faster version of the framework provides support for WCF, LINQ, and improved debugging and diagnostics.
Thursday, December 25, 2008
Use of Checked and UnChecked operator.
The checked and unchecked operators are used to control the overflow checking context for integral-type arithmetic operations and conversions.
checked-expression:
checked ( expression )
unchecked-expression:
unchecked ( expression )
Stack Overflows are usual problems during arithmetic operations and conversion of integer types. C# supports two operators: checked and unchecked, Which can be used for checking and/or unchecking stack overflows during program execution.
If an operation is checked, then an exception will be thrown if overflow occurs. If it is not checked, no exception will be raised but we will lose data.
Example:
int a=200000;
int b=300000;
try
{
int m=checked(a*b);
}
catch(OverflowException e)
{
Console.WriteLine(e);
}
Since a*b produces a value that will exceed the max value for an int, an overflow occurs. As the operation is checked with operator 'checked' , an overflow exception will be thrown. In this case, we will get output like this:
System.OverflowException: An exception of type System.OverflowException was thrown at ...
If we want to suppress the overflow checking, we can mark the code as unchecked.
int m = unchecked(a*b);
In this case, no exception will be raised, but we will lose data.
Sunday, December 21, 2008
When use Interface and Abstract Class? In which Scenario.
2. In future if you plan to inherit from multiple inheritance then go for interface. Abstract class do not allow multiple implementation.
3. If you want to restrict the accessbility of the methods that is defined then go for Abstract class as it can contain access modifiers for the subs, functions, properties.
Hope this would give an idea for your preference.
Interface is used to provide their own implementation, so your method signature is specified in interface which your implementing class need to implement inside and implementation would differ according to your requirement, example your known interface iEnumerable, iDisposable.
Another advantage is that your class can support multiple implementation using multiple interfaces.
Abstract class is used when you want to specify some common implementation (common in your application) along with variable things.
As Abstract Class allow implementation alogn with abstract methods. which inheriting class need to implement.
Abstract Classes: Classes which cannot be instantiated. This means one
cannot make a object of this class or in other way cannot create
object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is
abstract class.
Abstarct classes contains have one or more abstarct methods, ie method
body only no implementation.
Interfaces: These are same as abstract classes only difference is we
an only define method defination and no implementation.
When to use wot depends on various reasons. One being design choice.
One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.
When we do not want any one to create object of our class we define the class as abstract.
when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will.
Why use interface?
1. An interface is an entity that is defined by the word Interface and is not a class. An interface cannot have an implementation; it only has the signature - just the definition of the methods without the body. We can implement the methods in the classes in future by inheriting these interfaces.
2. Also, no fields can be defined in interfaces.
3. C# doesn’t support multiple inheritance hence interfaces are used to implement multiple inheritance.
4.An interface cannot have access modifiers for the subs, functions, properties etc. Everything is assumed to be Public.
5.Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
These features make us to use interfaces.
When we want to force the user to use the methods. so we will go for the interface. it will not have any defination of the method. the inherited method will add its own defination. we can use the interface as multiple inheritence.
Tuesday, December 16, 2008
Polymorphism
Dynamic Polymorphism
There are two type of polymorphism i.e.
static(compiplation time) and
Dynamic Polymorphism(Run Time).
Example of Compile Time Polymorphism
Method Overloading
- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.
- Example of Method Overloading:
class A1
{
void hello()
{ Console.WriteLine(“Hello”); }
void hello(string s)
{ Console.WriteLine(“Hello {0}”,s); }
}
Example of Run Time Polymorphism
Method Overriding
- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
- Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
- Example of Method Overriding:
Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}
Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}
static void main()
{
parent objParent = new child();
objParent.hello();
}
//Output
Hello from Child.
Tool are use to evaluate the performance in sql server application
1.Query Analyzer.
2.Index Wizard.
3.Profiler.
4.Performance Monitor
Check the below link for more info.
http://www.sqlteam.com/article/sql-server-2000-performance-tuning-tools
http://www.gre-sqlserver-solutions.com/MonitoringTools.html
Monday, December 15, 2008
Partial Classes
Partial Classes are useful feature for separating machine generated code and hard written code in the same class.It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable:
1. When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
2. When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.
Friday, December 12, 2008
Generic class can be inherited by a normal class
Yes, a normal class can be derived from a Generic class.
public class T< a >{
protected A t;
public T(A a) {
t = a;
}}
Here Class T is a generic class and the following class B inherited from T of type integer
public class B:T
{
public B(int a) : base(a) { }
public void Print() { Console.WriteLine(t); }
}
We should mention the Type in angle bracket (<>) to know which type of non generic class this is.
Temp Table VS Table Variable
Temp table:
Consider the below sample temp table which holds the information about companies.
CREATE TABLE #Tmp
(
CompanyId Int,
Name varchar (50),
Location varchar (50)
)
1. The temp table name always starts with # or ## and are created in the tempdb database. The # indicates that the temp table is a local temporary table i.e. table is accessible only by the particular connection of SQL Server which created it. The ## indicates that the temp table is a global temporary table i.e. the table is accessible from any connection. They are dropped automatically when the last session that uses them has completed.
2. Since the local temporary table is accessible only by the connection which created it, this helps in minimizing the locks.
3. We can create indexes, statistics in temp tables and hence performance can be improved.
4. We cannot have foreign key constraints on temp tables.
5. Causes recompilation within stored procedures.
6. Only undo information is logged in tempdb and not the redo information.
7. We can Rollback the transactions in temp table similar to a normal table but not in table variable.
8.Temp tables can be used in nested stored procedures.
9. The temp table names cannot exceed 116 characters whereas the permanent table can have 128 characters
The following example illustrates the transaction behavior in Temp tables:
--using temp tables where ROLLBACK happens
CREATE TABLE #Tmp
(
CompanyId Int,
Name varchar(20),
Location varchar(20)
)
GO
INSERT INTO #Tmp
VALUES (1,'Saikat','Kolkata')
GO
BEGIN TRAN
UPDATE #Tmp
SET Location='KOL'
WHERE CompanyId=1
ROLLBACK TRAN
SELECT * FROM #Tmp
Table variables:
The following the syntax for table variables:
DECLARE @Tmp TABLE
(
CompanyId Int,
Name varchar(20),
Location varchar(20)
)
1. Table variables are local to a stored procedure and hence cannot be used in nested stored procedures
2.We cannot create Nonclustered indexes in Table variables only Clustered index can be created by specifying them as constraints (Primary or Unique) DECLARE @Tmp TABLE (C1 int, C2 int, PRIMARY KEY (C1, C2))
3.Table variables store the contents in memory but not always. Under extreme memory pressure, the pages belonging to table variables will be moved to tempdb
4.We cannot Alter a table variable once its declared
5.We cannot create statistics in table variables
6.They cannot make use of multiple processors and hence Parallelism is not possible
7.Transactions cannot be rollbacked in Table variable
Sunday, December 7, 2008
Private constructor and where will you use it.
When you declare a Constructor with Private access modifier then it is called Private Constructor. If you declare a Constructor as private then it don't allow to create object for its derived class,i.e you loose inhirect facility for that class.
Ex:
Class A{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
Because Class A construcor declared as private hence its accessbility limit is to that class only ,Class B can't access. As i explained the heirarchy of Constructors in the previous qn, when we create a object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.
When we want to control the object instantiation or trying to implement singleton pattern