[go: up one dir, main page]

0% found this document useful (0 votes)
32 views11 pages

Dot Net Unit1

Uploaded by

richmakercasio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Dot Net Unit1

Uploaded by

richmakercasio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 1 - Introduction to C# & .

NET platform and Building C#


Applications
What is C#?

C# is pronounced "C-Sharp".It is an object-oriented programming language created by Microsoft that


runs on the .NET Framework. C# has roots from the C family, and the language is close to other popular
languages like C++ and Java.The first version was released in year 2002. The latest version, C# 8, was released
in September 2019.Anders Hejlsberg was involved as the lead developer who developed it when the Dot Net
Framework was created.

C# is used for:
 Mobile applications
 Desktop applications
 Web applications
 Web services
 Web sites
 Games
 Database applications

Why Use C#?


 It is one of the most popular programming language in the world
 It is easy to learn and simple to use
 It has a huge community support
 C# is an object oriented language which gives a clear structure to programs and allows code to be
reused, lowering development costs.
 As C# is close to C, C++ and Java, it makes it easy for programmers to switch to C# or vice versa

Features of C#
There are various features of C# which makes the programming language different and widely used. These are:

 Learning C# is very easy.


 It is a general purpose, easy integrating programming language.
 It is a highly structured programming language.
 Object-oriented concepts can be implemented efficiently using this language.
 It is platform independent, which means the programs written in C# can be executed in a variety of
computing environment.
 Efficient programming can be done using this language.
 It is a high-level programming language.
 GUI applications can be developed very easily using this language.
Programming Features of C#
Other reasons why programming in C# language is so strong and popular are:

 It deals with automatic garbage collections.


 It has a lot of standard libraries.
 Some properties and events can make programming smarter.
 Delegates and concepts of event management can also help in making the language strong.
 It supports Multi-threading.
 It has the concepts of Indexers.
 Generic concepts are easy to use in C#.

.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

 It is a platform neutral framework.


 It is a layer between the operating system and the programming language.
 It supports many programming languages, including VB.NET, C# etc.
 .NET provides a common set of class libraries, which can be accessed from any .NET based
programming language. There will not be separate set of classes and libraries for each language. If you
know any one .NET language, you can write code in any .NET language.
 In future versions of Windows, .NET will be freely distributed as part of operating system and users
will never have to install .NET separately.

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.

COM Technology( Component Object Model)


– Monolithic approach leads to many problem of maintainability and testing
– A program is broken into number of independent components where each one offers a particular service
– Each component can be developed and tested independently and then integrated into main system.
– Benefits:
– Reduces the overall complexity of software.
– Enables distributed development across multiple organization or departments.
– Enhances software maintainability

1) The .NET Solution

• 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)

Common Language Runtime (CLR)


 The runtime layer is properly referred to as the Common Language Runtime (CLR).
 The primary role of the CLR is to locate, load, and manage .NET types.
 The CLR also takes care of a number of low-level details such as memory
management and performing security checks.

The CLR provides a number of services that include:


 Loading and execution of codes
 Providing metadata
 Automatic garbage collection
 Enforcement of Security
 Managing exceptions and errors
 Provide support for debugging and profiling

Common Type System (CTS)


 The Common Type System (CTS) specification fully describes the entities like all
possible data types and programming constructs supported by the runtime.
 It specifies how these entities can interact with each other.
 It also specifies how they are represented in the .NET metadata format.
Given below is a list of CTS supported data types:
Data Type :System.Byte, System.Int16 System.Int32 System.Int64

Common Language Specification (CLS)


 A given .NET –aware language might not support each and every feature defined by
the CTS.
 The Common Language Specification (CLS) is a related specification that defines a
subset of common types and programming constructs that all .NET programming
languages can agree on.
 Thus, the .NET types that expose CLS-compliant features can be used by all .NET-
aware languages.
 But, a data type or programming construct, which is outside the bounds of the CLS,
may not be used by every .NET programming language.
The three compliance levels with their brief description are given below:
Compliant producer
The component developed in this type of language can be used by any other language.
Consumer
The language in this category can use classes produced in any other language. In simple words this
means that the language can instantiate classes developed in other language. This is similar to how COM
components can be instantiated by your ASP code.
Extender
Languages in this category can not just use the classes as in CONSUMER category; but can also extend
classes using inheritance.

3) The Role of the Base Class Libraries


 In addition to the CLR and CTS/CLS specifications, the .NET platform provides a base class library
that is available to all .NET programming languages.
 this base class library encapsulate various primitives such as threads, file input/output (I/O), graphical
rendering, and interaction with various external hardware devices,
 it also provides support for a number of services required by most real-world applications.
 For example, the base class libraries define types that facilitate database access, XML manipulation,
programmatic security, and the construction of web-enabled (as well as traditional desktop and
console-based) front ends.
 From a high level, you can visualize the relationship between the CLR, CTS, CLS, and the base class
library, as shown in Figure 1-1.

The CLR, CTS, CLS, and base class library relationship


The .NET Framework has an extensive set of class libraries. This includes classes for:

4) Additional .NET-Aware Programming Languages

Understand that C# is not the only language targeting the .NET platform.

Table 1-1. A Sampling of .NET-Aware Programming Languages


.NET Language Web Link Meaning in Life
http://www.oberon.ethz.ch/oberon.net Homepage for Active Oberon .NET.
http://www.usafa.af.mil/df/dfcs/bios/ Homepage for A# (a port of Ada to the .NET )
mcc_html/a_sharp.cfm
http://www.netcobol.com For those interested in COBOL .NET.
http://www.eiffel.com For those interested in Eiffel .NET.
http://www.dataman.ro/dforth For those interested in Forth .NET.
http://www.silverfrost.com/11/ftn95/ For those interested in Fortran .NET.
ftn95_fortran_95_for_windows.asp
http://www.vmx-net.com Yes, even Smalltalk .NET is available.
5) The Role of the Common Intermediate Language
 CIL is a language that sits above any particular platform-specific instruction set.
 Regardless of which .NET-aware language you choose (like C#, VB.NET,
VC++.NET etc).the associated compiler produces CIL instructions.
 Once the C# compiler (csc.exe) compiles the source code file, you end up with a
single-file *.exe assembly that contains a manifest, CIL instructions, and metadata
describing each aspect of the program.

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.

6) The Role of .NET Type Metadata


 In addition to CIL instructions, a .NET assembly contains full, complete, and accurate
metadata.
 The metadata describes each and every type (class, structure, enumeration etc.)
defined in the binary, as well as the members of each type (properties, methods,
events etc.).
 It is always the job of the compiler (not the programmer) to produce the latest and
greatest type metadata.
 As .NET metadata is so careful, assemblies are completely self-describing entities and
so .NET binaries have no need to be registered into the system registry.
 Metadata is used by numerous aspects of the .NET runtime environment, as well as
by various development tools.
 For example, the IntelliSense feature provided by Visual Studio 2005 is made
possible by reading an assembly’s metadata at design time.
 Metadata is also used by various object browsing utilities, debugging tools, and the
C# compiler itself.
 To be sure, metadata is the backbone of numerous .NET technologies including
remoting, reflection, late binding, XML web services, and object serialization.
7) The Role of the Assembly Manifest
 The .NET assembly also contains metadata that describes the assembly itself,
technically termed a manifest.
 Among other details, the manifest documents all external assemblies required by the
current assembly to function correctly, the assembly’s version number, copyright
information, and so on.
 Like type metadata, it is always the job of the compiler to generate the assembly’s
manifest.
 The manifest documents the list of external assemblies required by *.exe(via the
.assembly extern directive) as well as various characteristics of the assembly itself
(version number,module name, and so on).
8) A tour of the .NET namespaces.
 Unlike C, C++ and Java, the C# language does not provide any language-specific
code library.
 Instead, C# provides the language-neutral .NET libraries.
 To keep all the types within the base class libraries, the .NET platform makes
extensive use of the namespace concept.
 A namespace is a grouping of related types contained in an assembly.
 In other words, namespace is a way to group semantically related types (classes,
enumerations, interfaces, delegates and structures) under a single umbrella.
 For example, the System.IO namespace contains file I/O related types, the
System.Data namespace defines basic database types, and so on.
 It is very important to point out that a single assembly (such as mscorlib.dll) can
contain any number of namespaces, each of which can contain any number of types.

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

Table 3-1. A Sampling of .NET Namespaces


Accessing a Namespace Programmatically
A namespace is a convenient way to logically understand and organize related types.
Consider the System namespace. From programmer’s perspective, System.Console represents
a class named Console that is contained within a namespace called System. However, in the
eyes of the .NET runtime, it is a single entity named System.Console.

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 –

using System; // General base class library types.


using System.Drawing; // Graphical rendering types.

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

Option Meaning in Life

/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:library This option builds a single-file *.dll assembly.


/target:module This option builds a module.Modules are elements of multifile assemblies

/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.

3) The Command-Line Debugger (cordbg.exe)


Before moving on to our examination of building C# applications using TextPad, I would like to briefly
point out that the .NET Framework 2.0 SDK does ship with a command-line debugger named cordbg.exe. This
tool provides dozens of options that allow you to debug your assembly. You may view them by specifying the /?
flag: cordbg /?

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.

Table 2-3. A Handful of Useful cordbg.exe Command-Line Flags

Flag Meaning in Life


b[reak] Set or display current breakpoints.
del[ete] Remove one or more breakpoints.
ex[it] Exit the debugger.
g[o] Continue debugging the current process until hitting next
breakpoint.
o[ut] Step out of the current function.
p[rint] Print all loaded variables (local, arguments, etc.).
si Step into the next line.
so Step over the next line.

4) Using the visual studio .NET IDE & its debugging


Till now, we have discussed how to compile C# programs in command prompt. Visual Studio
.NET provides an IDE (Integrated Development Environment) to build applications using any
number of .NET-aware languages like C#, VB.NET, J#, MFC etc.
Here, we will examine some of the core features of VS.NET IDE.

The VS .NET Start Page


When we launch VS .NET, we can see start page as –
This will provide an option to open existing projects or to create new projects.

Creating a VS .NET Project Solution

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.

The preprocessor directives available in C# are:

Preprocessor directives in C#

You might also like