Namespaces in C#
A Namespace is simply a logical collection of related classes in C#. We bundle our related classes (like those
related with database activity) in some named collection calling it a namespace (e.g., DataActivity). As C# does
not allow two classes with the same name to be used in a program, the sole purpose of using namespaces is to
prevent name conflicts. This may happen when you have a large number of classes, as is the case in the Framework
Class Library (FCL). It is very much possible that our Connection Class in DataActivity conflicts with the
Connection Class of InternetActivity. To avoid this, these classes are made part of their respective namespace. So
the fully qualified name of these classes will be DataActivity.Connection and InternetActivity.Connection, hence
resolving any ambiguity for the compiler.
So, in the second line of our program we are declaring that the following classes (within { } block) are part of
MyHelloWorldApplication namespace.
namespace MyHelloWorldApplication
{
...
}
Printing on the Console
Console.WriteLine("Hello World");
Here we called WriteLine(), a static method of the Console class defined in the System namespace. This method
takes a string (enclosed in double quotes) as its parameter and prints it on the Console window.
C#, like other Object Oriented languages, uses the dot (.) operator to access the member variables (fields) and
methods of a class. Also, braces () are used to identify methods in the code and string literals are enclosed in double
quotation marks ("). Lastly, each statement in C# (like C, C++ and Java) ends with a semicolon (;), also called the
statement terminator.
Friday, November 27, 2009
The .Net Architecture and .Net Framework
The Common Language Runtime (CLR)
The most important concept of the .Net Framework is the existence and functionality of the .Net Common
Language Runtime (CLR), also called .Net Runtime for short. It is a framework layer that resides above the OS and
handles the execution of all the .Net applications. Our programs don't directly communicate with the OS but go
through the CLR.
MSIL (Microsoft Intermediate Language) Code
When we compile our .Net Program using any .Net compliant language (such as C#, VB.Net or C++.Net) our
source code does not get converted into the executable binary code, but to an intermediate code known as MSIL
which is interpreted by the Common Language Runtime. MSIL is operating system and hardware independent
code. Upon program execution, this MSIL (intermediate code) is converted to binary executable code (native
code). Cross language relationships are possible as the MSIL code is similar for each .Net language.
Just In Time Compilers (JITers)
When our IL compiled code needs to be executed, the CLR invokes the JIT compiler, which compile the IL code to
native executable code (.exe or .dll) that is designed for the specific machine and OS. JITers in many ways are
different from traditional compilers as they compile the IL to native code only when desired; e.g., when a function
is called, the IL of the function's body is converted to native code just in time. So, the part of code that is not used
by that particular run is never converted to native code. If some IL code is converted to native code, then the next
time it's needed, the CLR reuses the same (already compiled) copy without re-compiling. So, if a program runs for
some time (assuming that all or most of the functions get called), then it won't have any just-in-time performance
penalty.
As JITers are aware of the specific processor and OS at runtime, they can optimize the code extremely efficiently
resulting in very robust applications. Also, since a JIT compiler knows the exact current state of executable code,
they can also optimize the code by in-lining small function calls (like replacing body of small function when its
called in a loop, saving the function call time). Although Microsoft stated that C# and .Net are not competing with
languages like C++ in efficiency and speed of execution, JITers can make your code even faster than C++ code in
some cases when the program is run over an extended period of time (like web-servers).
The Framework Class Library (FCL)
The .Net Framework provides a huge Framework (or Base) Class Library (FCL) for common, usual tasks. FCL
contains thousands of classes to provide access to Windows API and common functions like String Manipulation,
Common Data Structures, IO, Streams, Threads, Security, Network Programming, Windows Programming, Web
Programming, Data Access, etc. It is simply the largest standard library ever shipped with any development
environment or programming language. The best part of this library is they follow extremely efficient OO design
(design patterns) making their access and use very simple and predictable. You can use the classes in FCL in your
program just as you would use any other class. You can even apply inheritance and polymorphism to these classes.
The Common Language Specification (CLS)
Earlier, we used the term '.Net Compliant Language' and stated that all the .Net compliant languages can make use
of CLR and FCL. But what makes a language a '.Net compliant' language? The answer is the Common Language
Specification (CLS). Microsoft has released a small set of specifications that each language should meet to qualify
as a .Net Compliant Language. As IL is a very rich language, it is not necessary for a language to implement all the
IL functionality; rather, it merely needs to meet a small subset of CLS to qualify as a .Net compliant language. This
is the reason why so many languages (procedural and OO) are now running under the .Net umbrella. CLS basically
addresses language design issues and lays down certain standards. For instance, there shouldn't be any global
function declarations, no pointers, no multiple inheritance and things like that. The important point to note here is
that if you keep your code within the CLS boundary, your code is guaranteed to be usable in any other .Net language.
The Common Type System (CTS)
.Net also defines a Common Type System (CTS). Like CLS, CTS is also a set of standards. CTS defines the basic
data types that IL understands. Each .Net compliant language should map its data types to these standard data
types. This makes it possible for the 2 languages to communicate with each other by passing/receiving parameters
to and from each other. For example, CTS defines a type, Int32, an integral data type of 32 bits (4 bytes) which is
mapped by C# through int and VB.Net through its Integer data type.
Garbage Collection (GC)
CLR also contains the Garbage Collector (GC), which runs in a low-priority thread and checks for un-referenced,
dynamically allocated memory space. If it finds some data that is no longer referenced by any variable/reference, it
re-claims it and returns it to the OS. The presence of a standard Garbage Collector frees the programmer from
keeping track of dangling data. Ask any C++ programmer how big a relief it is!
The most important concept of the .Net Framework is the existence and functionality of the .Net Common
Language Runtime (CLR), also called .Net Runtime for short. It is a framework layer that resides above the OS and
handles the execution of all the .Net applications. Our programs don't directly communicate with the OS but go
through the CLR.
MSIL (Microsoft Intermediate Language) Code
When we compile our .Net Program using any .Net compliant language (such as C#, VB.Net or C++.Net) our
source code does not get converted into the executable binary code, but to an intermediate code known as MSIL
which is interpreted by the Common Language Runtime. MSIL is operating system and hardware independent
code. Upon program execution, this MSIL (intermediate code) is converted to binary executable code (native
code). Cross language relationships are possible as the MSIL code is similar for each .Net language.
Just In Time Compilers (JITers)
When our IL compiled code needs to be executed, the CLR invokes the JIT compiler, which compile the IL code to
native executable code (.exe or .dll) that is designed for the specific machine and OS. JITers in many ways are
different from traditional compilers as they compile the IL to native code only when desired; e.g., when a function
is called, the IL of the function's body is converted to native code just in time. So, the part of code that is not used
by that particular run is never converted to native code. If some IL code is converted to native code, then the next
time it's needed, the CLR reuses the same (already compiled) copy without re-compiling. So, if a program runs for
some time (assuming that all or most of the functions get called), then it won't have any just-in-time performance
penalty.
As JITers are aware of the specific processor and OS at runtime, they can optimize the code extremely efficiently
resulting in very robust applications. Also, since a JIT compiler knows the exact current state of executable code,
they can also optimize the code by in-lining small function calls (like replacing body of small function when its
called in a loop, saving the function call time). Although Microsoft stated that C# and .Net are not competing with
languages like C++ in efficiency and speed of execution, JITers can make your code even faster than C++ code in
some cases when the program is run over an extended period of time (like web-servers).
The Framework Class Library (FCL)
The .Net Framework provides a huge Framework (or Base) Class Library (FCL) for common, usual tasks. FCL
contains thousands of classes to provide access to Windows API and common functions like String Manipulation,
Common Data Structures, IO, Streams, Threads, Security, Network Programming, Windows Programming, Web
Programming, Data Access, etc. It is simply the largest standard library ever shipped with any development
environment or programming language. The best part of this library is they follow extremely efficient OO design
(design patterns) making their access and use very simple and predictable. You can use the classes in FCL in your
program just as you would use any other class. You can even apply inheritance and polymorphism to these classes.
The Common Language Specification (CLS)
Earlier, we used the term '.Net Compliant Language' and stated that all the .Net compliant languages can make use
of CLR and FCL. But what makes a language a '.Net compliant' language? The answer is the Common Language
Specification (CLS). Microsoft has released a small set of specifications that each language should meet to qualify
as a .Net Compliant Language. As IL is a very rich language, it is not necessary for a language to implement all the
IL functionality; rather, it merely needs to meet a small subset of CLS to qualify as a .Net compliant language. This
is the reason why so many languages (procedural and OO) are now running under the .Net umbrella. CLS basically
addresses language design issues and lays down certain standards. For instance, there shouldn't be any global
function declarations, no pointers, no multiple inheritance and things like that. The important point to note here is
that if you keep your code within the CLS boundary, your code is guaranteed to be usable in any other .Net language.
The Common Type System (CTS)
.Net also defines a Common Type System (CTS). Like CLS, CTS is also a set of standards. CTS defines the basic
data types that IL understands. Each .Net compliant language should map its data types to these standard data
types. This makes it possible for the 2 languages to communicate with each other by passing/receiving parameters
to and from each other. For example, CTS defines a type, Int32, an integral data type of 32 bits (4 bytes) which is
mapped by C# through int and VB.Net through its Integer data type.
Garbage Collection (GC)
CLR also contains the Garbage Collector (GC), which runs in a low-priority thread and checks for un-referenced,
dynamically allocated memory space. If it finds some data that is no longer referenced by any variable/reference, it
re-claims it and returns it to the OS. The presence of a standard Garbage Collector frees the programmer from
keeping track of dangling data. Ask any C++ programmer how big a relief it is!
program for checking if a number is prime or not
SQL>ed prime.sql
Declare
N number:=&n;
I number:=2;
R number(3):=0;
Begin
While(i<=n/2) Loop R:=mod(n,i); If r=0 then Exit; End if; I:=i 1; End loop; If r=1 then Dbms_output.put_line(‘prime’); else dbms_output.put_line(‘not prime’); end if; end; / SQL>@ prime;
Declare
N number:=&n;
I number:=2;
R number(3):=0;
Begin
While(i<=n/2) Loop R:=mod(n,i); If r=0 then Exit; End if; I:=i 1; End loop; If r=1 then Dbms_output.put_line(‘prime’); else dbms_output.put_line(‘not prime’); end if; end; / SQL>@ prime;
Trigger
TRIGGER: Triggers are procedures that are sotored in the database not in the complied form and implicitly fired when an event occurs.Traditionally,triggers guarantee that when a specific operation is performed related actions are performd.Triggers support the execution of a Pl/SQL block when an INSERT
Before/after insert or delete or update onfor each row.
Before/after insert or delete or update on