UNIT 1 Introduction to the C# language
.NET Philosophy
• Origin of .NET Technology
• .NET Framework
• CLR - CTS, CLS, MSIL, JIT Complier, Garbage Collector
• Framework Base Classes
• User and program interfaces (ASP .NET)
• Benefits of the .NET Approach
.NET Philosophy
To understand .NET philosophy it is necessary to understand drawbacks of various programming
languages:
C : Lacks object-oriented concepts (as it is procedure oriented)
C++ : Not fully object-oriented, includes manual memory management, syntax ridden, error prone
Java : Not appropriate for developing graphical applications and does not support multi language
integration
Visual Basics : It is not fully object-oriented, does not support multi-threading, no inheritance
.NET is a software framework that includes everything required for developing software for web
services and stand-alone applications.
It integrates presentation technologies, component technologies and data technologies on a single
platform so as to enable users to develop Internet applications.
Origin of .NET Technology
The .NET technology has evolved through three significant
phases:
1. OLE (Object Linking and Embedding) Technology
2. COM (Component Object Model) Technology
3. .NET Technology
1.OLE (Object Linking and Embedding) Technology
Developed by Microsoft to enable easy inter-process communications.
OLE provides support to achieve following:
To embed documents from one application into another application
To enable one application to manipulate objects located in another application
Example: MS Word from MS PPT
2. COM (Component Object Model) Technology
A program can be divided into number of independent components where each one offers a
particular service.
Each component can be developed and tested independently and then integrated into the main
system.
This technology is known as Component Object Model (COM) and the software built using COM
is referred to as component-ware.
Overcomes the problems of maintaining and testing of software.
1
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Benefits:
• Reduces Complexity
• Enhances software maintainability
• Enables distributed development across multiple organizations
3. .NET Technology
• It is third generation component model
• Provides a new level of inter-operability compared to COM technology
• Inter-module communication is achieved using Microsoft Intermediate Language (MSIL) or simply IL
• IL allows for true cross language integration
• IL also provides metadata: describes characteristic of data including data types and locations.
• It also includes host of other languages and tools that enables to develop and implement Web-based
applications easily.
.NET Framework
.NET framework is a software framework that includes everything required for developing Software for
web services.
Provides an environment for building, deploying, and running web applications and console
applications.
It provides a new and simplified model for designing and deploying application programs on windows
platform.
.NET platform provides new environment for creating, running robust, scalable & distributed
applications over the web.
It supports VB, C#, and C++.
Offers a new programming model that allows programs created in different programming language to
communicate with each other.
What is software framework?
Software abstraction that provides general functionality
Functionality can be selectively changed by programmer to provide application specific software.
It is reusable software platform to develop applications.
The .Net frame work consists of three distinct
technologies:
1. Common Language Runtime
2. Framework Base Classes
3. User and program interfaces (ASP .NET and Windows
Forms)
Common Language Runtime (CLR)
CLR is the (heart and soul) basic and Virtual Machine component of the .NET Framework.
.NET CLR is a run-time environment that manages and executes the code written in any .NET
programming language.
It converts code into native code which further can be executed by the CPU.
It is responsible for loading and running programs.
It Supports cross-language inter-operability.
2
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Services or Functions of CLR:
• Loading and execution of programs
• Verification of type-safety
• Providing metadata
• Garbage Collection (Memory Management)
• Interoperability with other systems
• Managing exceptions and errors
• Debugging
• Enforcement of security
• Compilation of IL into native executable code
• Memory isolation for applications
• Working with Various programming languages
Workflow of CLR
• When the application is executed, the source code is compiled to IL while metadata engine creates
metadata information.
• IL and metadata are linked together with the native code.
• During execution, the IL code and any other requirement from base class library are brought
together by the class loader.
• The combined code is tested and then compiled by JIT to produce native machine code which is
sent to the runtime execution manager for execution.
Main Components of CLR
• Common Language Specification (CLS)
• Common Type System (CTS)
• Microsoft Intermediate language (MSIL)
• Managed Code
• Garbage Collection (GC)
• Just In – Time Compiler (JIT)
Common Type System (CTS)
.NET Framework provides multiple language support by the feature called CTS and is built into the
CLR
CTS supports variety of types and operations found in most programming languages
Calling one language from another language does not require type conversion
We can build .NET programs in number of languages including C#, C++, Visual Studio, etc.
Example for CTS: C# has an int data type and VB.NET has Integer data type. Hence a variable
declared as an int in C# and Integer in VB.NET, finally after compilation, uses the same structure
Int32 from CTS.
3
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Common Language Specification (CLS)
It defines rules and regulations that enables interoperability on the .NET platform
These rules serve as a guide to third party compiler designers and library builders
CLS is subset of CTS
Various .NET compiler differ in their syntactic rules and these compilers generate the CIL
(common intermediate language) instruction which are executed by the CLR
In .NET each language is converted into MSIL code after compilation and the MSIL code is
language specification of CLR.
Microsoft Intermediate Language (MSIL) Code
MSIL is simply called as IL
It is an instruction set into which all the .NET programs are compiled
It is similar to assembly language and contains instructions for loading, storing, initializing and
calling methods
Programs (C#) written in a CLS compliant language are compiled into MSIL
Just In Time Compilers (JITers)
In.NET Framework, all the .NET complied languages
use a Common Language Runtime, which generates
MSIL after compilation
Before the MSIL can be executed, it must be converted
by a .NET Framework Just-In-Time (JIT) compiler to
native code
Native code is CPU-specific code that runs on the same
computer architecture as the JIT compiler.
Garbage Collection (GC)
The garbage collector (GC) manages the allocation and de-allocation of memory.
It serves as an automatic memory manager.
Programmer need to know how to allocate and release memory or manage the lifetime of the
objects that use that memory.
An allocation is made any time when an object is created with a “new” keyword or a value type is
boxed.
When there is not enough memory to allocate an object, the GC collects and disposes the garbage
memory to make memory available for new allocations. This process is known as garbage
collection.
4
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Managed Code
• CLR is responsible for managing the execution of code compiled for the .NET platform.
• The code that satisfies the CLR at runtime in order to execute is known as managed code. This code
is directly executed by CLR with help of managed code execution. Any language that is written in
.NET Framework is managed code.
Framework Base Classes
Allows to implement applications quickly
The functionality of the base framework classes resides in the namespace called System
It is a standard library that is a collection of thousands of classes and used to build an application.
.NET Framework Class Library is the collection of classes, namespaces, interfaces and value types
that are used for .NET applications.
It provides types for strings, dates, numbers, etc. The Class Library includes APIs for reading and
writing files, connecting to databases, drawing, and more.
Different tasks of Framework base class
• Input/output operations
• String handling
• Managing arrays, lists, maps, etc
• Accessing files & file systems
• Accessing the registry
• Security
• Windowing
• Database management
• Drawing
• Managing errors & exceptions
• Connecting to Internet
User and Program Interfaces
The .NET framework provides the following tools for managing user &
application interfaces:
o Windows forms
o Web forms
o Console applications
o Web Services
These tools enable users to develop user-friendly desktop-based as well as web-based applications.
Benefits of the .NET Approach
• The .NET technology provides several benefits to developers and users. Some of them are:
Simpler and faster systems development
Enhanced built-in functionality
Many ways to communicate with the outside world
Integration of different languages into one platform
Easy execution
Wide range of scalability
Inter-operability with existing applications
Fewer bugs
Potentially better performance
5
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Differences between C# and C++
C# is high level object oriented language. C++ is a low level language.
In C# pointers can be used only in unsafe mode. In C++ pointers can be used anywhere in the
program.
C# is used to develop mobile, windows, and C++ is typically used for console applications.
console applications
C# code gets converted into intermediate language C++ code gets converted into machine code directly
code after compilation. after compilation.
C# is a pure object-oriented programming language C++ is not a pure object- oriented programming
language
C# does not support any multiple inheritances C++ support multiple inheritance through classes.
through classes.
In C# memory management is performed In C++ memory management is performed
automatically by the garbage collector. manually by the programmer.
C# supports for each loop. C++ does not support for each loop
Differences between C# and Java
C# is an object-oriented programming language Java is a high level, robust, secured and object-
developed by Microsoft that runs on .Net oriented programming language developed by
Framework. Oracle.
C# supports CLR(Common Language Runtime). Java supports JVM(Java Virtual Machine).
In C#, built-in data types that are passed by value In java, built-in data types that are passed by value
are called simple types. are called primitive types.
Arrays in C# are specialization of System. Arrays in Java are direct specialization of Object
C# supports goto statement. Java doesn't support goto statement.
C# supports structures and unions. Java doesn't support structures and unions.
C# supports unchecked exception. Java supports checked exception and unchecked
exception.
In C# you can use pointer only in an unsafe mode. Java does not support pointers.
Differences between C# and Visual Basic
C# Visual Basic
It is used to create a variety of application that runs It is also used to develop various applications
on the.NET Framework running on the .NET Framework.
It supports only structured error handling. VB.NET supports structured and unstructured error
handling.
Events are not possible in C#. Events are automatically bound.
It uses a simple programming structure as C, Java, Whereas, it uses Simple English for defining the
Python, C++, etc. structure
int x; Dim x As Integer
int x = 10; Public x As Integer = 10
Each statement does not end with a semicolon. Each statement is terminated with a semicolon (;)
It is a case sensitive language. For example," Hello" It is a case insensitive language. For example,
and "hello" are different. "Hello" and "hello" are the same
6
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Introduction to C#
C# is pronounced as “See Sharp”
C# is a fully Object-oriented programming language and is first Component oriented language
It is developed by Microsoft Corporation
It is designed to support the key features of .NET Framework
C# can be used to develop two categories of programs:
a) Executable application programs
b) Component libraries
Executable programs are written to carry out certain tasks and require Main() method in one of the
classes.
Component libraries do not require Main declaration because they are not standalone application
programs. They are written for use by other applications.
Understanding the HELLO WORLD Program
// C# program to display hello message
using system;
class hello
{
public static void main( )
{
Console.WriteLine(“Hello World”);
Console.ReadLine( );
}
}
In C# programming language, a simple "Hello World" program consists of different parts
A Namespace Declaration
Class Declaration & Definition
Class Members (like variables, methods etc.)
Main Method
Statements or Expressions
Adding Comments
• Comments are not executable statements
• It enhances readability and understanding of the code.
• Types of comments:
a) Single-line comments (//)
b) Multiline comments (/* ……… */)
Namespaces
• C# supports using directive that can be used to import the namespaces into the program.
using System;
Console.WriteLine(“Hello World”);
• Here System is a namespace in which the Console class is located.
• If you wish not to use namespace, then you can write the statement as:
System.Console.WriteLine(“Hello World);
7
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures,
interfaces, enumerators and delegates.
Generally, we use the using keyword to add namespaces in code-behind and class files. Then it makes all
the classes, interfaces and abstract classes and their methods and properties available in the current page.
Standard Namespaces in .NET
System :- It contains classes that allow you to perform basic operations such as mathematical operations
and data conversion.
System.IO :- It contains classes used for input and output operations for a file in C#.
System.Net :- It contains classes that are used to define network protocols.
System.Data :- It contains classes that are used to make ADO.NET data access architecture.
System.Collections:- It contains classes that implement collection of objects such as list.
System.Drawing :- It contains classes that are used to implement GUI functionalities
System.Web:- It contains classes that help implement HTTP protocol to access web pages.
Class Declaration
• In the above program the first line
class hello
The keyword class declares a class by the name hello and creates a template for the object.
Braces or Curly Brackets – { }
• C# is a block structured language
• The code is always enclosed within a pair of braces. Therefore every class in c# begins with an opening
brace „{‟ and ends with a corresponding closing brace „}‟that appears in the last line of the program.
Main Method
• Every C# executable program must include a Main() method that indicates the starting point of program
execution (Note, M is in UPPER-CASE)
• A C# application can have any number of classes but „only one‟ class can have the Main() method to
initiate the execution.
• Main() method can return two types of values – int or void
• When the return type is int, include a return statement at the end of the method.
• The value returned serves as the program‟s termination status code.
• It allows communication of success or failure to the execution environment.
Other Keywords used in Main() Method and its Meaning
public: An access modifier that specifies Main() method can be accessed by anyone
static: Declares Main() method to be global method that can be called without creating its objects.
void: The keyword void is a type modifier that states that the main method does not return any
value but simply prints some text to the screen
Output Line
Console.WriteLine(“Hello World”);
• WriteLine() method is used to display a line of characters on the standard output
stream (screen).
• WriteLine() is a method that belongs to Console class.
• Console is built-in class resides in the System namespace.
8
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Input Line
string name = Console.ReadLine();
• ReadLine() is method that reads a line of characters from the standard input stream
(keyboard).
• ReadLine() method returns a string value and should be stored in string variable.
• ReadLine() is a method that belongs to Console class.
• Console is built-in class resides in the System namespace.
System.Environment Class:
System.Environment class helps to obtain information pertaining to the current working
environment.
This information may include platform information, operating system version, .NET Framework
version, environment variable values etc.
Environment class is static class which provides the system configuration, Current program
execution Environment as well as some properties.
Some of the important members of the System.Environment Class are :
System.Console Class :
A console is an operating system window through which a user can communicate with the operating
system or we can say a console is an application in which we can give text as an input from the keyboard
and get the text as an output from the computer.
Example: Windows Command prompt which accepts MS-DOS commands.
In C#, the Console class is used to represent the standard input, output, and error streams for the
console applications.
We are not allowed to inherit Console class. This class is defined under System namespace.
This class does not contain any constructor. Instead of the constructor, this class provides different
types of properties and methods to perform operations.
Properties:
Property Description
BackgroundColor Gets or sets the background color of the console.
BufferHeight Gets or sets the height of the buffer area.
BufferWidth Gets or sets the width of the buffer area.
CapsLock Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or
turned off.
NumberLock Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or
turned off.
Out Gets the standard output stream.
9
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1
Methods:
Method Description
Beep() Plays the sound of a beep through the console speaker.
Clear() Clears the console buffer and corresponding console window of display information.
Read() Reads the next character from the standard input stream.
ReadKey() Obtains the next character or function key pressed by the user. The pressed key is
displayed in the console window.
ReadLine() Reads the next line of characters from the standard input stream.
Write() Writes the text representation of the specified value or values to the standard output
stream.
WriteLine() Writes the specified data, followed by the current line terminator, to the standard
output stream.
10
KLS Gogte BCA – Belagavi SEM-III C# and .NET Framework –UNIT 1