46
.NET VIA C# introduction to .Net concepts using c# Alexandre Marreiros, 2011

Net & c#

Embed Size (px)

DESCRIPTION

introductiob to .net framework and c#

Citation preview

Page 1: Net & c#

.NET VIA C# introduction to .Net concepts using c#

Alexandre Marreiros, 2011

Page 2: Net & c#

Agenda Introduction .NET core framework CLR JIT Compiler Managed Code Memmory Management .NET & C# CTS C# language

Alexandre Marreiros, 2011

Page 3: Net & c#

Introduction .NET is a framework that provides a big set of

cross plataform technologies

A program written in .NET can run in any system that has a implementation of .NET runtime;

Includes a virtual machine runtime;

Provides a programing API, and a unified language independent development framework;

Alexandre Marreiros, 2011

Page 4: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Common Language Runtime

Framework Class Library

Page 5: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Operating System

Common Language Runtime

Page 6: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Operating System

Common Language Runtime

.NET Framework

Page 7: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Operating System

Common Language Runtime

.NET Framework Class Lybrary

C++ C# VB.NET Perl J# F# …

Common Language Specification

Page 8: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Operating System

Common Language Runtime

.NET Framework (Base Class Library)

ADO .NET and XML

ASP .NETWeb Forms Web Services

WindowsForms Silverlight

ASP.NET MVC3

WCF WWF EF

WP7

Page 9: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Code in C#, Vb.NET …

Compiles

DLL or Exe

Managed Assembly

(IL)

*more about this at .NET IL assembler, Lindin

Page 10: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

CLR SO

Runing Program

IL Code Assembly

Compile

*more about this at .NET IL assembler, Lindin

Page 11: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

Compilation

ExecutionJIT Compiler

NativeCode

Code

Metadata

Source Code

Language Compiler

Class Loader

*more about this at .NET IL assembler, Lindin

Page 12: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

CLR

VBSource code

Compiler

C++C#

Assembly AssemblyAssemblyMSIL

Common Language Runtime JIT Compiler

Compiler Compiler

Nativecode

ManagedCode

ManagedCode

ManagedCode

UnmanagedCode

Operating System Services

*more about this at .NET IL assembler, Lindin

Page 13: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

*more about this at .NET IL assembler, Lindin

Type Descriptions

NameVersionCulture

Assembly Description

Other assembliesSecurity PermissionsExported Types

ClassesBase classesImplemented interfacesData membersMethods

Page 14: Net & c#

.NET Core FrameworkAlexandre Marreiros, 2011

*more about this at .NET IL assembler, Lindin

Assembly

Bin folder So GAC

Page 15: Net & c#

.NET Core Framework

IL (MSIL or CIL) – Intermediate Language It is low-level (machine) language, like Assembler, but

is Object-oriented

CTS is a rich type system built into the CLR Implements various types

And operations on those types

CLS is a set of specifications that all languages and libraries need to follow This will ensure interoperability between languages

Alexandre Marreiros, 2011

Page 16: Net & c#

CLR Operational Layer betwen the .NET app and

the operating system; Manages runing code like a Virtual machine

ThreadingMemory managementJit Compiling

Code access securityCode can be verified to be granted as type safeRole base security

Alexandre Marreiros, 2011

Page 17: Net & c#

JIT Compiler

Based on the CLR Class loader job, this entity is responsible for transforming IL code in to native code;

Compilation ondemand when a method is called

Alexandre Marreiros, 2011

Page 18: Net & c#

Managed Code Code that targets CLR is named as Managed

Code Object oriented Type-Safe Cross Language integration Exception Handling Verioning Represented in MSIL

Alexandre Marreiros, 2011

Page 19: Net & c#

Memmory Management

The CLR manages memory for managed code

All allocations of objects and buffers made from a Managed Heap (keyword new)

Unused objects and buffers are cleaned up automatically through Garbage Collection

*more about this at CLR via C#, Jeffrey Ritcher

Alexandre Marreiros, 2011

Page 20: Net & c#

.NET & C#Alexandre Marreiros, 2011

&

Page 21: Net & c#

CTS

Any .NET language have the same datatypes, that are based in CTS rules

At Binary Level all modules and elements writen in a .NET language are compatible

Alexandre Marreiros, 2011

Page 22: Net & c#

CTSAlexandre Marreiros, 2011

.Net Types

Value Type ReferenceType

Page 23: Net & c#

CTS – Value TypesAlexandre Marreiros, 2011

Most programming languages provide built-in data types, such as integers and floating-point numbers that are copied when they are passed as arguments (that is, they are passed by value). In the .NET Framework, these are called value type.

ValueTypes are always passed by copy between methods and in terms of memory management they work based on a stack philosophy.

Page 24: Net & c#

CTS – Reference TypesAlexandre Marreiros, 2011

Only acesseble by Reference, Are used as na overload to Object Types, Their base class are always the Object Type.

Reference types store a reference to the value’s memory address and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The data type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates.

Page 25: Net & c#

Value Types

Demo Value Type vs reference type

CTS-Value Types & Ref. Types

Alexandre Marreiros, 2011

*Explanation for study proposes http://msdn.microsoft.com/en-us/magazine/cc301569.aspx

ReferenceTypes

Page 26: Net & c#

Demo Boxing and unboxing

CTS-Boxing and Unboxing

Alexandre Marreiros, 2011

*Explanation for study proposeshttp://www.codeproject.com/Articles/76153/6-important-NET-concepts-Stack-heap-Value-types-re

Page 27: Net & c#

CTS – Types of strutures .NET defines diferent kinds of Object strutures:

Struct

Interface

Class

Abstract Class

Static Class

Enum

Alexandre Marreiros, 2011

Page 28: Net & c#

CTS – objects inner concepts Field

Method

Attribute

Propertie

Alexandre Marreiros, 2011

Page 29: Net & c#

CTS - VisibilityAlexandre Marreiros, 2011

Assembly

Private

Protected

Public

Page 30: Net & c#

CTS - InheritanceAlexandre Marreiros, 2011

Class

ClassInterfaces

Just 1 class inheritance

Many Class Inheritance

Page 31: Net & c#

Demo first Console app

Alexandre Marreiros, 2011

Page 32: Net & c#

CTS - DelegatesDelegates in .NET languages such as C# and are akin to function pointers in C++. I have found that simply being aware of this pseudonym is extremely helpful in understanding delegates. The term helps us to understand that delegates allow a developer to provide a pointer to a method/function/sub etc.

But when would a developer find this to be useful?

Alexandre Marreiros, 2011

Page 33: Net & c#

CTS - Delegates

Declaration:public delegate int Comparer(object obj1, object obj2);

Delegate event handler:

 public static int CompareFirstNames(object name1, object name2)

Alexandre Marreiros, 2011

Demo Delegates

Page 34: Net & c#

CTS - EventsAlexandre Marreiros, 2011

Page 35: Net & c#

CTS - EventsAlexandre Marreiros, 2011

A C# event is a class member that is activated whenever the event it was designed for occurs. I like to use the term "fires" when the event is activated. Anyone interested in the event can register and be notified as soon as the event fires. At the time an event fires, registered methods will be invoked.

Events and delegates work hand-in-hand to provide a program's functionality. It starts with a class that declares an event. Any class, including the same class that the event is declared in, may register one of its methods for the event. This occurs through a delegate, which specifies the signature of the method that is registered for the event. The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever is appropriate, you assign the delegate to the event, which effectively registers the method that will be called when theevent fires

Page 36: Net & c#

CTS - EventsAlexandre Marreiros, 2011

Declare delegatepublic delegate void Startdelegate()

Declare Event

public event Startdelegate StartEvent;

Register as a EventListner

 StartEvent += new Startdelegate(OnStartEvent);

Page 37: Net & c#

CTS - EventsAlexandre Marreiros, 2011

Demo Event

Page 38: Net & c#

C#Alexandre Marreiros, 2011

Now that you already now the basics of .Net Let’s explore a litle bit c# language

Page 39: Net & c#

C#Alexandre Marreiros, 2011

Simple, modern, general-purpose, object-oriented programming language.

Strong type checking, array bounds checking, detection of attempts to use un-initialized variables.

Automatic garbage collection.

Page 40: Net & c#

C#Alexandre Marreiros, 2011

Support for internationalization.

Suitable for writing applications for both hosted and embedded systems.

Low memory and processing power requirements.

There are no global variables.

Code is compiled as managed

Page 41: Net & c#

C#Alexandre Marreiros, 2011

Compare operators

== Equal

!= Not equal

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

Page 42: Net & c#

C# - Decision MethodsAlexandre Marreiros, 2011

if( cond ){}else{}

switch(s){   case "1":      Console.WriteLine("You entered 1");      break;   case "2":      Console.WriteLine("You entered 2");      break;   case "3":      Console.WriteLine("You entered 3");      break;   default:      Console.WriteLine("You entered some other number");      break;}

Page 43: Net & c#

C#-LoopsAlexandre Marreiros, 2011

for (x = 1; x <= 5; x++){}

while (y <= 5){}

Do{} while (y<=5);

Foreach(object o in array);

Page 44: Net & c#

C#Alexandre Marreiros, 2011

Other concepts hands on and final considerations

Questions?

Page 45: Net & c#

References Books:

CLR via c#, Richter .Net fundamentals, Don Box .Net IL assembler, Lindin

Visit following web sites: .NET Framework Home Site –

http://msdn.microsoft.com/netframework/ The Microsoft .NET Framework Community –

http://www.gotdotnet.com/ Code Project – http://www.codeproject.net/ Mono – Open Source .NET Framework – http://www.go-mono.org/ Rotor – Shared Source .NET CLI – http://

msdn.microsoft.com/net/sscli/

Read the news groups: news://msnews.microsoft.com/microsoft.public.dotnet.framework

Alexandre Marreiros, 2011

Page 46: Net & c#

Contacts

Alexandre Marreiros, 2011

Email: [email protected]: @alexmarreiros

Feel free to ask