Introduction to .Net and C#

Download Report

Transcript Introduction to .Net and C#

Introduction to .Net and C#
BIT-7
Agenda








Features of .Net Framework
.NET vs. J2EE
.Net Framework
C# features and design goals
C# basics
Unified type system
Enum
Arrays
Features of .Net Framework

Interoperability with other environments


Need of plateform independent applications (windows, Unix )
Microsoft Intermediate Language (MSIL)



Support for developing language independent applications





Set of CPU independent instruction
Unix compiler complies MSIL code to one that UNIX understand.
Common development environment for all languages
So code can be shared among application developed in diff. languages
Support for OOPs
Support for Web applications
Support for Web services
.NET vs. J2EE

Both are similar in many ways:






J2 Enterprise Edition


Server- and client-side model for building enterprise applications.
Virtual machine designed to inspect, load, and execute programs in a
controlled environment.
APIs for creating both fat- and thin-client models.
APIs for foundation services (data access, directory, remote object calls,
sockets, forms).
Development environment for dynamic web pages.
Language-Dependent & Platform-Independent
.NET

Language-Independent & Platform Dependent (for now)
J2EE: Language-Specific, PlatformIndependent
Linux
Person.java
Java VM
Person
bytecodes
Java VM
Deploy
Address
bytecodes
Windows
Java VM
Company
bytecodes
Solaris
Address.java
Company.java
Java VM
.NET: Language-Independent, (Mostly)
Platform- Specific
Person.vb
Windows
(Visual Basic)
CLR
Person
MSIL
CLR
Deploy
Address
MSIL
Windows
CLR
Company
MSIL
Others?
Address.cs
(C#)
Company.cbl
(Cobol)
CLR
.NET Framework
C#
VB.NET
C++.NET
Other
Common Language Specification
Framework Class Library
ASP.NET
Web Services
Windows Forms
Web Forms
ASP.NET Application Services
Controls
Drawing
Windows Application Services
ADO.NET
XML
Threading
IO
Network
Security
Diagnostics
Etc.
Common Language Runtime
Memory Management
Common Type System
Operating System
Lifecycle Monitoring
Visual
Studio
.NET
Common Language Runtime

A runtime provides services to executing programs


CLR provided by .NET manages the execution of
code and provides useful services



Standard C library, MFC,VB Runtime, JVM
Memory management, type system, etc.
Manage threads and helps in security
Managed vs. unmanaged code
.NET Framework Class Library


Framework – you can call it and it can call you
Large class library


Over 2500 classes
Major components




Base Class: Networking, security, I/O, files, etc.
Data and XML Classes
Web Services/UI
Windows UI
Common Language Specification

CLS is a set of rules that specifies features that all
languages should support


Goal: have the .NET framework support multiple languages
CLS is an agreement among language designers and class
library designers about the features and usage conventions
that can be relied upon
Some .NET Languages
•
•
•
•
•
•
•
C#
COBOL
Eiffel
Fortran
Mercury
Pascal
Python
Perl
Smalltalk
VB.NET
VC++.NET
J#.NET
….
More are planned or under
development
C#

Features








New OO programming language
C# is advanced version of C and C++ and
Specially designed for .NET environment
Strong versioning support
Unified type system / type safety
Automatic memory management
Designed to leverage the CLR
Design Goals of C#




Component-orientation
Everything is an object
Robust and durable software
Preserving your investment
C# Basics

Variables
<modifiers> <datatype> <variable1, variable2………..>

Modifiers






Internal
Private
Protected
Public
Read only
Static
C # basics : Types of variables

There are seven types of variables in c#








Static
Instance
Array elements
{stores starting address of an array in memory}
Value parameter
{without ref or out modifier}
Reference parameters {with ref modifier}
Out parameters
{with ref modifier}
Local variables
Variable Scope



Block
Procedure
Namespace
Out parameter
using System;
class Test
{
static void Divide(int a, int b, out int result, out int remainder) {
result = a / b;
remainder = a % b;
}
static void Main() {
for (int i = 1; i < 10; i++)
for (int j = 1; j < 10; j++) {
int ans, r;
Divide(i, j, out ans, out r);
Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r);
}
}
}
C # Basics : Unified Type System

Value types



Directly contain data
Cannot be null
Reference types


Contain references to objects
May be null
int i = 123;
string s = "Hello world";
i
s
123
"Hello world"
C # Basic :Unified Type System
Value (Struct)
Reference (Class)
Actual value
Memory location
Allocated
Stack
Heap
Nullability
Always has value
May be null
0
null
Aliasing
No
Yes
= means
Copy value
Copy reference
No
Yes
Variable holds
Default value
Can inherit from
Unified Type System

Everything is an object


All types ultimately inherit from object
Any piece of data can be stored, transported, and manipulated
with no extra work
object
Stream
MemoryStream
Hashtable
FileStream
int
double
Unified Type System

Polymorphism

The ability to use or store an object without knowing its
precise type
void Poly(object o) {
Console.WriteLine(o.ToString());
}
Poly(42);
Poly(“abcd”);
Poly(12.345678901234m);
Poly(new Point(23,45));
Unified Type System


Boxing copies a value type into a reference type
Unboxing copies it out
int i = 123;
i
object o = i;
o
int j = (int)o;
123
System.Int32
123
j
123
Unified Type System

Unboxing


Inverse operation of boxing
Copies the value out of the box


Copies from reference type to value type
Requires an explicit conversion

May not succeed
using System;
struct Struct1
{
Public int Value;
}
class Class1
{
public int Value = 0;
}
class Test
{
static void Main() {
Struct1 val1 = new Struct1();
Struct1 val2 = val1;
val2.Value = 123;
Class1 ref1 = new Class1();
Class1 ref2 = ref1;
ref2.Value = 123;
Console.WriteLine("Values: {0}, {1}", val1.Value, val2.Value);
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
}
}
Values: 0, 123
Refs: 123, 123
Boxing and Unboxing
Example
Unified Type System

Benefits




Enables polymorphism across all types
Collection classes work with all types
Eliminates need for wrapper classes
Lots of examples in .NET Framework
Hashtable t = new Hashtable();
t.Add(0, "zero");
t.Add(1, "one");
t.Add(2, "two");
string s = string.Format(
"Your total was {0} on {1}",
total, date);
Predefined Types
Integral Types
C# Type
System Type
Size (bytes)
Signed?
sbyte
System.Sbyte
1
Yes
short
System.Int16
2
Yes
int
System.Int32
4
Yes
long
System.Int64
8
Yes
byte
System.Byte
1
No
ushort
System.UInt16
2
No
uint
System.UInt32
4
No
ulong
System.UInt64
8
No
C# Type
System Type
Size (bytes)
float
System.Single
4
double
System.Double
8
Statements

Ecma : C# language specifications

Page 29
Operators

Is
Int i=10;
If(i is object)
{ …………….}

Sizeof
sizeof(int);

Typeof
typeof(string);
Types
User-defined Types

User-defined types
Enumerations
Arrays
Interface
enum
int[], string[]
interface
Reference type
class
Value type
struct
Function pointer
delegate
User defined Types :Enums
enum Color
{
Red,
Blue,
Green
}
class Shape
{
public void Fill(Color color) {
switch(color) {
case Color.Red:
.
break;
case Color.Blue:
.
break;
User defined Types : Arrays





Arrays allow a group of elements of a specific type to be
stored in a contiguous block of memory
Arrays are reference types
Derived from System.Array
Zero-based
Can be multidimensional


Arrays know their length(s) and rank
Provide bounds checking
User defined Types : Arrays

Declare
int[ ] primes;

Allocate
int[ ] primes = new int[9];

Initialize
int[ ] prime = new int[ ] {1,2,3,5,7,11,13,17,19};
int[ ] prime = {1,2,3,5,7,11,13,17,19};

Access and assign
prime2[i] = prime[i];

Enumerate
foreach (int i in prime) Console.WriteLine(i);
Single-dimensional arrays
using System;
class Test
{
static void Main() {
int[] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
arr[i] = i * i;
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}] = {1}", i, arr[i]);
}
}
User defined Types : Arrays

Multidimensional arrays

Rectangular




int[,] matR = new int[2,3];
Can initialize declaratively
int[,] matR =
new int[2,3] { {1,2,3}, {4,5,6} };
Jagged



An array of arrays
int[][] matJ = new int[2][];
Must initialize procedurally
User defined Types : Arrays
class Test
{
static void Main() {
int[] a1;
// single-dimensional array of int
int[,] a2;
// 2-dimensional array of int
int[,,] a3;
// 3-dimensional array of int
int[][] j2;
// "jagged" array: array of (array of int)
int[][][] j3; // array of (array of (array of int))
}
}
User defined Types : Arrays
class Test
{
static void Main() {
int[] a1 = new int[] {1, 2, 3};
int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}};
int[,,] a3 = new int[10, 20, 30];
int[][] j2 = new int[3][];
j2[0] = new int[] {1, 2, 3};
j2[1] = new int[] {1, 2, 3, 4, 5, 6};
j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
}
}
Parameter array
A parameter array enables a many-to-one relationship: many
arguments can be represented by a single parameter array. In other
words, parameter arrays enable variable length argument lists.
class Test
{
static void F(params int[] args) {
Console.WriteLine("# of arguments: {0}", args.Length);
for (int i = 0; i < args.Length; i++)
Console.WriteLine("\targs[{0}] = {1}", i, args[i]);
}
static void Main() {
F();
F(1);
F(1, 2);
F(1, 2, 3);


THANK YOU!