SlideShare a Scribd company logo
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 2
What is the C# Programming Language?
C# (pronouncedas“C Sharp”) is a simple,modern,object-oriented,andtype-safeprogramminglanguage.C#
language hasitsroots inC languagessuchasC, C++, andit ismostlysimilartoJavaprogramming.
C# is a stronglytypedobject-orientedprogramminglanguage.C#isopensource,simple,modern,flexible,and
versatile.Inthisarticle,letuslearnwhatC#is,what C# can do,and how C# is differentfromC++ andother
programminglanguages.
C# is a programminglanguage of .NetFramework.
C# is pronouncedas"C-Sharp".Itis an object-orientedprogramminglanguage providedbyMicrosoftthatruns on
.NetFramework.
C# is a programminglanguage developedandlaunchedbyMicrosoftin2001. C# is a simple,modern,andobject-
orientedlanguage thatprovidesmoderndaydevelopersflexibilityandfeaturestobuildsoftwarethatwill notonly
worktoday butwill be applicable foryearsinthe future.
A programminglanguage oncomputerscience isalanguage thatis usedto write software programs.
C# Programminglanguage will allowdeveloperstobuildavarietyof secure androbustapplicationssuchas
windowsapplications,webapplications,database applications,etc.,whichwill runon .NETFramework.
C# programminglanguage hasbeenbuilton .NETFramework torun the C# applications.We are requiredtoinstall
a .NET Framework componentonourmachines.
.NET Framework isa developmentplatformforbuildingappsforwindows,web,azure,etc.,byusingprogramming
languagessuchas C#, F#, and Visual Basic.Itconsistsof twomajor components,suchas CommonLanguage
Runtime (CLR),anexecutionenginethathandlesrunningapps,and .NETFrameworkClassLibrary,whichprovidesa
libraryof testedandreusable code thatdeveloperscanuse itintheirapplications.
C# Features
C# is object oriented programming language. It provides many features that are given below.
Simple
Modernprogramminglanguage
Objectoriented
Type safe
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 3
Interoperability
Scalable andUpdateable
Componentoriented
Structuredprogramminglanguage
Rich Library
Fast speed
Modernand easy
Fast and opensource
Crossplatform
Safe
Versatile
Simple
C# is a simple language in the sense that it provides structured approach (to break the problem into parts), rich set of
library functions, data types etc.
ModernProgramming Language
C# programming is based upon the current trend and it is very powerful and simple for building scalable,
interoperable and robust applications.
ObjectOriented
C# is object oriented programming language. OOPs makes development and maintenance easier where as in
Procedure-oriented programming language it is not easy to manage if code grows as project size grow.
Type Safe
C# type safe code can only access the memory location that it has permission to execute. Therefore,it improves a
security of the program.
Interoperability
Interoperability process enables the C# programs to do almost anything that a native C++ application can do.
Scalable and Updateable
C# is automatic scalable and updateable programming language. For updating our application, we delete the old files
and update them with new ones.
ComponentOriented
C# is component oriented programming language. It is the predominant software development methodology used to
develop more robust and highly scalable applications.
Structured Programming Language
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 4
C# is a structured programming language in the sense that we can break the program into parts using functions.
Therefore,it is easy to understand and modify.
Rich Library
C# provides many inbuilt functions that makes the development fast.
Fast Speed
The compilation and execution time of C# language is fast.
C# is modern and easy
C# is a simple,modern,andanobject-orientedprogramminglanguage.The purpose of C#was to developa
programminglanguage thatisnot onlyeasytolearnbut alsosupportsmoderndayfunctionalityforall kindof
software development.
If you lookat the historyof programminglanguagesandtheirfeatures,eachprogramminglanguage wasdesigned
for a specificpurpose tosolve aspecificneedatthat time.
C# language howeverwas designedtokeepbusinessandenterprisesneedsinmind.C#language wasdesignedfor
businessestobuildall kindsof software byusingone single programminglanguage.
C# providesfunctionalitytosupportmoderndaysoftware development.C#supportsWeb,Mobile,andapp
developmentneeds.Some of the modern-dayprogramminglanguagefeaturesC#supportsare generics,vartypes,
auto initializationof typesandcollections,lambdaexpressions,dynamicprogramming,asynchronousprogramming,
tuples,pattern matching,advanceddebuggingandexceptionhandling,andmore.
C# language syntaxesare influencedfromC++,Java, Pascal and few otherlanguagesthatare easyto adopt.C# also
avoidscomplexityandunstructuredlanguagefeatures.
C# is fast and opensource
C# is opensource underthe .NET Foundation,whichisgovernedandrunindependentlyof Microsoft.C#language
specifications,compilers,andrelatedtoolsare opensource projectsonGitHub.While C#language feature designis
ledbyMicrosoft, the opensource communityisveryactive inthe language developmentandimprovements.
C# is fastcompare to several otherhigh-level programminglanguages.C#8 has manyperformance improvements.
C# is cross platform
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 5
C# is cross platformprogramming language.Youcan build.NETapplicationsthatcanbe deployedonWindows,
Linux,andMac platforms.C#apps can alsobe deployedincloudandcontainers.
C# is safe and efficient
C# is a type safe language.C#doesnot allowtype conversionsthatmay leadto data lossor otherproblems.C#
allowsdeveloperstowrite safe code.C#alsofocusesonwritingefficientcode.
Here is a listof some of the keyconceptsinC# that helpswrite safe andefficientcode.
Unsafe type castingisnot allowed.
Nullable andnon-null abletypesare supportedinC#.
Declare a readonlystructto expressthata type isimmutable andenablesthe compilertosave copieswhenusingin
parameters.
Use a ref readonlyreturnwhenthe returnvalue isa structlarger thanIntPtr.Size andthe storage lifetime isgreater
than the methodreturningthe value.
Whenthe size of a readonlystructisbiggerthanIntPtr.Size,youshouldpassitasan in parameterforperformance
reasons.
Neverpassa struct as an inparameterunlessitis declaredwiththe readonlymodifierbecauseitmaynegatively
affectperformance andcouldleadtoan obscure behavior.
Use a ref struct, or a readonlyref structsuch as Span<T> or ReadOnlySpan<T>towork withmemoryasa sequence
of bytes.
C# is versatile
C# is a Swissarmyknife.While mostprogramminglanguagesweredesignedforaspecificpurpose,C#wasdesigned
to do C#. We can use C# to buildtoday’smodernsoftware applications.C#can be usedto developall kindof
applicationsincludingWindows clientapps,componentsandlibraries,servicesandAPIs,Webapplications,Mobile
apps,cloudapplications,andvideogames.
Here is a listof typesof applicationsC#can build:
Windowsclientapplications
Windowslibrariesandcomponents
Windowsservices
Webapplications
WebservicesandWebAPI
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 6
Native iOSandAndroidmobile apps
Backendservices
Azure cloudapplicationsandservices
Backenddatabase usingML/Data tools
InteroperabilitysoftwaresuchasOffice,SharePoint,SQLServerandsoon.
Artificial IntelligenceandMachine learning
Blockchainsand distributedledgertechnologyincludingcryptocurrency
Internetof Things(IoT) devices
Gamingconsolesandgamingsystems
Videogames
C# History
History of C# language isinterestingtoknow.Here we are goingto discussbrief historyof C#language.C#is
pronouncedas "C-Sharp".Itis an object-orientedprogramminglanguage providedby Microsoftthatruns on .Net
Framework. AndersHejlsbergisknownas the founderof C# language.
It isbasedon C++ and Java, but ithas many additional extensionsusedtoperformcomponentoriented
programmingapproach.
C# has evolvedmuchsince theirfirstrelease inthe year 2002. It was introducedwith .NETFramework 1.0 and the
currentversionof C# is 10.0.
C# Version History
C# was first introduced with .NET Framework 1.0 in the year 2002 and evolved much since then. The
following table lists important features introduced in each version of C#:
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 7
Version .NET Framework Visual Studio Important Features
C# 1.0 .NET Framework 1.0/1.1 Visual Studio .NET 2002 Basic features
C# 2.0 .NET Framework 2.0 Visual Studio 2005
Generics
Partial types
Anonymous methods
Iterators
Nullable types
Private setters (properties)
Method group conversions (delegates)
Covariance and Contra-variance
Static classes
C# 3.0 .NET Framework 3.03.5 Visual Studio 2008
Implicitly typed local variables
Object and collection initializers
Auto-Implemented properties
Anonymous types
Extension methods
Query expressions
Lambda expressions
Expression trees
Partial Methods
C# 4.0 .NET Framework 4.0 Visual Studio 2010
Dynamic binding (late binding)
Named and optional arguments
Generic co- and contra variance
Embedded interop types
C# 5.0 .NET Framework 4.5 Visual Studio 2012/2013
A sync features
Caller information
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 8
Version .NET Framework Visual Studio Important Features
C# 6.0 .NET Framework 4.6 Visual Studio 2013/2015
Expression Bodied Methods
Auto-property initializer
name of Expression
Primary constructor
Await in catch block
Exception Filter
String Interpolation
C# 7.0 .NET Core 2.0 Visual Studio 2017
out variables
Tuples
Discards
Pattern Matching
Local functions
Generalized a sync return types
C# 8.0 .NET Core 3.0 Visual Studio 2019
Read-only members
Default interface methods
Using declarations
Static local functions
Disposable ref structs
Nullable reference types
More
C# 9.0 .NET 5.0 Visual Studio 2019
Records
Init-only properties
Top-level statements
Init accessors and readonly fields
With-expressions
Value-based equality
more
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page | 9
Version .NET Framework Visual Studio Important Features
C# 10.0 .NET 6.0 Visual Studio 2022
Record structs
Global using directives
File-scoped namespace declaration
Extended Proptery Patterns
Null Parameter Checking
Constant interpolated strings
more
C# Example:HelloWorld
In C# programming language, a simple "hello world" program can be written by multiple ways. Let us see the top
four ways to create a simple C# example:
Simple Example
Using System
Using public modifier
Using namespace
C# Simple Example
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
Output:
Hello World!
Description
class: is a keyword, which is used to define class.
Program: is the class name. A class is a blueprint or template from which objects are created. It can have data
members and methods. Here,it has only Main method.’ static: is a keyword that means object is not required to
access static members. So it saves memory.
void: is the return type of the method. It does't return any value. In such case,return statement is not required.
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page |
10
Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main()
method is invoked first before any other method. It represents start up of the program.
string[] args: is used for command line arguments in C#. While running the C# program, we can pass values. These
values are known as arguments which we can use in the program.
System.Console.WriteLine("Hello World!"): Here,System is the namespace. Console is the class defined in
System namespace. The WriteLine() is the static method of Console class which is used to write the text on the
console.
C# Example: Using System
If we write using Systembefore the class,it means we don't need to specify System namespace for accessing any
class of this namespace. Here,we are using Console class without specifying System.Console.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:
Hello World!
C# Example: Using public modifier
We can also specify public modifier before class and Main() method. Now, it can be accessed from outside the class
also.
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page |
11
Hello World!
C# Example: Using namespace
We can create classes inside the namespace. It is used to group related classes. It is used to categorize classes so that
it can be easy to maintain.
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output:
C# Program Structure
A typical C# Program consists ofseveral different parts as shown below:
Namespace
Class
The main method
Methods inside the class
Class definition or class attributes
Statements
Comments
Few things need to be kept in mind while writing a C# program. C# programs are case sensitive, which means
“string” is different from “String”. All the statements written in the program must be concluded with a semicolon i.e.
“;”. A semicolon tells the program that the current line of the statement has ended.
Hello World!
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page |
12
As discussed earlier, the execution of the C# program starts from the Main method, hence the program should have
the Main method as its starting point.
Before we begin with an explanation ofthe C# components lets recall a fewpoints:
A program needs to have a reference to the .Net framework that has been used in the program.
For our current program to reference.Net,we use “using”. For Example “using System” in our above program.
After defining the references we declare a namespace. Syntax Example- namespace ConsoleApplication.
Then we declared a class using a class keyword. Syntax Example- class Program.
Inside the class, we have a Main method. This is the starting point of the execution.
Inside the Main method, we have defined “Console” which is a class in the .Net framework. The “WriteLine()” is a
method that is used to display a message in a console. Similar to “WriteLine()”, “ReadLine()” is also a method
that reads user input.
In the above program, we wrote a text in the console application and discussed various parts of the program. Here,
we will discuss various parts that were used in conjuncture with each other to make-up a valid syntax in detail. We
will discuss components, their definition and ways in which we have to declare them in the program.
Using Keyword
The using keyword is used to handle any object that is being used to implement the namespace. It is used to import a
namespace. As a namespace is a collection of classes and each class has its own functionality, so using the keyword
provides us with the facility to implement different functionality from the namespace that has been imported.
Namespace
The namespace is a collection of related classes and objects. It is designed to offer a way to keep a different set of
objects segregated from each other. This allows the programmers to declare one class inside a namespace and another
class inside another namespace without causing a conflict.
Let us say we are creating two namespaces namely “fruits” and “colors”. Both these namespaces can have a class
called “Orange” without creating any conflict with each other. A namespace is defined by writing your namespace
name preceded by keyword namespace.
For Example: namespace ConsoleApplication
Class
Defining a class is similar to defining a blueprint of a data type. Class does not actually define the data but they
arrange the data as a meaningful entity.
For Example,if we have a class called “Fruit”, and then we can specify different related objects inside the class such
as mango, apple, grapes, etc. Thus, the class fruit will contain different fruit objects with different properties such as
color, taste, smell, edible, etc. Each of these objects and property will be a part of the class.
Similar to the above example, the object-oriented programming language such as C# also defines different properties
inside i.e. fields, methods, conditions, events, etc. The objects inside the class contain a definition of the operations
that can be performed.
Objects are an instance of the class and the methods or the variables that are the members of the class. Thus, in short,
the class allows you to create your own custom objects by grouping other methods, events and object types.
A class is defined by putting the keyword “class” before the name of the class and a pair of curly braces should
define the class body.
Department of Information Technology Event-Driven Programming C#
ETHIOPIAN POLICEUNIVERSITY Page |
13
class Program
{
// class body
}
Access Modifiers
Access modifiers define the accessibility of an object and its components. All the C# components have their own
access levelthat can be controlled by defining the scope of accessibility for the member objects inside the class by
using access modifiers.
To define the accessibility level of the object we have to declare it by using one of the keywords provided by C#
language i.e. Public, Private, Protected and Internal. Access modifier is declared by using of the keywords mentioned
above either before the class or a method.
Departmentof InformationTechnology Event-DrivenProgrammingC#
ETHIOPIAN POLICEUNIVERSITY
These arguments are useful while calling a method with certain parameters. In the above example, we
have passed only one parameter that is “args” with the argument type as an array of string.
Constructor
Constructors are the special methods in a class that is automatically invoked whenever an instance of the
particular class is created. The main advantage of the constructor is that it can initialize the private fields
of a class. A class can have a number of constructors and it does not need to have any return type.
Only one static constructor is allowed inside a class and that cannot be parameterized. Constructors are
declared by using any access modifier and then by using the class name in which it is created.
For Example, a constructor definition will look something like this.
class Program
{
public class(){ //This is a constructor
}
}
Data types are used in almost all the programming languages. A data type tells the compiler what kind of
value a variable will hold. C# has several built-in data types such as String, Integer, Float, Boolean, etc.
Each data type has its own set of definitions for the values it can hold.
A data type is used to declare a variable by the preceding variable with the data type keyword. class
Program
{
static void Main(string[]args)
{
string stringValue = "Hello"; int integerValue = 10;float floatValue = 13.2f;
bool booleanValue = false;
}
}
Curly Braces
We saw the extensive usage of curly braces “{}” in our previous programs and declarations which is why
it is referred to as one of the most important parts of any program. It defines the beginning and the end of
any logical statement in a program block.
Departmentof InformationTechnology Event-DrivenProgrammingC#
ETHIOPIAN POLICEUNIVERSITY
The curly braces are not only restricted to the C# language, but they are also found in several other
languages like Java,C++, etc. You can clearly see our first “Hello World” code that they are used to wrap
multiple lines of code together. It clearly marks the starting and the endpoint of the logical statement for
the compiler.
Any logical entity like Namespace,Class,Method, Conditional statement, Loops, etc. should be enclosed
inside braces to clearly mark the start and end of the statement for the compiler.

More Related Content

Similar to C# handout.docx

DotNet Fundamentals
DotNet FundamentalsDotNet Fundamentals
DotNet FundamentalsMajdi SAIBI
 
Ten compelling reasons to learn .net framework
Ten compelling reasons to learn .net frameworkTen compelling reasons to learn .net framework
Ten compelling reasons to learn .net frameworkJanBask Training
 
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
The Importance of C Language in the Tech Industry.pdf
The Importance of C Language in the Tech Industry.pdfThe Importance of C Language in the Tech Industry.pdf
The Importance of C Language in the Tech Industry.pdfSaniyaSharma28
 
20210417-cppRelevancy-DataStructures.pptx
20210417-cppRelevancy-DataStructures.pptx20210417-cppRelevancy-DataStructures.pptx
20210417-cppRelevancy-DataStructures.pptxSuman Garai
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 
C# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdfC# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdfchristiemarie4
 
Chapter-1 C#.pptx
Chapter-1 C#.pptxChapter-1 C#.pptx
Chapter-1 C#.pptxfaarax4
 
C programming presentation for university
C programming presentation for universityC programming presentation for university
C programming presentation for universitySheikh Monirul Hasan
 
Why C is Called Structured Programming Language
Why C is Called Structured Programming LanguageWhy C is Called Structured Programming Language
Why C is Called Structured Programming LanguageSinbad Konick
 

Similar to C# handout.docx (20)

DotNet Fundamentals
DotNet FundamentalsDotNet Fundamentals
DotNet Fundamentals
 
Introduction to C# Programming
Introduction to C# ProgrammingIntroduction to C# Programming
Introduction to C# Programming
 
C#
C#C#
C#
 
C# Introduction brief
C# Introduction briefC# Introduction brief
C# Introduction brief
 
Characteristics of c#
Characteristics of c#Characteristics of c#
Characteristics of c#
 
C#
C#C#
C#
 
C#.ppt
C#.pptC#.ppt
C#.ppt
 
Intro1
Intro1Intro1
Intro1
 
C# chap 1
C# chap 1C# chap 1
C# chap 1
 
Ten compelling reasons to learn .net framework
Ten compelling reasons to learn .net frameworkTen compelling reasons to learn .net framework
Ten compelling reasons to learn .net framework
 
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
 
The Importance of C Language in the Tech Industry.pdf
The Importance of C Language in the Tech Industry.pdfThe Importance of C Language in the Tech Industry.pdf
The Importance of C Language in the Tech Industry.pdf
 
20210417-cppRelevancy-DataStructures.pptx
20210417-cppRelevancy-DataStructures.pptx20210417-cppRelevancy-DataStructures.pptx
20210417-cppRelevancy-DataStructures.pptx
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 
C-sharping.docx
C-sharping.docxC-sharping.docx
C-sharping.docx
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
C# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdfC# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdf
 
Chapter-1 C#.pptx
Chapter-1 C#.pptxChapter-1 C#.pptx
Chapter-1 C#.pptx
 
C programming presentation for university
C programming presentation for universityC programming presentation for university
C programming presentation for university
 
Why C is Called Structured Programming Language
Why C is Called Structured Programming LanguageWhy C is Called Structured Programming Language
Why C is Called Structured Programming Language
 

Recently uploaded

Hat in European paintings .ppsx
Hat    in    European    paintings .ppsxHat    in    European    paintings .ppsx
Hat in European paintings .ppsxguimera
 
indian folk dances and their significance
indian folk dances and their significanceindian folk dances and their significance
indian folk dances and their significanceksathish10
 
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理一比一原版NYU毕业证纽约大学毕业证成绩单如何办理
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理beduwt
 
Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)CristianMestre
 
acting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaaacting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaaangelicafronda7
 
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理beduwt
 
2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main storiesluforfor
 
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单zvaywau
 
Winning Shots from Siena International Photography Awards 2015
Winning Shots from Siena International Photography Awards 2015Winning Shots from Siena International Photography Awards 2015
Winning Shots from Siena International Photography Awards 2015rajeshkumar821445
 
Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)SuryaKalyan3
 
The Legacy of Breton In A New Age by Master Terrance Lindall
The Legacy of Breton In A New Age by Master Terrance LindallThe Legacy of Breton In A New Age by Master Terrance Lindall
The Legacy of Breton In A New Age by Master Terrance LindallBBaez1
 
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.docjosegrimaldo17
 
Sisters_Bond_storyboard.pdf_____________
Sisters_Bond_storyboard.pdf_____________Sisters_Bond_storyboard.pdf_____________
Sisters_Bond_storyboard.pdf_____________LauraBegliomini1
 
LPU infrastructure.pdf uniinfrastructure
LPU infrastructure.pdf uniinfrastructureLPU infrastructure.pdf uniinfrastructure
LPU infrastructure.pdf uniinfrastructureankitaagnihotri0801
 
一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单zvaywau
 
Tackling Poverty in Nigeria, by growing Art-based SMEs
Tackling Poverty in Nigeria, by growing Art-based SMEsTackling Poverty in Nigeria, by growing Art-based SMEs
Tackling Poverty in Nigeria, by growing Art-based SMEsikennaaghanya
 
Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)SuryaKalyan3
 
CLASS XII- HISTORY-THEME 4-Thinkers, Bes
CLASS XII- HISTORY-THEME 4-Thinkers, BesCLASS XII- HISTORY-THEME 4-Thinkers, Bes
CLASS XII- HISTORY-THEME 4-Thinkers, Besaditiyad2020
 
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...ikennaaghanya
 

Recently uploaded (20)

Hat in European paintings .ppsx
Hat    in    European    paintings .ppsxHat    in    European    paintings .ppsx
Hat in European paintings .ppsx
 
indian folk dances and their significance
indian folk dances and their significanceindian folk dances and their significance
indian folk dances and their significance
 
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理一比一原版NYU毕业证纽约大学毕业证成绩单如何办理
一比一原版NYU毕业证纽约大学毕业证成绩单如何办理
 
Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)Inter-Dimensional Girl Boards Segment (Act 3)
Inter-Dimensional Girl Boards Segment (Act 3)
 
acting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaaacting board rough title here lolaaaaaaa
acting board rough title here lolaaaaaaa
 
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理
一比一原版UC Berkeley毕业证加利福尼亚大学|伯克利分校毕业证成绩单如何办理
 
2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories2137ad - Characters that live in Merindol and are at the center of main stories
2137ad - Characters that live in Merindol and are at the center of main stories
 
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
一比一原版(GU毕业证)格里菲斯大学毕业证成绩单
 
Winning Shots from Siena International Photography Awards 2015
Winning Shots from Siena International Photography Awards 2015Winning Shots from Siena International Photography Awards 2015
Winning Shots from Siena International Photography Awards 2015
 
Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)Memory Rental Store - The Chase (Storyboard)
Memory Rental Store - The Chase (Storyboard)
 
The Legacy of Breton In A New Age by Master Terrance Lindall
The Legacy of Breton In A New Age by Master Terrance LindallThe Legacy of Breton In A New Age by Master Terrance Lindall
The Legacy of Breton In A New Age by Master Terrance Lindall
 
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc
2º CALIGRAFIAgggggggggggggggggggggggggggggggggggggg.doc
 
Sisters_Bond_storyboard.pdf_____________
Sisters_Bond_storyboard.pdf_____________Sisters_Bond_storyboard.pdf_____________
Sisters_Bond_storyboard.pdf_____________
 
LPU infrastructure.pdf uniinfrastructure
LPU infrastructure.pdf uniinfrastructureLPU infrastructure.pdf uniinfrastructure
LPU infrastructure.pdf uniinfrastructure
 
一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单一比一原版(DU毕业证)迪肯大学毕业证成绩单
一比一原版(DU毕业证)迪肯大学毕业证成绩单
 
European Cybersecurity Skills Framework Role Profiles.pdf
European Cybersecurity Skills Framework Role Profiles.pdfEuropean Cybersecurity Skills Framework Role Profiles.pdf
European Cybersecurity Skills Framework Role Profiles.pdf
 
Tackling Poverty in Nigeria, by growing Art-based SMEs
Tackling Poverty in Nigeria, by growing Art-based SMEsTackling Poverty in Nigeria, by growing Art-based SMEs
Tackling Poverty in Nigeria, by growing Art-based SMEs
 
Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)Memory Rental Store - The Ending(Storyboard)
Memory Rental Store - The Ending(Storyboard)
 
CLASS XII- HISTORY-THEME 4-Thinkers, Bes
CLASS XII- HISTORY-THEME 4-Thinkers, BesCLASS XII- HISTORY-THEME 4-Thinkers, Bes
CLASS XII- HISTORY-THEME 4-Thinkers, Bes
 
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...
Digital/Computer Paintings as a Modern- day Igbo Artists’ vehicle for creatin...
 

C# handout.docx

  • 1. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 2 What is the C# Programming Language? C# (pronouncedas“C Sharp”) is a simple,modern,object-oriented,andtype-safeprogramminglanguage.C# language hasitsroots inC languagessuchasC, C++, andit ismostlysimilartoJavaprogramming. C# is a stronglytypedobject-orientedprogramminglanguage.C#isopensource,simple,modern,flexible,and versatile.Inthisarticle,letuslearnwhatC#is,what C# can do,and how C# is differentfromC++ andother programminglanguages. C# is a programminglanguage of .NetFramework. C# is pronouncedas"C-Sharp".Itis an object-orientedprogramminglanguage providedbyMicrosoftthatruns on .NetFramework. C# is a programminglanguage developedandlaunchedbyMicrosoftin2001. C# is a simple,modern,andobject- orientedlanguage thatprovidesmoderndaydevelopersflexibilityandfeaturestobuildsoftwarethatwill notonly worktoday butwill be applicable foryearsinthe future. A programminglanguage oncomputerscience isalanguage thatis usedto write software programs. C# Programminglanguage will allowdeveloperstobuildavarietyof secure androbustapplicationssuchas windowsapplications,webapplications,database applications,etc.,whichwill runon .NETFramework. C# programminglanguage hasbeenbuilton .NETFramework torun the C# applications.We are requiredtoinstall a .NET Framework componentonourmachines. .NET Framework isa developmentplatformforbuildingappsforwindows,web,azure,etc.,byusingprogramming languagessuchas C#, F#, and Visual Basic.Itconsistsof twomajor components,suchas CommonLanguage Runtime (CLR),anexecutionenginethathandlesrunningapps,and .NETFrameworkClassLibrary,whichprovidesa libraryof testedandreusable code thatdeveloperscanuse itintheirapplications. C# Features C# is object oriented programming language. It provides many features that are given below. Simple Modernprogramminglanguage Objectoriented Type safe
  • 2. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 3 Interoperability Scalable andUpdateable Componentoriented Structuredprogramminglanguage Rich Library Fast speed Modernand easy Fast and opensource Crossplatform Safe Versatile Simple C# is a simple language in the sense that it provides structured approach (to break the problem into parts), rich set of library functions, data types etc. ModernProgramming Language C# programming is based upon the current trend and it is very powerful and simple for building scalable, interoperable and robust applications. ObjectOriented C# is object oriented programming language. OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grow. Type Safe C# type safe code can only access the memory location that it has permission to execute. Therefore,it improves a security of the program. Interoperability Interoperability process enables the C# programs to do almost anything that a native C++ application can do. Scalable and Updateable C# is automatic scalable and updateable programming language. For updating our application, we delete the old files and update them with new ones. ComponentOriented C# is component oriented programming language. It is the predominant software development methodology used to develop more robust and highly scalable applications. Structured Programming Language
  • 3. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 4 C# is a structured programming language in the sense that we can break the program into parts using functions. Therefore,it is easy to understand and modify. Rich Library C# provides many inbuilt functions that makes the development fast. Fast Speed The compilation and execution time of C# language is fast. C# is modern and easy C# is a simple,modern,andanobject-orientedprogramminglanguage.The purpose of C#was to developa programminglanguage thatisnot onlyeasytolearnbut alsosupportsmoderndayfunctionalityforall kindof software development. If you lookat the historyof programminglanguagesandtheirfeatures,eachprogramminglanguage wasdesigned for a specificpurpose tosolve aspecificneedatthat time. C# language howeverwas designedtokeepbusinessandenterprisesneedsinmind.C#language wasdesignedfor businessestobuildall kindsof software byusingone single programminglanguage. C# providesfunctionalitytosupportmoderndaysoftware development.C#supportsWeb,Mobile,andapp developmentneeds.Some of the modern-dayprogramminglanguagefeaturesC#supportsare generics,vartypes, auto initializationof typesandcollections,lambdaexpressions,dynamicprogramming,asynchronousprogramming, tuples,pattern matching,advanceddebuggingandexceptionhandling,andmore. C# language syntaxesare influencedfromC++,Java, Pascal and few otherlanguagesthatare easyto adopt.C# also avoidscomplexityandunstructuredlanguagefeatures. C# is fast and opensource C# is opensource underthe .NET Foundation,whichisgovernedandrunindependentlyof Microsoft.C#language specifications,compilers,andrelatedtoolsare opensource projectsonGitHub.While C#language feature designis ledbyMicrosoft, the opensource communityisveryactive inthe language developmentandimprovements. C# is fastcompare to several otherhigh-level programminglanguages.C#8 has manyperformance improvements. C# is cross platform
  • 4. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 5 C# is cross platformprogramming language.Youcan build.NETapplicationsthatcanbe deployedonWindows, Linux,andMac platforms.C#apps can alsobe deployedincloudandcontainers. C# is safe and efficient C# is a type safe language.C#doesnot allowtype conversionsthatmay leadto data lossor otherproblems.C# allowsdeveloperstowrite safe code.C#alsofocusesonwritingefficientcode. Here is a listof some of the keyconceptsinC# that helpswrite safe andefficientcode. Unsafe type castingisnot allowed. Nullable andnon-null abletypesare supportedinC#. Declare a readonlystructto expressthata type isimmutable andenablesthe compilertosave copieswhenusingin parameters. Use a ref readonlyreturnwhenthe returnvalue isa structlarger thanIntPtr.Size andthe storage lifetime isgreater than the methodreturningthe value. Whenthe size of a readonlystructisbiggerthanIntPtr.Size,youshouldpassitasan in parameterforperformance reasons. Neverpassa struct as an inparameterunlessitis declaredwiththe readonlymodifierbecauseitmaynegatively affectperformance andcouldleadtoan obscure behavior. Use a ref struct, or a readonlyref structsuch as Span<T> or ReadOnlySpan<T>towork withmemoryasa sequence of bytes. C# is versatile C# is a Swissarmyknife.While mostprogramminglanguagesweredesignedforaspecificpurpose,C#wasdesigned to do C#. We can use C# to buildtoday’smodernsoftware applications.C#can be usedto developall kindof applicationsincludingWindows clientapps,componentsandlibraries,servicesandAPIs,Webapplications,Mobile apps,cloudapplications,andvideogames. Here is a listof typesof applicationsC#can build: Windowsclientapplications Windowslibrariesandcomponents Windowsservices Webapplications WebservicesandWebAPI
  • 5. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 6 Native iOSandAndroidmobile apps Backendservices Azure cloudapplicationsandservices Backenddatabase usingML/Data tools InteroperabilitysoftwaresuchasOffice,SharePoint,SQLServerandsoon. Artificial IntelligenceandMachine learning Blockchainsand distributedledgertechnologyincludingcryptocurrency Internetof Things(IoT) devices Gamingconsolesandgamingsystems Videogames C# History History of C# language isinterestingtoknow.Here we are goingto discussbrief historyof C#language.C#is pronouncedas "C-Sharp".Itis an object-orientedprogramminglanguage providedby Microsoftthatruns on .Net Framework. AndersHejlsbergisknownas the founderof C# language. It isbasedon C++ and Java, but ithas many additional extensionsusedtoperformcomponentoriented programmingapproach. C# has evolvedmuchsince theirfirstrelease inthe year 2002. It was introducedwith .NETFramework 1.0 and the currentversionof C# is 10.0. C# Version History C# was first introduced with .NET Framework 1.0 in the year 2002 and evolved much since then. The following table lists important features introduced in each version of C#:
  • 6. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 7 Version .NET Framework Visual Studio Important Features C# 1.0 .NET Framework 1.0/1.1 Visual Studio .NET 2002 Basic features C# 2.0 .NET Framework 2.0 Visual Studio 2005 Generics Partial types Anonymous methods Iterators Nullable types Private setters (properties) Method group conversions (delegates) Covariance and Contra-variance Static classes C# 3.0 .NET Framework 3.03.5 Visual Studio 2008 Implicitly typed local variables Object and collection initializers Auto-Implemented properties Anonymous types Extension methods Query expressions Lambda expressions Expression trees Partial Methods C# 4.0 .NET Framework 4.0 Visual Studio 2010 Dynamic binding (late binding) Named and optional arguments Generic co- and contra variance Embedded interop types C# 5.0 .NET Framework 4.5 Visual Studio 2012/2013 A sync features Caller information
  • 7. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 8 Version .NET Framework Visual Studio Important Features C# 6.0 .NET Framework 4.6 Visual Studio 2013/2015 Expression Bodied Methods Auto-property initializer name of Expression Primary constructor Await in catch block Exception Filter String Interpolation C# 7.0 .NET Core 2.0 Visual Studio 2017 out variables Tuples Discards Pattern Matching Local functions Generalized a sync return types C# 8.0 .NET Core 3.0 Visual Studio 2019 Read-only members Default interface methods Using declarations Static local functions Disposable ref structs Nullable reference types More C# 9.0 .NET 5.0 Visual Studio 2019 Records Init-only properties Top-level statements Init accessors and readonly fields With-expressions Value-based equality more
  • 8. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 9 Version .NET Framework Visual Studio Important Features C# 10.0 .NET 6.0 Visual Studio 2022 Record structs Global using directives File-scoped namespace declaration Extended Proptery Patterns Null Parameter Checking Constant interpolated strings more C# Example:HelloWorld In C# programming language, a simple "hello world" program can be written by multiple ways. Let us see the top four ways to create a simple C# example: Simple Example Using System Using public modifier Using namespace C# Simple Example class Program { static void Main(string[] args) { System.Console.WriteLine("Hello World!"); } } Output: Hello World! Description class: is a keyword, which is used to define class. Program: is the class name. A class is a blueprint or template from which objects are created. It can have data members and methods. Here,it has only Main method.’ static: is a keyword that means object is not required to access static members. So it saves memory. void: is the return type of the method. It does't return any value. In such case,return statement is not required.
  • 9. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 10 Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main() method is invoked first before any other method. It represents start up of the program. string[] args: is used for command line arguments in C#. While running the C# program, we can pass values. These values are known as arguments which we can use in the program. System.Console.WriteLine("Hello World!"): Here,System is the namespace. Console is the class defined in System namespace. The WriteLine() is the static method of Console class which is used to write the text on the console. C# Example: Using System If we write using Systembefore the class,it means we don't need to specify System namespace for accessing any class of this namespace. Here,we are using Console class without specifying System.Console. using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } Output: Hello World! C# Example: Using public modifier We can also specify public modifier before class and Main() method. Now, it can be accessed from outside the class also. using System; public class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } Output:
  • 10. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 11 Hello World! C# Example: Using namespace We can create classes inside the namespace. It is used to group related classes. It is used to categorize classes so that it can be easy to maintain. using System; namespace ConsoleApplication1 { public class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } Output: C# Program Structure A typical C# Program consists ofseveral different parts as shown below: Namespace Class The main method Methods inside the class Class definition or class attributes Statements Comments Few things need to be kept in mind while writing a C# program. C# programs are case sensitive, which means “string” is different from “String”. All the statements written in the program must be concluded with a semicolon i.e. “;”. A semicolon tells the program that the current line of the statement has ended. Hello World!
  • 11. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 12 As discussed earlier, the execution of the C# program starts from the Main method, hence the program should have the Main method as its starting point. Before we begin with an explanation ofthe C# components lets recall a fewpoints: A program needs to have a reference to the .Net framework that has been used in the program. For our current program to reference.Net,we use “using”. For Example “using System” in our above program. After defining the references we declare a namespace. Syntax Example- namespace ConsoleApplication. Then we declared a class using a class keyword. Syntax Example- class Program. Inside the class, we have a Main method. This is the starting point of the execution. Inside the Main method, we have defined “Console” which is a class in the .Net framework. The “WriteLine()” is a method that is used to display a message in a console. Similar to “WriteLine()”, “ReadLine()” is also a method that reads user input. In the above program, we wrote a text in the console application and discussed various parts of the program. Here, we will discuss various parts that were used in conjuncture with each other to make-up a valid syntax in detail. We will discuss components, their definition and ways in which we have to declare them in the program. Using Keyword The using keyword is used to handle any object that is being used to implement the namespace. It is used to import a namespace. As a namespace is a collection of classes and each class has its own functionality, so using the keyword provides us with the facility to implement different functionality from the namespace that has been imported. Namespace The namespace is a collection of related classes and objects. It is designed to offer a way to keep a different set of objects segregated from each other. This allows the programmers to declare one class inside a namespace and another class inside another namespace without causing a conflict. Let us say we are creating two namespaces namely “fruits” and “colors”. Both these namespaces can have a class called “Orange” without creating any conflict with each other. A namespace is defined by writing your namespace name preceded by keyword namespace. For Example: namespace ConsoleApplication Class Defining a class is similar to defining a blueprint of a data type. Class does not actually define the data but they arrange the data as a meaningful entity. For Example,if we have a class called “Fruit”, and then we can specify different related objects inside the class such as mango, apple, grapes, etc. Thus, the class fruit will contain different fruit objects with different properties such as color, taste, smell, edible, etc. Each of these objects and property will be a part of the class. Similar to the above example, the object-oriented programming language such as C# also defines different properties inside i.e. fields, methods, conditions, events, etc. The objects inside the class contain a definition of the operations that can be performed. Objects are an instance of the class and the methods or the variables that are the members of the class. Thus, in short, the class allows you to create your own custom objects by grouping other methods, events and object types. A class is defined by putting the keyword “class” before the name of the class and a pair of curly braces should define the class body.
  • 12. Department of Information Technology Event-Driven Programming C# ETHIOPIAN POLICEUNIVERSITY Page | 13 class Program { // class body } Access Modifiers Access modifiers define the accessibility of an object and its components. All the C# components have their own access levelthat can be controlled by defining the scope of accessibility for the member objects inside the class by using access modifiers. To define the accessibility level of the object we have to declare it by using one of the keywords provided by C# language i.e. Public, Private, Protected and Internal. Access modifier is declared by using of the keywords mentioned above either before the class or a method.
  • 13. Departmentof InformationTechnology Event-DrivenProgrammingC# ETHIOPIAN POLICEUNIVERSITY These arguments are useful while calling a method with certain parameters. In the above example, we have passed only one parameter that is “args” with the argument type as an array of string. Constructor Constructors are the special methods in a class that is automatically invoked whenever an instance of the particular class is created. The main advantage of the constructor is that it can initialize the private fields of a class. A class can have a number of constructors and it does not need to have any return type. Only one static constructor is allowed inside a class and that cannot be parameterized. Constructors are declared by using any access modifier and then by using the class name in which it is created. For Example, a constructor definition will look something like this. class Program { public class(){ //This is a constructor } } Data types are used in almost all the programming languages. A data type tells the compiler what kind of value a variable will hold. C# has several built-in data types such as String, Integer, Float, Boolean, etc. Each data type has its own set of definitions for the values it can hold. A data type is used to declare a variable by the preceding variable with the data type keyword. class Program { static void Main(string[]args) { string stringValue = "Hello"; int integerValue = 10;float floatValue = 13.2f; bool booleanValue = false; } } Curly Braces We saw the extensive usage of curly braces “{}” in our previous programs and declarations which is why it is referred to as one of the most important parts of any program. It defines the beginning and the end of any logical statement in a program block.
  • 14. Departmentof InformationTechnology Event-DrivenProgrammingC# ETHIOPIAN POLICEUNIVERSITY The curly braces are not only restricted to the C# language, but they are also found in several other languages like Java,C++, etc. You can clearly see our first “Hello World” code that they are used to wrap multiple lines of code together. It clearly marks the starting and the endpoint of the logical statement for the compiler. Any logical entity like Namespace,Class,Method, Conditional statement, Loops, etc. should be enclosed inside braces to clearly mark the start and end of the statement for the compiler.