2. Prerequisites
This module assumes that you understand the
fundamentals of
Programming
Variables, statements, functions, loops, etc.
Object-oriented programming
Classes, inheritance, polymorphism,
members, etc.
C++ or Java
2
3. 3
Lecture Plan
Introduction to thecourse
Configure the Microsoft Visual StudioIDE
Create a simple hello world style application
MessageBox
4. Introduction to the course
1
Course Objectives
Todevelopment of Interactive
applications
Toenable student to write event
drivenprograms
Todevelopment of GUIapplications
Grading
Quizzes/Assignments = 30%
Mid-termexams = 20%
Final-termexams = 50%
6. What is .NET
Framework
6
✗ Net Framework is a software development platform
developed by Microsoft for building and running
Windows applications.
✗ The . Net framework consists of developer tools,
programming languages, and libraries to build
desktop and web applications. It is also used to build
websites, web services, and games.
7. What is .NET Framework
7
There are various implementations of .NET.
1. NET Framework is the original implementation of .NET. It supports
running websites, services, desktop apps, and more on Windows.
2. NET is a cross-platform implementation for running websites,
services, and console apps on Windows, Linux, and macOS. .NET is
open source on GitHub. .NET was previously called .NET Core.
3. Xamarin/Mono is a .NET implementation for running apps on all the
major mobile operating systems, including iOS and Android
11. Architecture of .NET Framework
11
The two major components of .NET Framework are the Common Language
Runtime and the .NET Framework Class Library.
The Common Language Runtime (CLR) is the execution engine that
handles running applications. It provides services like thread management,
garbage collection, type-safety, exception handling, and more.
The Class Library provides a set of APIs and types for common
functionality. It provides types for strings, dates, numbers, etc. The Class
Library includes APIs for reading and writing files, connecting to databases,
drawing, and more.
12. Architecture of .NET Framework
12
✗ .NET applications are written in the C#, F#, or Visual Basic programming
language. Code is compiled into a language-agnostic Common
Intermediate Language (CIL). Compiled code is stored in assemblies—files
with a .dll or .exe file extension.
✗ When an app runs, the CLR takes the assembly and uses a just-in-time
compiler (JIT) to turn it into machine code that can execute on the specific
architecture of the computer it is running on.
13. .NET Components : Common Language Runtime
13
Common Language Runtime
The “Common Language Infrastructure” or CLI is a platform in
dot-Net architecture on which the dot-Net programs are executed.
The CLI has the following key features:
Exception Handling – Exceptions are errors which occur when the
application is executed.
Examples of exceptions are:
• If an application tries to open a file on the local machine, but
the file is not present.
• If the application tries to fetch some records from a database,
but the connection to the database is not valid.
14. .NET Components : Common Language Runtime
14
Garbage Collection – Garbage collection is the process of removing
unwanted resources when they are no longer required.
Examples of garbage collection are
• 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.
• The database connection is no longer required. If the application
has finished all operations on a database, then the database
connection may no longer be required.
15. .NET Components : Common Language Runtime
15
A developer can develop an application in a variety of .Net programming
languages.
1. Language – The first level is the programming language itself, the most
common ones are VB.Net and C#.
2. Compiler – There is a compiler which will be separate for each programming
language. So underlying the VB.Net language, there will be a separate VB.Net
compiler. Similarly, for C#, you will have another compiler.
3. Common Language Interpreter – This is the final layer in .Net which would
be used to run a .net program developed in any programming language. So the
subsequent compiler will send the program to the CLI layer to run the .Net
application.
16. Architecture of .NET Framework
16
The two major components of .NET Framework are the Common Language
Runtime and the .NET Framework Class Library.
The Common Language Runtime (CLR) is the execution engine that
handles running applications. It provides services like thread management,
garbage collection, type-safety, exception handling, and more.
The Class Library provides a set of APIs and types for common
functionality. It provides types for strings, dates, numbers, etc. The Class
Library includes APIs for reading and writing files, connecting to databases,
drawing, and more.
17. Architecture of .NET Framework
17
✗ .NET applications are written in the C#, F#, or Visual Basic programming
language. Code is compiled into a language-agnostic Common
Intermediate Language (CIL). Compiled code is stored in assemblies—files
with a .dll or .exe file extension.
✗ When an app runs, the CLR takes the assembly and uses a just-in-time
compiler (JIT) to turn it into machine code that can execute on the specific
architecture of the computer it is running on.
18. Configure the IDE
18
After you open Visual Studio, you can identify the tool windows,
the menus and toolbars, and the main window space.
Tool windows are docked on the left and right sides of the
application window, with Quick Launch, the menu bar, and the
standard toolbar at the top. In the center of the application
window is the Start Page.
When you load a solution or project, editors and designers appear
in the space where the Start Page is. When you develop an
application, you’ll spend most of your time in this central area.
25. Why you should learn C#
C# and the Visual Studio
IDE make it easy for you to
get to the business of
writing code, and writing it
fast. When you’re working
with C#, the IDE is your
best friend and constant
companion.
25
27. What you get with Visual Studio and C#…
27
With a language like C#, tuned for Windows
programming, and the Visual Studio IDE, you can
focus on what your program is supposed to do
immediately:
29. Structure of C-sharp Program-SYNTAX
29
namespace example
{
<class keyword><class Name>
{
<statement 1>;
<statement 2>; //and so on
}
}
30. Structure of C-sharp Program
30
using System;
class Hello {
static void Main( ) {
Console.WriteLine("Hello world");
Console.ReadLine(); // Hit enter to finish
}
}
31. Things To Note:
31
C# is case sensitive.
All statements and expression must end with a semicolon (;).
The program execution starts at the Main method.
Unlike Java, program file name could be different from the class
name.
32. Structure of C-sharp Program
32
The Hello World application is very simple in C#. Some key points:
In C# there are no global methods. Everything belongs to a class.
A method named Main is the entry point for a C# application. Note that Main is spelled with a
capital “M”, which is different than C and C++. The reason is that for consistency, all method
names start with a capital letter in the .NET Framework
The line using System; means that we’ll be accessing members of the System namespace.
In a very rough comparison, a namespace could be translated to a Unit in Turbo Pascal/Delphi
or a .LIB file in C/C++. So in the Hello World example, the class Console, which contains the
method WriteLine belongs to the System namespace. We could avoid the “using” statement
by writing the complete path of the method:
System.Console.WriteLine(“Hello World”);
33. Simple Console Application-Example
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{{
class Program
{
{
static void Main(string[] args)
{ {
int a, b;
System.Console.WriteLine("Hello World");
Console.ReadLine();
Console.Write("enter any two numbers");
Console.ReadLine();
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
int sum = a + b;
Console.Write(sum);}}}
}
}
}
38. Arrays in General
When declaring an array, the square brackets ([]) must
come after the type, not the identifier. Placing the
brackets after the identifier is not legal syntax in C#.
int[] table; // not int table[];
38
39. Arrays in General
The size of the array is not part of its type as it is in the C language.
This allows you to declare an array and assign any array of int objects
to it, regardless of the array's length.
int[] numbers; // declare numbers as an int array of any size
numbers = new int [10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
39
41. CreatingArrays
Declaring them (as shown above) does not actually
create the arrays. You need to use the new keyword to
create an instance of the array
int[ ] numbers = newint[5];
double[ ] balance = newdouble[10];
41
42. Assigning Values to anArray
Note: If you do not initialize an array at the time of declaration, the
array members are automatically initialized to the default initial
value for the array type
You can assign values to individual array elements, by using the
index number, like:
double[] balance = new double[10];
balance[0] = 4500.0
42
43. Assigning Values to anArray
C# provides simple and straightforward ways to initialize arrays at declaration
time by enclosing the initial values in curly braces({}).
int[ ] numbers = new int[5] {1, 2, 3, 4,5};
string[ ] names = new string[ ] {"Matt", "Joanne","Robert"};
double[ ] balance = { 2340.0, 4523.69,3421.0};
43
44. Accessing ArrayMembers
static void Main(string[] args)
{
int[] n = new int[10]; int i, j;
//initialize values
for (i = 0; i < 10; i++)
{
n[i] = i + 100;
}
//access elements
for (j = 0; j < 10; j++)
{
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
//Console.ReadKey();
}
}
44
45. WRITE A PROGRAM IN C# TO GET
VALUES FROM USER TO STORE IT
IN AN ARRAY AND THEN DISPLAY
IT AS OUTPUT.
45
Q#
46.
47. foreach loop
The foreach loop is used to iterate over the
elements of the collection. The collection may be
an array or a list. It executes for each element
present in the array.
48. What is the
difference between
for loop and
foreach loop?
48
for loop executes a statement or
a block of statement until the given
condition is false. Whereas
foreach loop executes a
statement or a block of statements
for each element present in the
array and there is no need to define
the minimum or maximum limit.
we can not obtain array index using
ForEach loop
49. Using foreach LOOP onArrays
int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
foreach (int i in numbers)
{
System.Console.WriteLine(i);
}
50. Using foreach onArrays
static void Main(string[] args)
{
int[] n = new int[10];
//initialize values
for (int i = 0; i < 10; i++)
{
n[i] = i + 100;
}
//access elements
foreach ( int j in n )
{
int i = j - 100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
}}}
50