0% found this document useful (0 votes)
7 views24 pages

C# Programming Language Overview

Uploaded by

2401101776
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)
7 views24 pages

C# Programming Language Overview

Uploaded by

2401101776
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

The C# Programming Language

Introduction to C#

FT
NOEL B. LACPAO
Department of Mathematics

A
BUKIDNON STATE UNIVERSITY
Malaybalay City

R
D
2nd Semester S.Y. 2024-2025

N.B. Lacpao INTRODUCTION TO C#


WHAT IS C#?

C# is a modern object-oriented, general-purpose programming


language, created and developed by Microsoft together with the
.NET platform.
SOFTWARES DEVELOPED WITH C#

FT
1 office applications,
2 web applications,

A
R
3 websites, D
4 desktop applications,
5 mobile applications,
6 game, and
7 many others.

N.B. Lacpao INTRODUCTION TO C#


C# is a high-level language that is similar to Java and C++
and, to some extent, languages like Delphi, [Link] and C. All
C# programs are object-oriented. They consist of a set of

FT
definitions in classes that contain methods and the methods
contain the program logic – the instructions which the

A
computer executes.
R
D

N.B. Lacpao INTRODUCTION TO C#


The Microsoft .NET Framework
The C# language is not distributed as a standalone product –
it is a part of the Microsoft .NET Framework platform .NET
Framework generally consists of an environment for the
development and execution of programs, written in C# or some
other language, compatible with .NET. It consists of:
1 the .NET programming languages;

FT
2 an environment for the execution of managed code (CLR),
which executes C# programs in a controlled manner;

A
R
3 a set of development tools, such as the csc compiler, which
D
turns C# programs into intermediate code (called MSIL)
that the CLR can understand;
4 a set of standard libraries, like [Link], which allow
access to databases (such as MS SQL Server or MySQL)
and WCF which connects applications through standard
communication frameworks and protocols like HTTP,
REST, JSON, SOAP and TCP sockets.
N.B. Lacpao INTRODUCTION TO C#
INTRODUCTION TO PROGRAMMING

The Essence of Programming


The essence of programming is to control the work of the
computer on all levels. This is done with the help of "orders"

FT
and "commands" from the programmer, also known as
programming instructions. To "program" means to organize the

A
work of the computer through sequences of instructions. These
R
commands (instructions) are given in written form and are
D
implicitly followed by the computer (respectively by the
operating system, the CPU and the peripheral devices).

N.B. Lacpao INTRODUCTION TO C#


A sequence of steps to achieve, complete some work or obtain
some result is called an algorithm. Programmers are the
people who create these instructions, which control computers.
These instructions are called programs. Numerous programs
exist, and they are created using different kinds of
programming languages. Each language is oriented towards

FT
controlling the computer on a different level. There are

A
languages oriented towards the machine level (the lowest) –

R
Assembler for example. Others are most useful at the system
D
level (interacting with the operating system), like C. There are
also high level languages used to create application programs.
Such languages include C#, Java, C++, PHP, Visual Basic,
Python, Ruby, Pearl, Javascripts and others.

N.B. Lacpao INTRODUCTION TO C#


Stages in Software Development
1 Gathering the requirements for the product and creating a
task;
2 Planning and preparing the architecture and design;

FT
3 Implementation (includes the writing of program code);

A
4 Product trials (testing);
5
R
Deployment and exploitation; and
D
6 Support.

N.B. Lacpao INTRODUCTION TO C#


Our First C# program
class HelloCSharp
{
static void Main(string[] args)

FT
{

A
[Link]("Hello C#!");

R
} D
}

N.B. Lacpao INTRODUCTION TO C#


Our first program consists of three logical parts:
Definition of a class HelloCSharp;

FT
Definition of a method Main();
Contents of the method Main().

A
R
D

N.B. Lacpao INTRODUCTION TO C#


Defining a Class

On the first line of our program we define a class called

FT
HelloCSharp. The simplest definition of a class consists of the
keyword class, followed by its name. In our case the name of

A
the class is HelloCSharp. The content of the class is located in

R
a block of program lines, surrounded by curly brackets: {}.
D

N.B. Lacpao INTRODUCTION TO C#


Defining the Main () Method

On the third line we define a method with the name Main(),


which is the starting point for our program. Every program
written in C# starts from a Main() method with the following
title (signature):

static void Main(string[] args)

FT
The method must be declared as shown above, it must be static

A
and void, it must have a name Main and as a list of parameters

R
it must have only one parameter of type array of string. In our
D
example the parameter is called args but that is not mandatory.
This parameter is not used in most cases so it can be omitted
(it is optional). In that case the entry point of the program can
be simplified and will look like this:

static void Main()

N.B. Lacpao INTRODUCTION TO C#


Contents of the Main() Method

The content of every method is found after its signature,


surrounded by opening and closing curly brackets. On the next
line of our sample program we use the system object

FT
[Link] and its method WriteLine() to print a message

A
on the default output (the console), in this case "Hello, C#!".

R
In the Main() method we can write a random sequence of
D
expressions and they will be executed in the order we assigned
to them.

N.B. Lacpao INTRODUCTION TO C#


Notes:
1 C# distinguishes between uppercase and lowercase!

2 The program code must be correctly formatted

Main Formating Rules


1 Methods are indented inside the definition of the class
(move to the right by one or more [Tab] characters);
2 Method contents are indented inside the definition of the

FT
method;

A
3 The opening curly bracket must be on its own line and

R
placed exactly under the method or class it refers to;
D
4 The closing curly bracket must be on its own line, placed
exactly vertically under the respective opening bracket
(with the same indentation);
5 All class names must start with a capital letter;
6 Variable names must begin with a lower-case letter;
7 Method names must start with a capital letter;
N.B. Lacpao INTRODUCTION TO C#
File Names Correspond to Class Names

Every C# program consists of one or several class definitions.


It is accepted that each class is defined in a separate file with a

FT
name corresponding to the class name and a .cs extension.
When these requirements are not met, the program will still

A
work but navigating the code will be difficult. In our example,

R
the class is named HelloCSharp, and as a result we must save
D
its source code in a file called [Link].

N.B. Lacpao INTRODUCTION TO C#


KEYWORDS

C# uses the following keywords to build its programming


constructs: abstract, as, base, bool, break, byte, case, catch,
char, checked class, const, continue, decimal, default, delegate,
do, double, else, enum, event, explicit, extern, false, finally,
fixed, float, for, foreach, goto, if, implicit, in, int, interface,

FT
internal, is, lock, long, namespace, new, null, object, operator,
out, override, params, private, protected, public, readonly,

A
record, ref, return, sbyte, sealed, short, sizeof, stackalloc, static,
R
string, struct, switch, this, throw, true, try, typeof, uint, ulong,
D
unchecked, unsafe, ushort, using, virtual, void, volatile, while
The main program elements in C# (which are defined and used
with the help of keywords) are classes, methods, operators,
expressions, conditional statements, loops, data types,
exceptions and few others.

N.B. Lacpao INTRODUCTION TO C#


Independence from the Environment and the
Programming Language

1 One of the advantages of .NET is that programmers using


different .NET languages can easily exchange their code.

FT
2 A big advantage of the .NET technology is the ability to

A
run code, which is written and compiled only once, on

R
different operating systems and hardware devices. We can
D
compile a C# program in a Windows environment and
then execute it under Windows, Windows Mobile,
Windows RT or Linux.

N.B. Lacpao INTRODUCTION TO C#


Common Language Runtime (CLR) – the Heart
of .NET

In the very center of the .NET platform beats its heart – the
Common Language Runtime (CLR) – the environment that
controls the execution of the managed code (MSIL code). It

FT
ensures the execution of .NET programs on different hardware
platforms and operating systems.

A
CLR is an abstract computing machine (virtual machine).
R
Similarly to physical computers, it supports a set of
D
instructions, registries, memory access and input-output
operations. CLR ensures a controlled execution of the .NET
programs using the full capabilities of the processor and the
operating system.

N.B. Lacpao INTRODUCTION TO C#


The .NET Platform
The .NET platform contains the C# language, CLR and many
auxiliary instruments and libraries ready for use. There are a
few versions of .NET according to the targeted user group:

FT
1 .NET Framework

A
2 .NET Compact Framework (CF)
3 Silverlight
R
D
4 .NET for Windows Store apps

N.B. Lacpao INTRODUCTION TO C#


Application Programming Interface (API)
Each .NET library or technology is utilized by creating objects

FT
and calling their methods. The set of public classes and
methods in the programming libraries is called Application

A
Programming Interface or just API.

R
D

N.B. Lacpao INTRODUCTION TO C#


What We Need to Program in C#?

In order to program in C#, we need two basic things –

FT
1 an installed .NET Framework and

A
2 a text editor

R
D

N.B. Lacpao INTRODUCTION TO C#


Visual Studio IDE

Visual Studio is a powerful integrated environment (IDE) for


developing software applications for Windows and the .NET
Framework platform. Visual Studio (VS) supports different
programming languages (for example C#, [Link] and C++)

FT
and different software development technologies (Win32, COM,
[Link], [Link] Entity Framework, Windows Forms,

A
WPF, Silverlight, Windows Store apps and many more

R
Windows and .NET technologies). It offers a powerful
D
integrated environment for writing code, compiling, executing,
debugging and testing applications, designing user interface
(forms, dialogs, web pages, visual controls and others), data and
class modeling, running tests and hundreds of other functions.

N.B. Lacpao INTRODUCTION TO C#


Compilation and Execution of C# Programs
The time has come to compile and execute the simple example
program written in C# we already discussed. To accomplish
that, we need to do the following:

FT
1 Create a file named [Link];
2 Write the sample program in the file;

A
3 Compile [Link] to an executable file
R
[Link] using the console-based C# compiler
D
([Link]);
4 Execute the [Link] file.

N.B. Lacpao INTRODUCTION TO C#


Exercise 1
This group activity consists of five members. Each group should run
the Visual Studio program. You may use ChatGPT 3.0 to generate
the necessary code while ensuring that the program meets the
minimum system requirements. Each group will then have five
minutes to present and explain their output.
1 Modify the sample program to print a different greeting, for
example "Good Day!".

FT
2 Write a console application that prints your first and last name

A
on the console.
3

R
Write a program that prints the following numbers on the
D
console 1, 101, 1001, each on a new line.
4 Write a program that prints on the console the current date and
time.
5 Write a program that prints the square root of 12345.
6 Write a program that prints the first 100 members of the
sequence 2, -3, 4, -5, 6, -7, 8.
N.B. Lacpao INTRODUCTION TO C#
Assignment
1 Find out how to use the [Link]() method.
2 Use the [Link]() method.

FT
3 Find out what features are offered by the
[Link] class.

A
R
4 Find out what features are offered by the [Link]
D
class.

N.B. Lacpao INTRODUCTION TO C#

You might also like