C#Unit-1 Notes
C#Unit-1 Notes
C#Unit-1 Notes
2. Portability
Applications built on the .Net framework can be made to work on any Windows
platform. And now in recent times, Microsoft is also envisioning to make
Microsoft products work on other platforms, such as iOS and Linux.
4. Open source:
The .NET framework is opensource which inturn is free to use and explore as
well as wide support in development and enhancement of product and
technologies.
5. Large community
The .NET has managed to build a huge community ever since it was launched,
but it was not until it was made open-source by Microsoft that it really exploded.
6. Security
The .NET Framework has a good security mechanism. The inbuilt security
mechanism helps in both validation and verification of applications. Every
application can explicitly define their security mechanism. Each security
mechanism is used to grant the user access to the code or to the running program.
7. Simplified deployment
The .Net framework also have tools, which can be used to package applications
built on the .Net framework. These packages can then be distributed to client
machines. The packages would then automatically install the application.
8. Simplicity
Executing usual jobs using the .NET platform is very simple and straightforward.
Besides, the submission of forms is very easier, and also there are site design,
deployment, and client consent.
9. Monitoring
Ultimately, the net platform also stands for its monitoring automation. It will
punctually notice any issues such as immense loops, memory leaks, etc. Not only
this, but it will also terminate these activities automatically and restart itself.
.NET Framework architecture:
Garbage Collection
Garbage collection is the process of removing unwanted resources when they
are no longer required.
Examples
A File handle which is no longer required. If the application has finished all
operations on a file, then the file handle may no longer be required.
Components of CLR:
A developer can develop an application in a variety of .Net programming
languages.
Language – The first level is the programming language itself; the most
common ones are VB.Net and C#.
Compiler – There is a compiler which will be separate for each programming
language. e.g., C#, you will have its own compiler. This compiler will generate
intermediate code.
Common Language Interpreter – CLI is the interpreter that takes the compiler
generated code in intermediary language. Programs written in all the languages
will be generated in common intermediary language. Which will then be
converted to byte code and executed.
Class Library
The .NET Framework includes a set of standard class libraries. A class library is
a collection of methods and functions that can be used for the core purpose.
For example, there is a class library with methods to handle all file-level
operations eg., Methods intended for performing reading and writing operations
on the file.
3. Languages
The types of applications that can be built in the .Net framework is classified
broadly into the following categories.
Interoperability
The platform allows a lot of backend support. In case if the user had an
application created on an older version like 2.0 of this framework. And if he
tried to run the same application on a system that includes the higher version of
the platform, such as 3.5. Besides, the application would still continue to work.
This is because Microsoft ensures with every release that the older framework
versions also support well with the newer version.
Portability
Apps build on the .Net platform can be made to perform on any Windows
platform. And now in present times, Microsoft is also imagining to make its
products work on other platforms. Like iOS and Linux OS.
Security
The .NET framework includes a good security system. The inbuilt security
system helps in both validation and confirmation of apps. Every app can define
its security system in a clear and complete manner. Moreover, each security
system is useful to allow the user access to the code or to the running program.
Simplified deployment
The .Net framework includes some tools that are useful to package applications
built on the .Net platform. Later, these packages can be spread to client systems.
Further, the packages would automatically install the application.
Version compatibility
With rare anomalies, apps that are built by using a specific version of.NET
framework run without change in a later version. This is a great thing here.
.NET Remoting
.Net framework Remoting now supports Ipv6 addresses and the exchange of
common types. .NET facilitates authentication, encryption and other security
parameters. This can enhance the execution of network load-balancing by
remote clusters.
Parallel Computing
The .Net Framework ver.4 presents a new program model for writing
multithreaded and asynchronous code. This model greatly clarifies the job of an
application and library developers. The latest model allows developers to write
systematic, and highly scalable parallel code in a natural idiom. It doesn’t need
to work directly with threads.
The base class library encapsulates a large number of common functions which
can be used by the developers. They are the foundational types and act as the
base for all the .NET class libraries. App-specific libraries will be built on top of
BCL.
Microsoft .Net Assembly is a logical unit of code, that contains code which
the Common Language Runtime (CLR) executes.
It is the smallest unit of deployment of a .net application and it can be a .dll
or an exe .
Assembly is really a collection of types and resource information that are
built to work together and form a logical unit of functionality.
It include both executable application files that you can run directly from
Windows without the need for any other programs (.exe files), and libraries
(.dll files) for use by other applications.
WindowsForms
Windows Forms is a Graphical User Interface(GUI) class library which is
bundled in .Net Framework.
Its main purpose is to provide an easier interface to develop the applications for
desktop, tablet, PCs. It is also termed as the WinForms.
The applications which are developed by using Windows Forms or WinForms
are known as the Windows Forms Applications that runs on the desktop
computer.
WinForms can be used only to develop the Windows Forms Applications not
web applications.
WinForms applications can contain the different type of controls like labels, list
boxes, tooltip etc.
ASP.NET
It is a web framework designed and developed by Microsoft. It is used to
develop websites, web applications and web services.
It provides fantastic integration of HTML, CSS and JavaScript. It was first
released in January 2002.
It is built on the Common Language Runtime (CLR) and allows programmers to
write code using any supported .NET language.
ASP.NET AJAX
Asynchronous JavaScript and XML (AJAX) is a development technique used to
create interactive web applications or rich internet applications. AJAX uses a
number of existing technologies together, including: XHTML, CSS, JavaScript,
Document Object Model, XML, XSLT, and the XMLHttpRequest object.
With AJAX, web applications can retrieve data from the server asynchronously,
in the background, without reloading the entire browser page. The use of AJAX
has led to an increase in interactive animation on web pages.
AJAX is about updating parts of a web page, without reloading the whole page.
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small
amounts of data with the server behind the scenes. This means that it is possible
to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the
content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and
Facebook tabs.
AJAX applications are browser- and platform-independent!
Advantages
Reduces the traffic travels between the client and the server.
No cross browser pain.
Better interactivity and responsiveness.
With AJAX, several multi purpose applications and features can be handled
using a single web page(SPA).
API's are good because those work with HTTP method and JavaScript.
Disadvantages
Search engines like Google would not be able to index an AJAX application.
It is totally built-in JavaScript code. If any user disables JS in the browser, it
won't work.
The server information cannot be accessed within AJAX.
Security is less in AJAX applications as all the files are downloaded at client
side.
The data of all requests is URL-encoded, which increases the size of the
request.
ADO.NET:
ADO.NET is a module of .Net Framework which is used to establish connection
between application and data sources.
Data sources can be such as SQL Server and XML. ADO.NET consists of
classes that can be used to connect, retrieve, insert and delete data.
ADO.NET provides a bridge between the front-end controls and the back-end
database.
The ADO.NET objects encapsulate all the data access operations and the
controls interact with these objects to display data, thus hiding the details of
movement of data.
ADO.NET is a set of classes (a framework) to interact with data sources such as
databases and XML files. ADO is the acronym for ActiveX Data Objects. It
allows us to connect to underlying data or databases. It has classes and methods
to retrieve and manipulate data.
The following are a few of the .NET applications that use ADO.NET to connect
to a database, execute commands and retrieve data from the database.
ASP.NET Web Applications
Console Applications
Windows Applications.
Windows CardSpace
Windows CardSpace was developed by Microsoft as a software program for the
Identity Metasystem. CardSpace was used to create digital identities for users,
storing some personal information about them that could be requested and
accessed by websites or other software applications. Users also had the ability
to create personal cards as well, utilizing up to 14 fields to store information.
When users' information, or identity, was requested, the CardSpace UI would
appear, looking somewhat like a business card or Outlook Contact with
information about the user. The user can select what card to make available to a
website or application, which results in the website or application requesting a
digitally signed XML token. This token would then contain the information
being requested about the user.
Microsoft originally included Windows CardSpace with the .NET Framework
3.0, which was designed to run on Windows XP, Windows Vista and Windows
Server 2003. It is installed by default on Windows Vista and Windows 7, and
can be downloaded for Windows XP and Windows Server 2003 for free.
Microsoft has since discontinued Windows CardSpace, opting to put time and
resources into developing a replacement call U-Prove.
LINQ:
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET
to retrieve data from different sources and formats. It is integrated in C# or VB,
thereby eliminating the mismatch between programming languages and
databases, as well as providing a single querying interface for different types of
data sources.
For example, SQL is a Structured Query Language used to save and retrieve
data from a database. In the same way, LINQ is a structured query syntax built
in C# and VB.NET to retrieve data from different types of data sources such as
collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server
and other databases.
LINQ queries return results as objects. It enables you to uses object-oriented
approach on the result set and not to worry about transforming different formats
of results into objects.
Introduction to C#
C# (C-Sharp) is a programming language developed by Microsoft that runs on
the .NET Framework.
C# is used to develop web apps, desktop apps, mobile apps, games and much
more.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
using System;
The using keyword is used for including the namespaces in the program. A
program can include multiple using statements
Comments:
Single-line comments are indicated by the '//' symbol. For example,
//This is a comment
The multiline comments in C# programs start with /* and terminates with the
characters */
/* This is a
Multiline comment */
C# Keywords:
Keywords are predefined sets of reserved words that have special meaning in a
program. The meaning of keywords cannot be changed, neither can they be
directly used as identifiers in a program.
C# has a total of 79 keywords. All these keywords are in lowercase.
Here is a complete list of all C# keywords.
Contextual Keywords
Besides regular keywords, C# has 25 contextual keywords. Contextual
keywords have specific meaning in a limited program context and can be used
as identifiers outside that context. They are not reserved words in C#.
C# Identifiers:
Identifiers are the name given to entities such as variables, methods, classes, etc.
They are tokens in a program which uniquely identify an element. For example,
Rules for Naming an Identifier
An identifier can not be a C# keyword.
An identifier must begin with a letter, an underscore or @ symbol. The
remaining part of identifier can contain letters, digits and underscore symbol.
Whitespaces are not allowed. Neither it can have symbols other than letter,
digits and underscore.
Identifiers are case-sensitive. So, vcetMCA, vcetmca and Vcetmac
represents 3 different identifiers.
Note:
PascalCase for public member variables (string MyName = "James")
camelCase for local variables (string myName = "James")
_leadingUnderscore for private member variables (string _myName = "James")
Primitive Data types in C#
int x, y=20;
x=10;
x=y;
Constants are similar to variables but once a value is assigned it can’t be changed
during the execution of the program. Constants differ in one significant way in
that once a value has been assigned to a constant it cannot subsequently be
changed.
const int x=10; //Valid
const int x;
x=10 //Invalid
In C#, these data types are categorized based on how they store their value in the
memory. C# includes the following categories of data types:
Value type
Reference type
Pointer type
Value Type:
A data type is a value type if it holds a data value within its own memory space.
It means the variables of these data types directly contain values.
For example, consider integer variable int i = 100;
The system stores 100 in the memory space allocated for the variable i.
Reference Type
Unlike value types, a reference type doesn't store its value directly. Instead, it
stores the address where the value is being stored. In other words, a reference
type contains a pointer to another memory location that holds the data.
For example, consider the following string variable:
string s = "Hello World!!";
As you can see in the above image, the system selects a random location in
memory (0x803200) for the variable s. The value of a variable s is 0x600000,
which is the memory address of the actual data value. Thus, reference type
stores the address of the location where the actual value is stored instead of the
value itself.
Reference type data types include:
String
Arrays (even if their elements are value types)
Class
Delegate
3. Pointer Type:
Pointer type or pointer variables refers to those variables that holds the address
of another variables.
int x=100;
int *ptr = & x;.
Here ptr holds the address of x.
*ptr is used to represent the value stored in the address held by ptr variable.
&- address of x
&x represents assignment of address of x to pointer variable ptr.
Explicit type conversion - These conversions are done explicitly by users using
the pre-defined functions. Explicit conversions require a cast operator.
Generally, larger types like double (having large memory size) are converted to
smaller types like int (having small memory size).
There is a possible chance of data loss in this type of conversion.
// Explicit casting
double y=100.123
int x = (int) y;
Now the double type is converted to int type explicitly.
Here, (int) is a cast expression that explicitly converts the double type to int
type.
value 100 of type int stored in variable num is converted to string data type and
stored in string variable str.
Example 2: Convert.ToDouble(num) - converts num to the Double type.
The casting of a boxed value is not permitted. The following will throw an
exception.
First perform unboxing and then casting.
Namespaces in C#:
Namespaces play an important role in managing related classes in C#.
For example, there are some built-in namespaces in .NET such as System,
System.Linq, System.Web, etc. Each namespace contains related classes.
Example:
namespace VCET
{
class UG
{
//Member data and member functions
}
class PG
{
//Member data and member functions
}
System namespace:
Contains fundamental classes and base classes that define commonly-used value
and reference data types, events and event handlers, interfaces, attributes, and
processing exceptions.
The using System; line means that you are using the System library in your
project. Which gives you some useful classes like Console or functions/methods
like WriteLine.
C# arrays:
Array in C# is a group of similar types of elements that have contiguous
memory location. In C#.
Array is an object of base type System.Array.
In C#, array index starts from 0.
We can store only fixed set of elements in C# array.
Advantages of C# Array
Code Optimization (less code)
Random Access
Easy to traverse data
Easy to manipulate data
Easy to sort data etc.
Disadvantages of C# Array
Fixed size
C# Array Types
[] has to be placed after the keyword(int) and not after the identifier(arr).
Which if misplaced throws a compile time error.
Multidimensional array:
To create a 2D array, add each array within its own set of curly braces, and
insert a comma (,) inside the square brackets:
int[,] numbers = { {1, 4, 2}, {3, 6, 8}};
Note:
The single comma [,] specifies that the array is two-dimensional. A three-
dimensional array would have two commas: int[,,].
numbers is now an array with two arrays as its elements. The first array element
contains three elements: 1, 4 and 2, while the second array element contains 3, 6
and 8. To visualize it, think of the array as a table with rows and columns:
Let's see an example to initialize jagged array. The size of elements can be
different.
arr[0] = new int[4];
Since we know each element of a jagged array is also an array, we can set the
size of the individual array.
For example,
// set size of the first array as 3
jaggedArray[0] = new int[3];
Once we declare a jagged array, we can use the index number to initialize it. For
example,
// initialize the first array
jaggedArray[0][0] = 1;
jaggedArray[0][1] = 3;
jaggedArray[0][2] = 5;
index at the first square bracket represents the index of the jagged array element
index at the second square bracket represents the index of the element inside
each element of the jagged array.
int[ ][ ] jaggedArray = {
new int[ ] {10, 20, 30},
new int[ ] {11, 22},
new int[ ] {88, 99}
};
Accessing elements of a jagged array
We can access the elements of the jagged array using the index number. For
example,