I-Unit C#
I-Unit C#
Notes of Unit-I
C# and Dot Net Framework
UNIT-I
Introduction to C#
‘C#’ (pronounced as ‘C Sharp’) is an elegant and type-safe object-oriented
language that enables developers to build a variety of secure and robust
applications that run on the .Net Framework.
It is a case-sensitive language.
Characteristics of C#
1. Simple
C# simplifies c++ by eliminating irksome operators such as ->, :: and
pointers. C# treats integer and Boolean data types as two entirely different
types.
2. Consistent (Compatible)
C# supports a unified type system that eliminates the problem of varying
ranges of integer types. All types are treated as objects and developers can
extend the type system simply and easily.
3. Object-Oriented
C# is truly object-oriented it supports the main 4 pillars of object-oriented
as follows
Encapsulation
Inheritance
Polymorphism
Abstraction
4. Type-safe
Fi
g: .Net Architecture
.Net Applications:
i. Windows Forms
WinForms can be used only to develop windows Forms Applications not web
applications. WinForms applications can contain a different types of controls
like labels, list boxes, tooltips etc.
ASP.Net: This is used for developing web-based applications, which are made
to run on any browser such as Internet Explorer, Chrome or Firefox. (Active
Server Page)
These services use the XML to exchange the information with the other
software with the help of the common internet Protocols. In the simple term,
we use the Web Service to interact with the objects over the internet.
An XML library is a collection of methods and functions that can be used for the
core purpose.
The .Net Framework provides a huge Framework (or Base) Class Library (FCL)
for common, usual tasks.
It is simply the largest standard library ever shipped with any development
environment or programming language.
The best part of this library is they follow extremely efficient OO design
(design patterns) making their access and use very simple and predictable.
You can use the classes in FCL in your program just as you would use any other
class. You can even apply inheritance and polymorphism to these classes.
Common Language Runtime (CLR)
Prof. Satish Malayi 5|Page
C# and Dot Net Framework
Our .Net
Applications
Windows
OS
The most important concept of the .Net Framework is the existence and
functionality of the .Net Common Language Runtime (CLR), also called .Net Runtime for
short. It is a framework layer that resides above the OS and handles the execution of all
the .Net applications. Our programs don't directly communicate with the OS but go
through the CLR.
Windows OS
This is the last layer of our .Net architecture here the last
executable code (Native code) is stored from all the above
processes.
The .Net language which conforms to the Common Language Specification (CLS)
uses its corresponding runtime to run the application on different Operating
Systems.
During the code execution time, the managed code is compiled only when it is
needed that is it converts the appropriate instructions to the native code for
execution just before when each function this process is called Just-In-Time
(JIT) the common language runtime (CLR) doing this task.
The common language runtime (CLR) provides various Just-In-Time (JIT) and
each work on a different architecture depending on Operating System.
The same Microsoft intermediate language (MSIL) can be executed on different
operating systems without rewriting the source code.
Just-In-Time compiler (JIT) code generally offers far better performance than
interpreters.
Prof. Satish Malayi 7|Page
C# and Dot Net Framework
The Common Language Specification (CLS) is one of the specifications that describes
that the compiled code of every .Net language should be the same (i.e. Common
Intermediate Language (CIL) / MSIL code).
Common Language Specification (CLS) defines a subset of the Common Type System (CTS).
Common Type System (CTS) is nothing but a common data type platform where you
write your code either in C# or in VB or in any dot net supported programming
languages, all the data types of these languages get into a common data type platform
which is known as Common Type System.
At the time of compilation, all language-specific data types are converted into CLR’s data
type. This data type system of CLR which is common to all programming languages
of .NET is known as CTS.
• Value Type
All fixed-length data types int, float, char, etc. will come under the category of a value
type.
• Reference Type
All variable length data types like string and object will come under the category of
reference types.
Framework work is a software which will masks (acting as) the functionality of
operating system making the Common Intermediate Language Code to execute
under its control.
WF Workflow Forms
C# And C++
SL.NO C# C++
C+ is a low-level programing
1. C# is a high-level language.
language
It does not support multiple
2. It support the multiple inheritance
inheritance
C# automatically manages In C++ you require to manage
3.
memory memory Manually
4. In C# after compiling code is In C++ after compiling code is
changed in to an MSIL. changed into machine code (.exe)
C# programming used for
C++ used for developing console
5. windows, mobile and console
applications.
applications
For each loop is not supported in C+
6. For each loop support in C#
+
C++ does not support garbage
7. C# support garbage collection
collection
C# is quite easy because it
C++ includes very complex
8. has the well-defined
features.
hierarchy of classes.
C++ is not a pure object-oriented
C# is pure object-oriented
9. programming language due to the
programming language.
primitive data types.
In C# only in unsafe mode In C++ you can use pointer
10.
you can use a pointer anywhere in the Program
C# And JAVA
Prof. Satish Malayi 10 | P a g e
C# and Dot Net Framework
SL.NO C# JAVA
C# is an object-oriented Java is a high level, robust, secured
programming language and object-oriented
1.
developed by Microsoft that runs programming language developed by
on .Net Framework. Oracle.
Its derives from C family
2. Its derives from C family language.
language.
C# compiler generates Microsoft
3. Java compiler generates Java byte code.
Intermediate Language (MSIL).
4. C# supports goto statement. Java doesn't support goto statement.
C# supports structures and Java doesn’t support structures and
5.
unions. unions.
6. C# supports garbage collection Java supports garbage collection
To import libraries/file uses To import libraries / files using packages
7.
using keyword
C# supports operator overloading of Java does not support operator
8. overloading
multiple operators
Java supports checked exception and
9. C# supports unchecked exception.
unchecked exception.
10. C# type safety is unsafe. Java type safety is safe.
Each statement is terminated with a Each statement does not end with a
7.
semicolon (;) semicolon.
Namespace in C#
A Namespace is simply a logical collection of related classes in C#. We
bundle our related classes in some named collection calling it a
namespace.
Namespaces are used to organize the classes. It helps to control the scope
of methods and classes in larger .Net programming projects.
It is also referred as named group of classes having common features.
The members of a namespace can
be namespaces, interfaces, structures, and delegates.
Defining a Namespace
Syntax:
namespace name_of_namespace
{
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}
Example: using System;
using System.linq; etc.
using System;
The using keyword above allows us to use the classes in the following
'System' namespace. By doing this, we can now access all the classes defined in
the System namespace like we are able to access the Console class in our Main method
later. One point to remember here is using allows you to access the classes in the
referenced namespace only and not in its internal/child namespaces. Hence we might
using System.Collections;
need to write
Console.WriteLine(“HelloWorld”);
Here we called WriteLine(), a static method of the Console class defined in the
System namespace.
This method takes a string (enclosed in double quotes) as its parameter and
prints it on the Console window.
Comments
Comments are the programmer's text to explain the code, are ignored by the
compiler and are not included in the final executable code.
C# uses syntax for comments that is similar to Java and C++. The text following
double slash marks (// any comment) are line comments. The comment ends
with the end of the line:
C# also supports the comment block. In this case, the whole block is ignored
by the compiler. The start of the block is declared by slash-asterisk (/*) and
ends with asterisk-slash mark (*/):
static void Main()
{
/* These lines of text
will be ignored by the compiler */
-------
}
Write()
This method is used to display any message to the user in the output stream.
After displaying the message blinking cursor remains in the same line.
WriteLine()
This method is used to display required message to the user on the output
stream.
After displaying the message blinking cursor moves to a new line.
Read()
This will read an input stream from the console window and
return the input string when user presses the enter key.
And it convert any type of value to string type we mostly use
ReadLine() instead of Read().
Clear()
This method is used to delete the contents of screen and is same
as clrscr() in C / C++.
Example:
using System;
namespace ConsoleClassExample
{
class Consoleclass
{
static void Main(string[] args)
{
Console.Write("BCA ");
Console.WriteLine("KLE BCA");
Console.WriteLine("Athani");
Console.ReadLine();
}
}
}
Output: BCA KLE BCA
Athani
using System;
using System.Collections;
Prof. Satish Malayi 19 | P a g e
C# and Dot Net Framework
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleEnvironmentClass
{
class Program
{
static void Main(string[] args)
{
ArrayList ar = new ArrayList();
ar.Add("OSVersion: " + Environment.OSVersion.ToString());
ar.Add("OSVersion Platform: " + Environment.OSVersion.Platform.ToString());
ar.Add("NewLine :" + Environment.NewLine.ToString());
ar.Add("MachineName :" + Environment.MachineName.ToString());
ar.Add("UserDomainName: " + Environment.UserDomainName.ToString());
ar.Add("UserName : " + Environment.UserName.ToString());
Console.WriteLine(Environment.NewLine + "Environment Class Details" +
Environment.NewLine + "-----------------------");
foreach (var item in ar)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output:
**********