31 January 2013

String and String Builder in C#

  1. System.String is immutable.(Non updatable) 
  2. System.StringBuilder is mutable (Updatable) 
  3. Using StringBuilder, various string operations can be performed in most effective manner.
Example 
class Program 

static void Main(string[] args) 

//for example: 
string str = "hello"; //Creates a new object when we concatenate any other words along with str variable it does not actually modify the str variable, instead it creates a whole new string. 
str = str + " to all"; 
Console.WriteLine(str); 
StringBuilder s = new StringBuilder("Hi"); 
s.Append(" To All"); 
Console.WriteLine(s); 
Console.Read(); 

}

Difference between Primary Key & Foreign Key

Difference between Primary Key & Foreign Key
Primary Key
Foreign Key
Primary key uniquely identify a record in the table.
Foreign key is a field in the table that is primary key in another table.
Primary Key can't accept null values.
Foreign key can accept multiple null value.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Foreign key do not automatically create an index, clustered on non-clustered. You can manually create an index on foreign key.
We can have only one Primary key in a table.
We can have more than one foreign key in a table.

Difference between Primary Key & Unique Key

                                                   Difference between Primary Key & Unique Key
Primary Key
Unique Key
Primary Key can't accept null values.
Unique key can accept only one null value.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
By default, Unique key is non-clustered index.
We can have only one Primary key in a table.
We can have more than one unique key in a table.
Primary key can be made foreign key into another table.
Unique key can't be made foreign key into another table.

Anonymous method in C#

The concept of anonymous method was introduced in C# 2.0. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say, an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.

Features of anonymous method

  1. A variable, declared outside the anonymous method can be accessed inside the anonymous method.
  2. A variable, declared inside the anonymous method can’t be accessed outside the anonymous method.
  3. We use anonymous method in event handling.
  4. An anonymous method, declared without parenthesis can be assigned to a delegate with any signature.
  5. Unsafe code can’t be accessed within an anonymous method.
  6. An anonymous method can’t access the ref or out parameters of an outer scope.

Difference between Stored Procedure and Function

Basic Difference

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).
  2. Functions can have only input parameters for it whereas Procedures can have input/output parameters .
  3. Function takes one input parameter it is mandatory but Stored Procedure may take o to n input parameters..
  4. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Advance Difference

  1. Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.
  2. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.
  3. Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.
  4. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.
  5. Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

Assembly in .Net

An assembly is the basic building block in .NET. It is the compiled format of a class, that contains Metadata, Manisfest & Intermediate Language code. 
An assembly may be either Public or Private. A public assembly means the same as Shared Assembly. 
Private Assembly :
This type of assembly is used by a single application. It is stored in the application's directory or the applications sub-directory. There is no version constraint in a private assembly. 
Shared Assembly or Public Assembly : 
A shared assembly has version constraint. It is stored in the Global Assembly Cache (GAC). GAC is a repository of shared assemblies maintained by the .NET runtime. It is located at C:\Windows\Assembly OR C:\Winnt\Assembly. The shared assemblies may be used by many applications. To make an assembly a shared assembly, it has to be strongly named. In order to share an assembly with many applications, it must have a strong name. 

A Strong Name assembly is an assembly that has its own identity, through its version and uniqueness. 

What is .Net Framework ?

The .NET Framework is an environment for building, deploying, and running Web Services and other applications. It consists of three main parts: the Common Language Runtime, the Framework classes, and ASP.NET.
.Net Frame work is platform where developer develops web application,windows service,web services ,WCF services,Remote servics and also deploy the applications.
It has two components
1.CLR :Common language Runtime
2. Class Liberary.
Common Language Runtime :
The Common Language Runtime (CLR) Environment provides a rich set of features for cross-language development and deployment. CLR supports both object-oriented languages and procedural languages. CLR manages the execution of code and provides various services such as security, garbage collection, cross-language exception handling, cross-language inheritance, support for the Base Class Library (BCL), and so on. These are the main constituents of the CLR:
  • The Common Type System (CTS) supports object-oriented programming languages as well as procedural languages. Basically CTS provides a rich type system that's intended to support a wide range of languages.
  • The Common Language Specification (CLS) is a subset of the Common Type System, to which all language compilers targeting CLR must adhere.
  • All compilers under .NET will generate a uniform, common language called Intermediate Language (IL), no matter what language is used to develop the application. In fact, CLR will not be aware of the language used to develop an application. For this reason, IL can be considered the language of CLR—a platform for cross-language development.
  • The Just in Time (JIT) compiler converts the Intermediate Language code back to a platform/device-specific code. In .NET you have three types of JIT compilers:
  • Pre-JIT (compiles entire code into native code at one stretch)
  • Ecno-JIT (compiles code part by part, freeing when required)
  • Normal-JIT (compiles only that part of the code when called, and places it in the cache)
  • Type safety is ensured in this phase. In all, the role of a JIT compiler is to bring higher performance by placing the once-compiled code in cache, so that when the next call is made to the same method/procedure, it's executed at a faster speed.
  • The Virtual Execution System (VES) implements the Common Type System. VES loads links and runs Portable Executable (PE) files. VES also ensures loading of the information contained in metadata.
  • Metadata describes and references the datatypes defined by the VOS type system, lays out instances of classes in memory, resolves method invocation, and solves versioning problems (DLL hell).
Figure 1 depicts the .NET architecture.
Figure 1 .NET architecture.
The Windows operating systems run on the Intel family of microprocessor chips. To generate platform-neutral code, two things should happen:
  • Elimination of hardware dependencies such as microprocessor instruction sets, etc.
  • Elimination of software dependencies such as operating-system-naïve API, etc.
Once the code is compiled using any of the .NET-compliant language compilers, it gets converted to IL (Intermediate Language), as shown in Figure 1. This code is not compiled to machine-native code, but to an intermediate form that doesn't contain any specific information about hardware or software dependencies. When you run this code, since it's not in machine-specific form, it fails to execute. A runtime environment is required that understands and converts the IL code to machine-specific code. This is the role played by CLR (along with the many other functions discussed earlier). I won't talk about these issues in detail here, but will instead concentrate on implementation of cross-language capabilities of the .NET Framework.

Common Language Features

  • All languages use the same library, the Base Class Library (BCL). However, the syntax used by these languages remains the same as that of the original language.
  • No language under .NET has its own library.
  • Garbage collection is the responsibility of the CLR environment and not a language.