[go: up one dir, main page]

0% found this document useful (0 votes)
10 views38 pages

Chapter 1 cgc

Uploaded by

my689013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views38 pages

Chapter 1 cgc

Uploaded by

my689013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Introducing C# and the .

NET Framework

Er. Riddhi K. Shrestha


Introduction to C#

❖ Microsoft introduced a new language called C# (pronounced C Sharp).


❖ C# is designed to be a simple, modern, general-purpose, object-oriented programming language,
borrowing key concepts from several other languages, most notably Java.
❖ C# was developed by Anders Hejlsberg and his team during the development of .Net
Framework.
❖ C# is a part of .NET Framework.

2
Introduction to C#

❖ C# could theoretically be compiled to machine code, but in real life, it’s always used in
combination with the .NET framework.
❖ Therefore, applications written in C#, requires the .NET framework to be installed on the
computer running the application.
❖ While the .NET framework makes it possible to use a wide range of languages, C# is sometimes
referred to as THE .NET language, perhaps because it was designed together with the
framework.
❖ Language interoperability is the ability of code to interact with code that is written by using a
different programming language.

3
Features of C#

❖ Type-safety Checking
❖ Object-oriented Programming
❖ Garbage Collection
❖ Generic Types and Methods
❖ Iterators
❖ Methods with named Arguments
❖ Methods with optional Arguments
❖ Static Classes

4
Features of C#

❖ Nullable Types
❖ Auto-implemented Properties
❖ Accessor Accessibility
❖ Anonymous Methods
❖ Parallel Computing
❖ Auto-implemented Properties
❖ Partial Classes

5
Features of C#

❖ Object-oriented Programming: Focuses on objects so that code written once can be reused. This helps
reduce time and effort on the part of developers.
❖ Type-safety Checking: Checked the overflow of types because uninitialized variables cannot be used in
C# as C# is a case-sensitive language.
❖ Garbage Collection: Performs automatic memory management from time to time and spares the
programmer the task.
❖ Generic Types and Methods: Are a type of data structure that contains code that remains the same
throughout but the data type of the parameters can change with each use.
❖ Iterators: Enable looping (or iterations) on user-defined data types with the for each loop.
❖ Static Classes: Contain only static members and do not require instantiation.
❖ Partial Classes: Allow the user to split a single class into multiple source code (.cs) files.

6
Features of C#

❖ Anonymous Methods: Enable the user to specify a small block of code within the delegate declaration.
❖ Methods with named Arguments: Enable the user to associate a method argument with a name rather
than its position in the argument list.
❖ Methods with optional Arguments: Allow the user to define a method with an optional argument with a
default value.
❖ Nullable Types: Allow a variable to contain a value that is undefined.
❖ Accessor Accessibility: Allows the user to specify the accessibility levels of the get and set accessors.
❖ Auto-implemented Properties: Allow the user to create a property without explicitly providing the
methods to get and set the value of the property.
❖ Parallel Computing: Support for parallel programming using which develop efficient, fine-grained, and
scalable parallel code without working directly with threads or the thread pool.

7
Object Orientation

● C# is a rich implementation of the object-orientation paradigm, which includes encapsulation, abstraction, inheritance and
polymorphism.
● The features of C# from an object-oriented perspective are:
● Unified type system:
○ The fundamental building block in C# is an encapsulated unit of data and functions called a type.
○ C# has a unified type system, where all types ultimately share a common base type. This means that all types, whether they
represent business objects or are primitive types such as numbers, share the same basic functionality.
○ For example, an instance of any type can be converted to a string by calling its ToString method.
● Classes and interfaces:
○ In a traditional object-oriented paradigm, the only kind of type is class.
○ In C# , there are several other kinds of types, one of which is an interface.
○ An interface is like a class, except that it only describe members. The implementation for those members come from types that
implement the interface.
○ Interfaces are particularly useful in scenarios where multiple inheritance is required.
○ C # doesn’t support multiple inheritance of classes.

8
Object Orientation

● Properties, method, and events:


○ In the pure object-oriented paradigm, all functions are methods.
○ In C#, methods are only one kind of function member, which also include properties and events.
○ Properties are function members that encapsulate a piece of an object’s state.
○ While C# is primarily an object-oriented language, it also borrows from the functional programming
paradigm.

9
Type Safety

● C# is primarily a type-safe language, meaning that instances of types can interact only through protocols
they define, thereby ensuring each type’s internal consistency.
● For instance, C# prevents you from interacting with a string type as though it were an integer type.
● More specifically, C# supports static typing , meaning that the language enforces type safety at compile
time. This in addition to type safety being enforced at run-time.
● Static typing eliminates a large class of errors before a program is even run. It shifts the burden away
from runtime unit tests onto the compiler to verify that all the types in a program fit together correctly.
This makes large programs much easier to manage, more predictable, and more robust. Furthermore,
static typing allows tools such as IntelliSense in Visual Studio to help you write a program, since it knows
for a given variable what type it is, and hence what methods you can call on that variable.
● C# also called a strongly typed language because its types rules are very strict.

10
Memory Management

● C# relies on the runtime to perform automatic memory management.


● The Common Language Runtime has a Garbage Collector that executes as part of your program,
reclaiming memory for objects that are no longer referenced.
● This frees programmers from explicitly deallocating the memory for an object, eliminating the
problem of incorrect pointers encountered in languages such as C++.

11
Platform Support

● Historically, C# was used almost entirely for writing code to run on Windows platforms.
● Recently, however, Microsoft and other companies have invested in other platforms, including
Linux, macOS, iOS and Andriod.

12
Applications Of C#
❖ Gaming applications
❖ Web applications
❖ Mobile applications for pocket PCs, PDAs, and cell phones
❖ Web services
❖ Cloud applications
❖ Simple standalone desktop applications such as Library Management Systems, Student Mark
Sheet generation, and so on
❖ Large-scale enterprise applications
❖ Complex distributed applications that can spread over a number of cities or countries

13
.Net Framework

❖ .NET is a programming framework created by Microsoft that developers can use to create
applications more easily.
❖ The first beta version released in 2000 and first version of the .Net framework was released in
the year 2002
❖ A framework is just a bunch of code that the programmer can call without having to write it
explicitly.
❖ It provides a controlled programming environment where software can be developed, installed
and executed on Windows-based operating systems.
❖ It is basically a collection of libraries.

14
.Net Framework

❖ Is a programming platform that is used for developing Windows, Web-based, and mobile software.
❖ It has a number of pre-coded solutions that manage the execution of programs written specifically for
the framework.
❖ A programmer can develop applications using one of the languages
(C#.NET,VB.NET,C++.NET,J#.NET,F#.NET,JSCRIPT.NET,WINDOWS POWERSHELL,IRON RUBY, IRON
PYTHON,C OMEGA,ASML(Abstract State Machine Language)) supported by .NET.
❖ The .NET Framework is an infrastructure that is used to build, deploy, and run different types of
applications and services using .NET technologies.
❖ The .NET Framework is an infrastructure that is used to Minimize software development, deployment,
and versioning conflicts.

15
.NET Architecture

16
.NET Architecture

Language WinForms, ADO.NET, ASP.NET etc

Framework Class Libraries ( FCL )


Library

Common Language Common Language Runtime ( Execution Engine )


Runtime

17
.NET Architecture - Language

❖ The types of applications that can be built in the .NET framework is classified broadly into the
following categories :
➢ WinForms: This is used for developing Forms-based applications, which would run on an end user machine
➢ ASP.NET: This is used for developing web-based applications, which are made to run on any browser such
as Internet Explorer, Chrome or Firefox. (Active Server Page)
➢ ADO.NET: This technology is used to develop applications to interact with Databases such as Oracle or
Microsoft SQL Server (ActiveX Data Object)

18
.NET Architecture - Library

❖ A class library is a collection of methods and functions that can be used for the core purpose.
❖ e.g. - class library with methods to handle all file-level operations. So there is a method which
can be used to read the text from a file. Similarly, there is a method to write text to a file.

19
.NET Architecture - Library

FCL (Framework Class Library)

The BCL (Base Class Library) is the core of the FCL


and provides basic functionalities. provides basic functionalities.

20
Common Language Runtime (CLR)

It is a program execution engine that loads and


executes the program.

It converts the program into native code.

It acts as an interface between the framework and


operating system.

21
Common Language Runtime (CLR)

It is a program execution engine that loads and


executes the program.

It converts the program into native code.

It acts as an interface between the framework and


operating system.

22
Common Language Runtime (CLR)

❖ Suppose we have written a C# program and save it in a file.


❖ Language specific compiler compiles the source code into the MSIL(Microsoft Intermediate
Language) which is also known as the CIL(Common Intermediate Language) or IL(Intermediate
Language) along with its metadata.
❖ Metadata includes the all the types, actual implementation of each function of the program. MSIL
is machine independent code.
❖ Now CLR comes into existence. CLR provides the services and runtime environment to the MSIL
code. Internally CLR includes the JIT(Just-In-Time) compiler which converts the MSIL code to
machine code which further executed by CPU.

23
Architecture of CLR

24
Main components of CLR

1. Common Language Specification (CLS)


2. Common Type System (CTS)
3. Garbage Collection (GC)
4. Just In – Time Compiler (JIT)

25
Main components of CLR

❖ Common Language Specification (CLS) : It is responsible for converting the different .NET
programming language syntactical rules and regulations into CLR understandable format.
Basically, it provides the Language Interoperability.
❖ Common Type System (CTS) :Every programming language has its own data type system, so CTS
is responsible for understanding all the data type of system.
❖ Garbage Collection (GC): It is used to provide the Automatic Memory Management feature.
❖ Just In – Time Compiler (JIT) : It is responsible for converting the CIL(Common Intermediate
Language) into machine code or native code using the Common Language Runtime environment.

26
Download and Install .NET Framework

1. Install Visual Studio

or

1. Download .Net Framework from .NET Framework , choose developer pack/runtime pack and
install it in your system.

27
Download and Install .NET Framework

3. Set your .NET Framework install directory in Environment Variable


a. Copy the .NET Framework installed directory path

28
Download and Install .NET Framework

3. Set your .NET Framework install directory in Environment Variable


b. On your window search: Edit Environment variables for your account and click on searched result

29
Download and Install .NET Framework

3. Set your .NET Framework install directory in Environment Variable


c. Click on : Environment variables after that click on Path and Edit

30
Download and Install .NET Framework

3. Set your .NET Framework install directory in Environment Variable


d. Click on : New & then paste the copied .NET framework installed directory location and Click Ok (3 times)

31
Download and Install .NET Framework

3. Set your .NET Framework install directory in Environment Variable


e. To check and confirm CSC access -> Open CMD terminal and type CSC

32
Example - write and compile source code

1. Open your Notepad.


2. And type following example source code and save the file with HelloWorld.cs extension.

33
Example - write and compile source code

3. Go to your file directory where you save your HelloWorld.cs source file.
4. And type following example source code and save the file with HelloWorld.cs extension.

34
Example - write and compile source code

5. To compile type: csc HelloWorld.cs

5. Again type: dir

35
Example - write and compile source code

7. To run compiled file: HelloWorld

36
37
Happy Coding !!!

38

You might also like