I-UNIT C#
I-UNIT C#
Navanagar Bagalkot
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.
2|Page
C# and Dot Net Framework
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
3|Page
C# and Dot Net Framework
❖ .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.
5|Page
C# and Dot Net Framework
• You can use the classes in FCL in yourprogram just as you would use any other
class. You can even apply inheritance and polymorphism to these classes.
•
❖ Common Language Runtime (CLR)
• CLR is an execution engine of .Net framework in which every application of .Net will
runs under the control of Common Language Runtime (CLR).
• The main function of Common Language Runtime (CLR) is to convert the MSIL code
into Native Code and then execute the program.
• The MSIL code compiled only when it needed that is it converts the appropriate
instructions when each function is called.
• The Common Language Runtime (CLR)’s Just-In-Time (JIT) compilation converts
Microsoft Intermediate Language (MSIL) to native code on demand at application run
time.
• During the execution of program the Common Language Runtime (CLR) manages
memory, Thread execution, Garbage Collection (GC), Exception Handling, Common
Type System (CTS), code safety verification, and other system services.
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
6|Page
C# and Dot Net Framework
• 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 workon a different architecture depending on Operating System.
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).
8|Page
C# and Dot Net Framework
➢ 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.
9|Page
C# and Dot Net Framework
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
6. For each loop support in C#
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.
10 | P a g e
C# and Dot Net Framework
C# And JAVA
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
of 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.
11 | P a g e
C# and Dot Net Framework
Each statement is terminated with a Each statement does not end with a
7.
semicolon (;) semicolon.
12 | P a g e
C# and Dot Net Framework
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.
13 | P a g e
C# and Dot Net Framework
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 need to write
using System.Collections;
14 | P a g e
C# and Dot Net Framework
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()
{
16 | P a g e
C# and Dot Net Framework
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.
17 | P a g e
C# and Dot Net Framework
Read()
• Read method reads the single character and convert that
character into the ASCII value that means it returns the value of
type integer.
ReadLine()
• 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
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();
}
}
18 | P a g e
C# and Dot Net Framework
}
Output: BCA KLE BCA
Athani
19 | P a g e
C# and Dot Net Framework
using System;
using System.Collections;
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:
20 | P a g e
C# and Dot Net Framework
**********
21 | P a g e