Module-1 With Content
Module-1 With Content
NET [20MCA42]
Module – 1:
Chapter – 1: Getting started with .NET Framework 4.0
1.1 C# Understanding Previous Technologies
1.2 Components of .NET Framework 4.0
1.3 Benefits of .NET Framework
1.4 Architecture of .NET Framework 4.0
1.5 .NET Execution Engine CLR
1.6 CTS
1.7 Metadata and Assemblies
1.8 .NET Framework Class Library
1.9 Windows Forms
1.10 ASP .NET and ASP .NET AJAX
1.11 ADO .NET,
1.12 Windows workflow Foundation
1.13 Windows Presentation Foundation,
1.14 Windows Communication Foundation,
1.15 Widows CardSpace
1.16 LINQ
Chapter - 2: Introducing C#
2.1 Creating a Simple C# Console Application
2.2 Identifiers and Keywords
2.3 System Data Types
2.4 Variables and Constants
2.5 Value Types
2.6 Reference Types
2.7 Understanding Type Conversions
2.8 Boxing and UnBoxing
2.9 Namespaces
2.10 The System namespace
2.11 .NET ArrayTypes
Prepared by
Mrs. Suma M G
Assistant Professor
Department of MCA
RNSIT
Bengaluru – 98.
Module – 1 20MCA42
Chapter - 1
Evolution of .NET:-
At the time of mid ninety, java was so popular to develop the application because of its platform independent
technique and open source policy of Sun Microsystems. Around 2002, Sun Microsystems introduced the Java
2 Enterprise Edition (J2EE) to create the distributed application.
As the J2EE was gaining popularity, the market share of Microsoft ultimately led to the turn down. Therefore,
Microsoft starts working on a project known as Next Generation Windows Services (NGWS) to recover the
market share. The project took around three years to build up, which is now known as .NET. Microsoft released
the different version of .Net with the different name as listed below:
Drawbacks of VB are:
VB is not fully object oriented
Developing multi-threading applications using VB is a bit complex as we have to call the low level
WIN32 API calls.
No need to declare the data type of variables prior to its usage, there will be more chances to get
runtime errors.
d) JAVA / J2EE:
Advantages:
i. Platform Independent
ii. It contains large number of pre-defined packages that contain various type definitions.
iii. Java programmer can build pure Java applications complete with database connectivity, messaging
support, web-enabled front ends, and a richer user interface.
iv. Java eliminates pointers which is major concern in C and C++
v. Garbage collection is one of the powerful features, which automatically cleans the memory.
Disadvantages:
i. Java is a very elegant language that means must use of Java front-to-back during development cycle
ii. Java applets are not supported by all the browsers and they need separate plug-in to be installed
iii. Java developers have less control over on garbage collection to free objects
iv. Primary goal of Java is to make Java as a single programming language for every need. So Java offers
a little chance for language integration.
⚫ Cross-Platform Support: Specifies that any windows platform that supports CLR can execute .NET
application, that is, .NET application enables interoperability between multiple Windows operating
systems.
⚫ Language Interoperability: Enables code written in different languages to interact with each other.
This allows reusability of code and improves the efficiency of the development process.
⚫ Automatic Management of Resource: In .NET, no need to manually free the application resources,
such as files, memory, network, and database connection because .NET Framework provides a feature
called CLR that automatically tracks the resource usage and helps you in performing the task of manual
resource management.
⚫ Ease of Deployment: In most cases, to install an application, you need to copy the application along
with its components on the target computer. But in .NET, applications are deployed in the form of
assemblies that’s why, do not affect the existing applications. Therefore, registry entries are not required
to store information about applications and also version problem is eliminated in .NET Framework.
The following figure shows the process of compilation and execution of the code by the JIT Compiler:
Managed Code: Managed code is the code that is executed directly by the CLR. The application that are created
using managed code automatically have CLR services, such as type checking, security, and automatic garbage
collection.
The process of executing a piece of managed code is as follows:
Selecting a language compiler
Compiling the code to IL[This intermediate language is called managed code]
Compiling IL to native code
Executing the code
Suma M G, Asst. Prof, Dept. of MCA, RNSIT 5 | 29
Module – 1 20MCA42
Unmanaged Code: Unmanaged Code directly compiles to the machine code and runs on the machine where it
has been compiled. It does not have services, such as security or memory management, which are provided by
the runtime. If your code is not security-prone, it can be directly interpreted by any user, which can prove
harmful.
Automatic Memory Management: CLR calls various predefined functions of .NET framework to allocate and
de-allocate memory of .NET objects. So that, developers need not to write code to explicitly allocate and
de-allocate memory.
The base class of all the types is the System.Object class. The hierarchy of the types as shown figure 1.4:
The following table shows some of the differences between values types and reference types.
• Represent base data types and • Access information about loaded types
exceptions • Invoke .NET Framework security checks
• Encapsulate data structures • Provide data access, rich client-side GUI, and
• Perform I/O server-controlled, client-side GUI
AJAX, which is also known as Atlas, is used as an extension for ASP.NET to develop and implement the
AJAX functionality. ASP.NET AJAX has both side client and server components to develop the Web
applications, which can update the information on a Web application without reloading the page completely.
Advantages of using AJAX:
• Asynchronous: Enables asynchronous calls to the Web server without making the users wait for the data.
• Minimal transfer of data: Helps in sending only a part of the modified data to the web server to minimize
the network traffic and perform the operations quicker.
• Minimal processing on the Web server: Minimizes the processing on the Web server as only the
necessary data needs to be sent. Now, the server is not required to send a full page back to the user.
• Context: Reduces burden on the server by sending a request back to the server through the client-side
script. AJAX-based applications implement partial page post backs instead of full page post backs, which
reduce the data traffic between the client and server.
1.17 LINQ:
LINQ is an acronym for Language Integrated Query, which is one of the components of .NET Framework 4.0
that adds native data querying capabilities to .NET languages by using syntax similar to SQL.
To print that list, you have to write the following lines of code:
Visual Basic In this code, you can find that the syntax of the query is similar to
For Each name In query the SQL, which we normally follow for accessing relational data.
Response. Write(name) It is important to note that even if it looks similar to SQL, the LINQ
Next
query displayed is not an SQL statement. Instead, it is purely in C#
C# language.
foreach(var name in query){ LINQ queries resemble SQL, they are not restricted to accessing
Response.Write(name);
only relational databases. LINQ enabled data access components
} are as follows:
→ LINQ to ADO.NET:- Includes two options, LINQ to SQL, which translates a query into an SQL query,
and then issues it against the tables specified in the query in an SQL Server database, and LINQ to
DataSet, which executes a query on the contents of a DataSet.
→ LINQ to Object: - Allows querying objects in a collection. LINQ to Objects is not dynamic. After
creating and using the result set, any changes made to the source collection do not automatically update
the result set.
→ LINQ to XML: - Allows querying of XML data. In addition, it helps in creating and manipulating XML
data. This option has a different syntax; however, the basic LINQ query remains the same.
Additional Topics:
CLS [Common Language Systems]:
The Common Language Specification (CLS) is a set of rules that a given .NET-aware compiler must support to
produce code that can be hosted by the CLR, while at the same time be accessed in a uniform manner by all
languages that target the .NET platform. In many ways, the CLS can be viewed as a subset of the full
functionality defined by the CTS.
The CLS helps enhance and ensure language interoperability by defining a set of features that developers can
rely on to be available in a wide variety of languages. The classes that follow the rules specified by CLS are
termed as CLS-complaint classes. The classes defined in the .NET Framework class library are CLS-Compliant.
The CLS describes a set of features that different languages have in common. CLS Compliance is especially
important when creating software components that will be used by other languages.
The most important rules, and which apply to public and protected members are
i. All types appearing in a method prototype must be CLS-compliant
ii. Array elements must have a CLS-compliant element type. Arrays must also be 0-indexed
A CLS compliant class must inherit from a CLS-compliant class only
Introducing C#
2.1 Creating a Simple C# Console Application:
A class declaration in C# is composed of attributes, modifiers, the class name, base class and interfaces, and a
body. Attributes, modifiers, and bases are all optional.
Example 2.1
It is declared inside a class. It must be
using System;
static and it should not be public.
class HelloClass {
public static int Main(string[] args){ C# executable program must include
Console.WriteLine("Hello World!"); the Main() method in any one of the
Console.ReadLine(); classes.
return 0;
} public: is an access modifier that tells
} the main method is accessible by
anyone.
static: the Main method is a global one and can be called without creating an instance of the class.
void: is a type modifier, that the Main method does not return any value.
string[] args: method has single parameter which happens to be an array of strings and also contain any
number of incoming command – line – arguments.
When a program starts, it looks for an entry point. This is the role of the Main() method. In fact, a
program, that is an executable program, starts by, and stops with, the Main() method. They are...
i. public static void Main(string[] args) { -------} //No Return Type and String Arguments
ii. public static void Main() { ------- } //No Return Type and No Arguments
iii. public static int Main(string[] args) { - - - } //Integer Return Type & String Argument
iv. public static int Main() { ---------- } //Integer Return Type and No Arguments
Keywords are the reserved words whose meanings are predefined to the C# compiler.
Rules:
⚫ You cannot use keywords as ⚫ If you want to use the keywords as identifiers,
variable, methods and properties prefix the keyword with @ character.
Table 2.1: Lists of Reserved keywords available in C#
Reserved Keywords
abstract as base bool break byte case this object
Catch char checked class const continue decimal is sizeof
Default delegate do double else enum event out readonly
Extern false finally fixed float for if param using
foreach goto int public protected private short namespace unchecked
Figure 2.1: Shows different types of value types and reference types
Example 2.1:
using System;
namespace Class_Demos{
class TypeMaxMin{
static void Main(){
Console.WriteLine("System Minimum");
Console.WriteLine("MinValue of sbyte{0}", sbyte.MinValue);
Console.WriteLine("System Maximum");
Console.WriteLine("MaxValue of sbyte{0}", sbyte.MaxValue);
Console.ReadLine();
} } }
Can apply only public, private and internal access modifiers to a struct and member definition.
Cannot use the protected and protected internal access modifiers in structs because structs are
implicitly sealed. This means that a struct cannot be a parent class of other classes.
The struct types are divided into the following 5 categories:
⚫ Integral Type: C# provides a set of basic data types which are known as integral types such as sbyte,
byte, short, ushort, int, uint, long, ulong & char
⚫ Floating-Point Type: It is divided into two types; they are, single and double floats.
🡆🡆 Single float holds – is similar to the float data type, hold values up to 10308.
🡆🡆 Double float holds – is similar to the double data type, hold values up to 10308.
⚫ Decimal Type: It is equivalent to a long integer
⚫ Boolean Type: true or false
⚫ Nullable Type: when dealing with databases or when a user does not want to assign a value to a variable
of any data type.
Enum Type:
Enumerations are the user-defined integer data types that are declared using the “enum” keyword. You can
define a set of named integral constants that can be assigned to a variable.
Example 2.2:
namespace Class_Demos{
enum Color { Red=1, Green, Yellow}
class EnumEx{
public static void Main(){
Console.WriteLine("Select\n1.Red\n2.Green\n3.Yellow");
int colopt =Int32.Parse(Console.ReadLine());
Color col=(Color)colopt;
switch (col) {
case Color.Red:
Console.WriteLine("The selected color is RED");
break;
case Color.Green:
Console.WriteLine("The selected color is Green");
break;
case Color.Yellow:
Console.WriteLine("The selected color is Yellow");
break;
}
} } }
Dynamic type:
You can store any type of value in the dynamic type variable. It performs the type checking of the dynamic type
variable at run time.
Syntax: dynamic dyn=10;
Object type:
You can assign values of any type to the variable of object type. It performs the type checking of the variable
of object type at compile time.
Object type is base class for the other types such as value types, reference types, predefined types, user-defined
types. The object type can be assigned a value of any other types, but requires the type conversion.
⚫ A value type converted to the object type, the Example:
process is known as boxing int i=100
⚫ An object type converted to the value type, the object obj = i; // this is boxing
process is known as unboxing. int j=obj; //this is unboxing
String type:
It enables you to assign string values to the variables of string type. The string type is an alias for the
System.String class. String type is directly derived from the object type. You cannot change its value.
The value for a string type can be assigned using the string literals in 2 forms: quoted and @quoted.
Example: Where, @- Verbatim string literals: String
string sname = “C# 2010”; literals are embed escape characters
@“C:\Windows\Microsoft.NET\Framework”
Classes:
All classes are user-defined data types and are considered as objects. class class_name{
All objects are derived from a single base class, known as static void Main(){
System.Object. - - - - -
}
}
Interface:
Interface define a set of functionalities that can be related to any class or struct. You cannot instantiate an object
through an interface. Interface have method declarations, these are known as abstract ie, they do not have
interface interface_name{ implementation part.
Abstract method delcarations(); A class that implements all the methods which is
- - - - - - - - declared in the interface; otherwise, the compiler
}
raise an error.
Delegates:
A delegates is almost equivalent to function pointer in C++ or C. C# contains two kinds of objects – those that
create a change and respond to the change.
A delegate acts as the tunnel between the two kinds of objects:
• Moving information from one side to another
• C# delegates are class methods and can be either static or instance class methods.
A delegate can keep a track of its own state by maintaining information in the object to which it belongs. You
can create a delegate by using delegate keyword, as shown below:
public delegate int Intdelegate(int i);
Example 2.4
namespace Class_Demos{
public delegate int intDelgate(int a);
class Calulator {
public static int SquareNum(int num){
return num * num;
}
}
class DelegatEx{
public static void Main(){
Console.WriteLine("Demonstrating Simple Delegate");
intDelgate obj = new intDelgate(Calulator.SquareNum);
Console.WriteLine("Value" + obj(2));
Console.Read();
} } }
NOTE: For all the data types, the base class is System.Object class.
The following are the differences between pointer type and reference type:
Both, pointer and references represent memory address but the difference is that,
⚫ References are tracked by the garbage collector and pointer are not.
⚫ You can perform arithmetic operations on pointers but not on references
Method Description
ToBoolean Converts types to a Boolean value, where possible
ToByte Converts a type to a byte.
ToChar Converts a type to a single Unicode character, where possible.
ToDateTime Converts a type (integer or string type) to date-time structures.
ToDecimal Converts a floating point or integer type to a decimal type.
ToDouble Converts a type to a double type.
ToInt16 Converts a type to a 16-bit integer.
ToInt32 Converts a type to a 32-bit integer.
ToInt64 Converts a type to a 64-bit integer.
ToSbyte Converts a type to a signed byte type.
ToSingle Converts a type to a small floating point number.
ToString Converts a type to a string.
ToType Converts a type to a specified type.
ToUInt16 Converts a type to an unsigned int type.
ToUInt32 Converts a type to an unsigned long type.
ToUInt64 Converts a type to an unsigned big integer.
Example 2.5:
using System;
namespace Class_Demos{
class StringEx1{
public static void Main() {
int i = 75;
string str = "12";
Console.WriteLine(i.ToString());
Console.WriteLine(Convert.ToInt32(str));
Console.Read();
} } }
From To
sbyte short, int, long, float, double , decimal
byte short, ushort, int, uint, long, ulong, float, double , decimal
short int, long, float, double , decimal
ushort int, uint, long, ulong, float, double , decimal
int long, float, double , decimal
uint long, ulong, float, double , decimal
long float, double , decimal
char ushort, int, uint, long, ulong, float, double , decimal
float double
ulong float, double , decimal
Explicit Conversion:
When one data type is converted Example:
explicitly to another data type, with float i=123.45f;//explicitly from floating-point to integer
the help or pre-defined functions. int x=(int)i;
C# allows to convert explicitly from type to another type ie, listed below:
Table 2.5: Lists of Explicitly conversion
From To
sbyte byte, ushort, uint, ulong, or char
byte sbyte, char
short sbyte,byte, ushort, uint, ulong, char
ushort sbyte, byte, short, char
int sbyte, byte, short, ushort, uint, ulong, char
uint sbyte, byte, short, ushort, uint, ulong, char
long sbyte, byte, short, ushort, uint, ulong, char
ulong sbyte, byte, short, ushort, uint, ulong, char
char sbyte, byte, or short
float sbyte, byte, short, ushort, int, uint, long, ulong, char, decimal
double sbyte, byte, short, ushort, int, uint, long, ulong, char, float, decimal
decimal sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double
class StringEx2{
Example 2.6: public static void Main(){
double d = 55673.745;
int i;
i = (int)d;
Console.WriteLine(i);
Console.Read();
} }
2.9 Namespaces:
A namespaces is wrapper that is wrapped around one or more structural elements to make them unique and
differentiated from other elements.
To declare namespace in C# .Net has a reserved keyword namespace XYZ{
“namespace”. class class_name{
- - - - - -
If a new project is created in Visual Studio .NET it
}
automatically adds some global namespaces. }
These namespaces can be different in different projects.
But each of them should be placed under the base namespace “System”.
All namespaces should import by using keyword, which can tell the compiler which namespaces and
libraries of the code you want to use in the system.
Namespaces Meaning
These namespaces define a number of stock container objects
System.Collections (ArrayList, Queue, and so forth), as well as base types and interfaces
System.Collections.Generic that allow you to build customized collections. As of .NET 2.0, the
collection types have been extended with generic capabilities
System.Data System.Data.Odbc These namespaces are used for interacting with databases using
System.Data.OracleClient ADO.NET.
System.Data.OleDb
System.Data.SqlClient
Here, you find numerous types that can be used to programmatically
System.Diagnostics
debug and trace your source code.
using System;
namespace Examples{
class ArrayBaseDemo{
static void dumpArray(string[] a){
foreach(string i in a ){
Console.WriteLine(i);
}
Console.WriteLine();
}
static void Main(){
string[] a = { "alpha", "beta", "gamma", "delta",
"epsilon" };
Console.WriteLine("The Array:");
dumpArray(a);
Array.Reverse(a);
Console.WriteLine("The Reverse list:");
dumpArray(a);
Array.Clear(a, 1, 2);
Console.WriteLine("After cleared list:");
dumpArray(a);
Console.ReadKey();
} } }
12. What are namespaces? List and explain the purpose of any three namespaces 05