Dot Net Unit1
Dot Net Unit1
C# is used for:
Mobile applications
Desktop applications
Web applications
Web services
Web sites
Games
Database applications
Features of C#
There are various features of C# which makes the programming language different and widely used. These are:
.NET Framework
.NET is a framework to develop software applications. It is designed and developed by Microsoft
and the first beta version released in 2000.
It is used to develop applications for web, Windows, phone. Moreover, it provides a broad range of
functionalities and support.
This framework contains a large number of class libraries known as Framework Class Library
(FCL). The software programs written in .NET are executed in the execution environment, which is
called CLR (Common Language Runtime). These are the core and essential parts of the .NET
framework.
This framework provides various services like memory management, networking, security, memory
management, and type-safety.
The .Net Framework supports more than 60 programming languages such as C#, F#, VB.NET, J#,
VC++, JScript.NET, APL, COBOL, Perl, Oberon, ML, Pascal, Eiffel, Smalltalk, Python, Cobra,
ADA, etc.
Features of .NET
COM Programmer
The Component Object Model (COM) was Microsoft’s previous application development framework.
The beauty of a binary COM server is that it can be accessed in a language-independent manner.Thus, C++
programmers can build COM classes that can be used by VB6. COM’s language independence is somewhat
limited. Rather, you must make useof the more cumbersome “has-a” relationship to reuse COM class types.
Another benefit of COM is its location-transparent nature. Using constructs such as application identifiers
(AppIDs), stubs, proxies, and the COM runtime environment, programmers can avoid the need to work with raw
sockets, RPC calls, and other low-level
details.
• Full interoperability with existing code: Existing COM binariescan commingle (i.e., interop) with newer
.NET binaries and vice versa. Also, Platform InvocationServices (PInvoke) allows you to call C-based libraries
(including the underlying API of the operating system) from .NET code.
• Complete and total language integration: Unlike COM, .NET supports cross-language inheritance, cross-
language exception handling, and cross-language debugging.
• A common runtime engine shared by all .NET-aware languages: One aspect of this engine is a well-
defined set of types that each .NET-aware language “understands.”
• A base class library: This library provides shelter from the complexities of raw API calls and offers a
consistent object model used by all .NET-aware languages.
• No more COM plumbing: IClassFactory, IUnknown, IDispatch, IDL code, and the evil VARIANTcompliant
data types (BSTR, SAFEARRAY, and so forth) have no place in a native .NET binary.
• A truly simplified deployment model: Under .NET, there is no need to register a binary unit into the system
registry. Furthermore, .NET allows multiple versions of the same *.dll to exist in harmony on a single machine.
2) Introducing the Building Blocks of the .NET Platform (the CLR, CTS,
and CLS)
Understand that C# is not the only language targeting the .NET platform.
Benefits of CIL
The benefits of compiling source code into CIL rather than directly to a specific
instruction set are as given below:
Language integration: Each .NET-aware compiler produces nearly identical CIL
instructions. Therefore, all languages are able to interact within a well-defined binary
arena.
Given that CIL is platform-agnostic, the .NET Framework itself is platform-agnostic.
So a single code base can run on numerous operating systems, just like Java.
The advantage of having namespaces is that, any language targeting the .NET runtime makes
use of same namespaces and same types as a C# developer. For example, consider following
programs written in C#, VB.NET and MC++.
// C# code
using System;
public class Test
{
public static void Main()
{
Console.WrtieLine(“Hello World”);
}
}
// VB.NET code
Imports System
Public Module Test
Sub Main()
Console.WrtieLine(“Hello World”)
End Sub
End Module
In C#, the using keyword simplifies the process of referencing types defined in a particular
namespace. In a traditional desktop application, we can include any number of namespaces
like –
Once we specify a namespace, we can create instances of the types they contain. For
example, if we are interested in creating an instance of the Bitmap class, we can write:
using System;
using System.Drawing;
class MyApp
{
public void DisplayLogo()
{
// create a 20x20 pixel bitmap.
Bitmap bm = new Bitmap(20, 20);
...
}
}
As this application is referencing System.Drawing, the compiler is able to resolve the Bitmap
class as a member of this namespace. If we do not specify the System.Drawing namespace,
we will get a compiler error. However, we can declare variables using a fully qualified name
as well:
using System;
class MyApp
{
public void DisplayLogo()
{
// Using fully qualified name.
System.Drawing.Bitmap bm =new System.Drawing.Bitmap(20, 20);
...
}
}
Building C# Applications :
1) Role of the command line complier(csc.exe)
There are a number of techniques to compile C# source code. One way is to use the C#
command-line compiler, csc.exe (where csc stands for C-Sharp Compiler). This tool is
included with the .NET Framework 2.0 SDK. Though we may not build a large-scale
application using the command-line compiler, it is important to understand the basics of how
to compile *.cs files by hand. Few reasons for having a grip on the process are:
The most obvious reason is the simple fact that one may not have a copy of Visual
Studio 2005. But only free .NET SDK.
One may plan to make use of automated build tools such as MSBuild or NAnt.
One may want to understand C# deeply. When you use graphical IDEs to build
applications, you are ultimately instructing csc.exe how to manipulate your C# input
files. In this light, it is better to see what takes place behind the scenes.
Another benefit of working with csc.exe is that you become that much more
comfortable in manipulating other command-line tools included with the .NET
Framework 2.0 SDK.Moreover, a number of important utilities are accessible only
from the command line.
2) Building C# Applications Using csc.exe
To build a simple C# application, open a notepad and type the following code:
// A simple C# application.
using System;
class Test
{
public static void Main()
{
Console.WriteLine("Hello World!!!");
}
}
Now, save this file in any convenient location as Test.cs. Now, we have to provide the option
for which type of output file we want. The options for output are given in the following table.
Table 2-2. Output-centric Options of the C# Compiler
/out This option is used to specify the name of the assembly to be created. By
default, the assembly name is the same as the name of the initial input *.cs
file (in the case of a *.dll) or the name of the type containing the program’s
Main() method (in the case of an *.exe).
/target:exe This option builds an executable console application. This is the default file
output type, and thus may be omitted when building this application type.
/target:winexe Although you are free to build Windows-based applications using the
/target:exe flag, the /target:winexe flag prevents a console window from
appearing in the background.
Table 2-3 documents some (but certainly not all) of the flags recognized by cordbg.exe (with the alternative
shorthand notation) once you have entered a debugging session.
Once we opt for creating new project, then, we can see the following window:
This window will allow us to choose the language (C#, VB.NET etc), then type of the
application (windows, console etc). Then we can provide the name of the application and the
location where the application to be stored. Following is a list of project types provided by
C#.
5) C# ”pre-processor” directives.
As the name justifies, preprocessor directives are a block of statements that gets processed before the actual
compilation starts. C# preprocessor directives are the commands for the compiler that affects the compilation
process.
These commands specifies which sections of the code to compile or how to handle specific errors and warnings.
C# preprocessor directive begins with a # (hash) symbol and all preprocessor directives last for one line.
Preprocessor directives are terminated by new line rather than semicolon.
Preprocessor directives in C#