Sei sulla pagina 1di 17

Advantages of .

NET

• Object-oriented programming
• Language independence
Compile to a common Intermediate Language
Efficient data access — A set of .NET components, collectively
known as ADO.NET, provides
efficient access to relational databases and a variety of data
sources.

Code sharing
introducing the concept of the assembly, which replaces the
traditional DLL. Assemblies have formal
facilities for versioning, and different versions of assemblies can
exist side by side
Amila Thennakoon (British Computer Socity )
1
The Common Language Runtime
NET Framework is its runtime execution environment, known as the Common Language
Runtime (CLR) or the .NET runtime. Code running under the control of the CLR is often
termed
managed code.

before executed by the CLR, any source code that you develop (in C# or some other
language) needs to be compiled. Compilation occurs in two steps in .NET:

1. Compilation of source code to Microsoft Intermediate Language (IL).


2. Compilation of IL to platform-specific code by the CLR.

IL
• based on numeric codes rather than text
• which can be quickly translated into native machine code
• platform independence,
• performance improvement,
• language interoperability
Amila Thennakoon (British Computer Socity ) 2
Common Type System

data type problem is solved in .NET using the Common Type System (CTS). The
CTS defines the
predefined data types available in IL so that all languages that target the .NET
Framework can produce
compiled code ultimately based on these types

Amila Thennakoon (British Computer Socity ) 3


Common Language Specification

The Common Language Specification (CLS) works with the CTS to ensure
language interoperability.

Amila Thennakoon (British Computer Socity ) 4


Garbage Collection

The garbage collector is .NET’s answer to memory management


Garbage collection works in .NET because IL has been designed to facilitate the process

Amila Thennakoon (British Computer Socity ) 5


Namespace s

Namespaces are the way that .NET avoids name clashes between classes

Amila Thennakoon (British Computer Socity ) 6


Core C#

Initialization of Variables

C# 3.0 introduced a var keyword that can be used to declare implicit types. The purpose of
implicit types is to store any type in a variable.
The following code snippet declares an implicit variable that stores a string.
var name = "Mahesh Chand";

There are a few rules that you need to follow:


➤➤ The variable must be initialized. Otherwise, the compiler doesn’t have anything from which to infer
the type.
➤➤ The initializer cannot be null.
➤➤ The initializer must be an expression.
➤➤ You can’t set the initializer to an object unless you create a new object in the initializer.

Amila Thennakoon (British Computer Socity ) 7


Variable Scope

The scope of a variable is the region of code from which the variable can be
accessed.

the scope is
determined by the following rules:
➤➤ A field (also known as a member variable) of a class is in scope for as long as its
containing class is in
scope.
➤➤ A local variable is in scope until a closing brace indicates the end of the block statement or
method in
which it was declared.
➤➤ A local variable that is declared in a for, while, or similar statement is in scope in the body
of that loop.

Amila Thennakoon (British Computer Socity ) 8


Constants

constant is a variable whose value cannot be changed throughout its lifetime


Prefixing a variable with the const keyword when it is declared and initialized
designates that variable as a
constant:
const int a = 100; // This value cannot be changed.

They must be initialized when they are declared; and after a value has been assigned, it can
never be
overwritten.
➤➤ The value of a constant must be computable at compile time. Therefore, you can’t
initialize a constant
with a value taken from a variable. If you need to do this, you must use a read-only field
(this is
explained in Chapter 3).
➤➤ Constants are always implicitly static. However, notice that you don’t have to (and, in
fact, are not
permitted to) include the staticAmila
modifier in the constant declaration.
Thennakoon (British Computer Socity ) 9
Constants make your programs easier to modify
Ex Tax constant can apply to every where and only modify in one place
Constants help prevent mistakes in your programs
Cant assign another value to constant in anywhere in the programe

Amila Thennakoon (British Computer Socity ) 10


Pre defined Data Type s

C# distinguishes between two


categories of data type:
➤➤ Value types
➤➤ Reference types

Conceptually, the difference


is that a value type stores its value directly, whereas a reference type stores a
reference to the value.

These types are stored in different places in memory; value types are stored in an
area known as the stack,
and reference types are stored in an area known as the managed heap.

If a variable is a reference, it is possible to indicate that it does not refer to any


object by setting its value to null:
y = null;
Amila Thennakoon (British Computer Socity ) 11
In C#, basic data types such as bool and long are value types. This means that if
you declare a bool
variable and assign it the value of another bool variable, you will have two
separate bool values in memory.
Later, if you change the value of the original bool variable, the value of the
second bool variable does not
change. These types are copied by value.

Copied by reference
Copied by Value

Amila Thennakoon (British Computer Socity ) 12


The Common Language Runtime (CLR) implements an
elaborate algorithm to track which reference variables are still reachable and
which have been orphaned.
Periodically, the CLR will destroy orphaned objects and return the memory that
they once occupied back to
the operating system. This is done by the garbage collector.

If you want to define your own type as a value type, you should declare it as a struct

C# has 15 predefined types, 13


value types, and 2 (string and object) reference types.

Amila Thennakoon (British Computer Socity ) 13


Integer Types
C# supports eight predefined integer types

Amila Thennakoon (British Computer Socity ) 14


Amila Thennakoon (British Computer Socity ) 15
1. Sequence
When a program simply needs to execute a series of steps in order and ALL
steps must be completed, program instructions are written in sequence.

Amila Thennakoon (British Computer Socity ) 16


For example, a flowchart to show typical night
time activities:

Amila Thennakoon (British Computer Socity ) 17

Potrebbero piacerti anche