Name: Deepak Gupta Organization: Chitkara University: WWW - Chitkara.edu - in
Name: Deepak Gupta Organization: Chitkara University: WWW - Chitkara.edu - in
NET
www.chitkara.edu.in
Introduction
• .NET provides an environment to develop any
application which can run on Windows.
S-2 www.chitkara.edu.in
Significance of .NET and C#
• Windows Technologies – a process of evolving and
extending the API rather than replacing it. E.g.,
• Windows 3.1
• Windows 95
• Windows XP
• Windows 2003
• Windows Vista
• Windows 7
• COM/DCOM/COM+
S-3 www.chitkara.edu.in
Cont…
• Backward compatibility is the success mantra for
evolutionary approach.
• Advantage:
• Disadvantage:
• Complexity
• Better solution:
S-4 www.chitkara.edu.in
Cont…
• .NET framework is though a fresh start, but keeps
backward compatibility.
S-5 www.chitkara.edu.in
Advantages of .NET
• OO programming.
• Good design.
• Language independence:
All languages in .NET compile to common intermediate
language (IL). Thus, languages are interoperable.
S-6 www.chitkara.edu.in
Cont…
• Efficient data access using ADO.NET
Note: XML allows data manipulation, which can be imported from
and exported to non-Windows platforms.
• Code sharing
• Assemblies are used to share code between applications,
replacing DLLs. Different versions of assemblies can exist side
by side.
• Improved security:
• Assemblies have built in security information which describes
which user (s) or process (s) can call which method on which
class.
S-7 www.chitkara.edu.in
Cont…
• Zero impact installation
• Shared assemblies
• VS 2010
S-8 www.chitkara.edu.in
Microsoft Intermediate Language (IL)
• .NET compilation process has 2 steps:
• Compilation of source code to Microsoft Intermediate Language
(IL).
S -9 www.chitkara.edu.in
Benefits of IL
• Platform Independence
• The byte code can be put on any platform at runtime (before
CLR does the final compilation).
• Performance Improvement
• JIT (Just-In-Time) compiler compiles only the portion of code
which is called (NOT the complete code).
S -10
(Expected !) www.chitkara.edu.in
Cont…
• Language Interoperability
• Compile source code written in one language supported by .NET
, say C#, to IL
S -11 www.chitkara.edu.in
First Program
S -12 www.chitkara.edu.in
Introduction to C#
• A language of it’s own, generating code which
targets .NET framework.
• Case sensitive
• Comments:
• //
• /* */
S -13 www.chitkara.edu.in
Cont…
• Using statement is used to locate a namespace
which compiler looks for any class referenced in the
code, but not defined current namespace.
S -14 www.chitkara.edu.in
Compile and Run a Program
• Make sure you use command line of VS2010
(automatically sets environmental variables)
• Compile a program
• csc First.cs
S -15 www.chitkara.edu.in
Variables
• datatype identifier
• int x; //declaration
• x = 1; //initialization
• Note:
• Prior to variable usage, its initialization is mandatory. This avoids
retrieval of junk values from memory. (Smartness of C# compiler)
S -16 www.chitkara.edu.in
Variables Initialization
• C# ensures variables initialization using following
approach:
• class / struct variables are initialized to 0 (by default) as these
cannot be initialized explicitly.
int d;
return 0;
S -17 www.chitkara.edu.in
Cont…
• MyClass objMyClass
• This creates only a reference for a MyClass object, but you
cannot refer to any method inside object.
S -18 www.chitkara.edu.in
Type Inference
• Type inference makes use of the var keyword.
S -19 www.chitkara.edu.in
Cont…
• using System;
• namespace Wrox {
• class Program {
S -21 www.chitkara.edu.in
Variable Scope
• Rules for finding scope:
• Class / struct scope.
• Same variable name can be used in different parts as long as the scope
is different.
S -22 www.chitkara.edu.in
Cont…
• public static int Main() {
for (int i = 0; i < 10; i++)
Console.WriteLine(i);
Console.WriteLine(i);
return 0;
• }
S -23 www.chitkara.edu.in
Cont…
• What should be output?
public static int Main()
int j = 20;
{
// IMP: Can't do this as j is in loop’s scope which is nested into Main() method
// scope.
int j = 30;
Console.WriteLine(j + i);
}
S -24 www.chitkara.edu.in
return 0;
Cont…(Scope clashes for Fields (member) and local
variables
• C# is INTELLIGENT enough to make a distinction between field
and local variables.
using System;
namespace Wrox{
class Scope{
return;
}}}
• NOTE: Use this keyword to access an instance field (a field related to a particular
instance of object.)
S -25 www.chitkara.edu.in
Constants
• const int x = 10;
• Constants are:
• Initialized when they are declared.
• Cannot be overwritten.
S -26 www.chitkara.edu.in
Cont…
• Advantages:
• Enhance readability:
Programs becomes easier to read by replacing magic numbers and
strings with readable names.
• Prevent mistakes:
You cannot modify a constant variable by mistake.
S -27 www.chitkara.edu.in
Value and Reference Data Types
• Value type stores data directly (uses Stack).
int i, j;
i = 10;
j = i;
S -28 www.chitkara.edu.in
Cont…
• Consider a class Vector.
x.Value = 30;
y = x;
Console.WriteLine (y.Value);
y.Value = 50;
Console.WriteLine(x.Value);
S -29 www.chitkara.edu.in
Cont…
• Key notes:
• An object is created using new.
• Output:
30
50
S - 30 www.chitkara.edu.in
Cont…
• If x = null;
means reference variable y does not refer to an object and
thus it’s not possible to call nonstatic functions or fields.
S - 31 www.chitkara.edu.in
Predefined Value Types
• The basic data types in C# are not intrinsic to the
language, but are part of the .NET Framework.
string s = i.ToString();
S - 33 www.chitkara.edu.in
Cont…
• 8 predefined integers.
S - 34 www.chitkara.edu.in
Floating Point Types
• double provides twice the precision than float.
• For any non integer number, normally compiler
treat it as a double.
S - 35 www.chitkara.edu.in
The Decimal Types
• Higher precision floating – point numbers.
• Useful in financial calculations to have greater
accuracy.
• Slow down the performance.
S - 37 www.chitkara.edu.in
The Boolean Types
• Boolean values are either true or false.
• No implicit conversion of a bool into integer or vice
versa.
S - 38 www.chitkara.edu.in
The Character Types
• 16-bit characters.
• Enclosed in single quotation mark.
S - 39 www.chitkara.edu.in
Predefined Reference Types
• object type is the ultimate parent type from which all
other intrinsic and user-defined types are derived.
S - 40 www.chitkara.edu.in
Cont…
• string belongs to reference type.
• One of exception is that the strings are immutable , i.e.,
if you make a change to one string, it’ll create a new
string object and leave another unchanged.
using System;
class StringExample {
public static int Main() {
string s1 = “First String";
string s2 = s1;
Console.WriteLine("s1 is " + s1);
Console.WriteLine("s2 is " + s2);
s1 = “Second String"; // Creates new string object
Console.WriteLine("s1 is now " + s1);
Console.WriteLine("s2 is now " + s2);
return 0;
}}
S - 41 www.chitkara.edu.in
Cont…
• Output:
First String
First String
Second Sting
First String
S - 42 www.chitkara.edu.in
Flow Control
Conditional Statements
• if
• Switch
S - 43 www.chitkara.edu.in
The switch Statement
• C#’s switch prohibits fall-through conditions to avoid a lot
of logical errors. The compiler enforces this restriction by
making it mandatory to have break statement in every case
clause.
S - 45 www.chitkara.edu.in
Loops
• for
• while
• do-while
• foreach.
• The foreach loop allows you to iterate through each item in a collection,
in one by one mode.
S - 46 www.chitkara.edu.in
Cont…
• The value of an item cannot be changed in the collection.
S - 47 www.chitkara.edu.in
Jump Statements
• The goto statement
goto Label1;
Console.WriteLine("This won't be executed");
Label1:
Console.WriteLine("Continuing execution from here");
• Restrictions:
• Can’t jump into a block of code such as a for loop.
• Can’t jump out of a class.
S - 48 www.chitkara.edu.in
Cont…
• The break statement.
• The continue statement.
• The return statement.
S - 49 www.chitkara.edu.in
Enumerations
S - 51 www.chitkara.edu.in
Namespace
• A namespace is a logical rather than physical grouping.
• Defining the namespace hierarchy should be planned out
prior to the start of a project.
• namespace Wrox
• {
• namespace ProCSharp
• {
• namespace Basics
• {
• class NamespaceExample
• {
• // Code for the class here...
• }
• }}}
S - 52 www.chitkara.edu.in
Cont…
This can be also written like:
namespace Wrox.ProCSharp.Basics
{
class NamespaceExample
{
// Code for the class here...
}
}
S - 53 www.chitkara.edu.in
Using Directive
using Wrox.ProCSharp.OOP;
using Wrox.ProCSharp.Basics;
S - 54 www.chitkara.edu.in
Main () method
• Only one entry point should be there.
• In case, there are two Main () methods. This will give an
error as compiler fails to find the entry point.
o using System;
o namespace Wrox {
o class Client {
o public static int Main() {
o MathExample.Main();
o return 0;
o }}
o class MathExample {
o static int Add(int x, int y) {
o return x + y;
o }
o public static int Main() {
o int i = Add(5,10);
o Console.WriteLine(i);
o return 0;
o }}}
S - 55 www.chitkara.edu.in
Cont…
Better solution:
Specify the entry point using /main switch like
S - 56 www.chitkara.edu.in
Passing arguments to Main()
• The parameter is a string array, traditionally called args
S - 57 www.chitkara.edu.in
Cont…
How do you run the program using command line?
ArgsExample A B C
Output
A
B
C
S - 58 www.chitkara.edu.in
C# Programming Guidelines
• Rules for Identifiers.
o They must begin with a letter or underscore, although they can
contain numeric characters.
• Naming Conventions.
S - 59 www.chitkara.edu.in