You are on page 1of 47

1

.NET and C# Basics

part 1

Introduction

www.luxoft-training.com
2

Module Content
 .Net Framework Overview

 Using IDE

 Code Organization

www.luxoft-training.com
3

.Net Platform Overview


 Definition

 Advantages over Other Platforms

 Supported Programming Languages

 Structure (Libraries, Classes)


 Base Class Library

 Common Language Runtime

www.luxoft-training.com
4

Definition
.NET Framework is a software framework developed by Microsoft which
runs primarily on Microsoft Windows.

It includes a large class library named Framework Class Library (FCL)


and provides language interoperability

 (each language can use code written in other languages)

www.luxoft-training.com
5

Benefits of .Net Framework


 A unified development model
 A simplified CLR programming model
 No problems with versions
 Easy deployment
 Integration of programming languages
 Easy reuse of code

www.luxoft-training.com
6

Benefits of .Net Framework


 Automated memory management (garbage
collection)
 Type security check
 A single principle of failure handling
 Security
 Interoperability with existing code

www.luxoft-training.com
7

Current .Net Framework Languages


 C#: Most widely used CLI language, bearing similarities to Java, Object
Pascal (Delphi) and C++
 C++/CLI: A version of C++ including extensions for using Common
Language Runtime (CLR) objects
 F#: A multi-paradigm CLI language supporting functional programming
and imperative object-oriented programming disciplines
 Visual Basic .NET (VB.NET): A redesigned dialect of Visual Basic
 IronPython: An open-source CLI implementation of Python, built on
the Dynamic Language Runtime (DLR)

www.luxoft-training.com
8

.Net Framework Structure

www.luxoft-training.com
9

What can we develop?


In addition to classical console applications,
libraries and services, .Net framework
(using various technologies) allows to create:
 Desktop applications (WPF)
 Web applications (ASP.NET)
 Mobile applications (Xamarin)

www.luxoft-training.com
10

BCL - Base Class Library


BCL is a base class library of the .NET Framework platform.

Programs written in any of the languages supporting .NET Framework can


use BCL classes and methods:
 to create class objects
 to inherent required BCL classes

www.luxoft-training.com
11

BCL - Base Class Library


 BCL is not a kind of “add-on” for the operating system functions or for
some API.
 It can be viewed as an API of .NET virtual machine.

www.luxoft-training.com
12

BCL - Base Class Library

www.luxoft-training.com
13

CLR - Common Language Runtime


The Common Language Runtime is a runtime for CIL (MSIL) byte code
for compiling programs written in .NET compatible programming
languages.

It is a kind of virtual machine

www.luxoft-training.com
14

The Common Language Runtime

www.luxoft-training.com
15

The Common Language Runtime


Base Class Library Support

Thread Support COM Marshaler

Type Checker Exception Manager

Security Engine Debug Engine

MSIL to Native Code Garbage


Compilers Manager Collector

Class Loader

www.luxoft-training.com
16

Using MS Visual Studio


 Project structure
 Main files
 Code entry point

 Assembly

 Debugging. Debugging windows

www.luxoft-training.com
17

Solution
Every project represents a service, library, or
application.
They all are compiled into an assembly.

Solution means a logical combination of


projects within a single IDE.

www.luxoft-training.com
18

Assembly
 Definition

 Assembly properties

 Main files

 Strong named assemblies

 Global assembly cache

www.luxoft-training.com
19

Assembly
An assembly is the main unit of
 deployment
 version management
 reuse
 activation and security permit areas

www.luxoft-training.com
20

Assembly Properties
An assembly has the following properties:

 It contains code executed in the CLR

 It creates the security boundary

 It creates the type boundary

 It creates the reference scope

www.luxoft-training.com
21

Assembly Properties
 It creates the version scope

 Forms a unit of deployment

 It is a unit for which side-by-side

execution is supported

www.luxoft-training.com
22

Main files
Depending on the project type and technologies used, the set of files will
be slightly different.

 The entire С# code is in .cs files


 All information about an assembly is in the file AssemblyInfo.cs
 Configuration file - App.config
 The References tab contain links to the project’s external dependencies
(other assemblies)

www.luxoft-training.com
23

Where execution starts


The entry point (where the program starts for console applications) is the
function Main.
Start

End

www.luxoft-training.com
24

Strong named assemblies


Strong name includes:
 name
 version
 information on the language and regional culture
 public key and digital signature

www.luxoft-training.com
25

Strong named assemblies


 A strong named assembly can use types only from strong named
assemblies
 A strong named assembly “informs” about its origin
 It’s easy to apply a security policy
 Versioning is guaranteed
 Only a strong named assembly can be placed in GAC

www.luxoft-training.com
26

Assembly Structure
Assembly disassembled with IL DASM :

Public types

www.luxoft-training.com
27

Assembly Structure
Assembly disassembled with IL DASM :
Assembly version

External references and their SN tokens

www.luxoft-training.com
28

Assembly Structure
Assembly disassembled with IL DASM :

Public key

www.luxoft-training.com
29

Creating a strong named assembly


 By using Visual Studio
 By using the utilities Strong Name Tool (key generation) and
Assembly Linker

www.luxoft-training.com
30

Global Assembly Cache


 On every computer with the installed CLR
there is machine-level code cache called
global assembly cache.

 The global assembly cache stores


assemblies intended for sharing by multiple
applications on the computer.

www.luxoft-training.com
31

Benefits of using GAC


 Common access – no need to keep copies of third party dependencies

 Guarantee of the assembly correct version

 Only strong named assemblies

 High performance

www.luxoft-training.com
32

Debugging
Debugging is a stage in software program development where errors are
identified, localized and fixed. To understand where an error occurred, you
have to:

 Know current values of variables

 Find the path of program execution

www.luxoft-training.com
33

Debugging
 Visual Studio Debugger
 Setting breakpoint, breakpoint conditions
 Navigating the code
 Viewing/changing the values of variables in the process of debugging

www.luxoft-training.com
34

Breakpoints
breakpoint - is an intentional stopping the
program
execution when a debugger is called.

After moving to the debugger, you can explore the


program state (logs, memory, CPU registers,
stacks, etc.) to find out if the program behaves
correctly.

www.luxoft-training.com
35

Call Stack
 Using the Call Stack window you can view function and procedure
calls that are currently in the stack.

 The Call Stack window displays the name of each function and
programming language it is written in.

www.luxoft-training.com
36

DEMO

www.luxoft-training.com
37

Code Organization
 Namespace
 Comments
 Documentation

www.luxoft-training.com
38
Namespace
 The namespace is used for preventing name conflicts.

Console type is declared in the System

System namespace

www.luxoft-training.com
39
Namespace

You can include any code into your namespace

www.luxoft-training.com
40
General recommendations on naming
 A name should reflect functionality available in the namespace
 Do not introduce names of generic types
 Use the names in the namespace in plural if possible
 Add a prefix consisting of the company name to the names in the
namespace
 On the second level of name hierarchy in the namespace, use a
permanent, version-independent product name

www.luxoft-training.com
41
Namespace
 A good practice — Extend your namespace in case of project files
nesting

www.luxoft-training.com
42

Comments
 Single-line comment:

 Delimited comment:

www.luxoft-training.com
43

Comments
During code compilation, comments are
deleted and do not increase the application
size.
Make comments to those parts of code where
you have sophisticated or unclear logic or
complex algorithms. But here you should ask
yourself a question: “Is this code complex or
just a bad one?” :)

www.luxoft-training.com
44

Documentation Generation
 It is generated together with the build.
 Stubs are places with a subtle movement of the hand.

www.luxoft-training.com
45

Documentation Generation

www.luxoft-training.com
46

Summary
 .Net Framework Overview

 Definition

 Advantages over Other Platforms

 Supported Programming Languages

 Structure (Libraries, Classes)

 Using IDE

 Code Organization

www.luxoft-training.com
47

Questions

www.luxoft-training.com

You might also like