You are on page 1of 28

Overview of C# programming language

Student: Xəlilov Kamran


Teacher: Sevinc Kerimova
Subject: Fundamentals of programming
Faculty: ITIF
Group: 604.22E
Table of content

 What exactly is C#
 What is the .NET framework
 Why C#
 Application fields of C# programming language
 C# program structure
 Data types and variables in C#
 Object-oriented programming in C#
 C# libraries and frameworks
 Future of C#
What exactly is C#?
Computers only understand binary: 1’s and 0’s. All of the information they keep track of is ultimately
nothing more than a glorified pile of bits. All of the things they do boil down to instructions, written with
1’s and 0’s.
But humans are notoriously bad at doing anything with a random pile of billions of 1’s and 0’s. So rather
than doing that, we created programming languages, which are based on human languages (usually English)
and structured in a way that allows you to give instructions to the computer. These instructions are called
source code and are made up of simple text files.
When the time is right, your source code will be handed off to a special program called a compiler, which
can take it and turn it into the binary 1’s and 0’s that the computer understands, typically in the form of
a .exe file. In this sense, you can think of the compiler as a translator from your source code to the binary
machine instructions that the computer knows.

C# is one of the most mainstream of the entirety of the programming languages accessible. There are in a
real sense thousands, perhaps a huge number of these languages, and each one is intended for its own
motivations. C# is a basic broadly useful programming language, which means you can utilize it to create
basically anything, including work area applications, worker side code for sites, and even computer games.
C# is an object-oriented, modern programming language that was created by Microsoft. It runs on
the .NET Framework. C# is very close to C/C++ and Java programming languages. It was developed by
Anders Hejlsberg and his team within the .NET initiative that approved by the European Computer
Manufacturers Association (ECMA) and International Standards Organization (ISO).The first version of
C# was released in 2002 and the latest version is 11 released in November 2022.

C# gives an excellent balance between convenience and force. Different languages give less force and are
simpler to utilize (like Java) and others that give more force, surrendering a portion of its simplicity (like
C++). Because of the balance it strikes, it is the perfect language for almost all that you will need to do, so
it’s an incredible language to realize, regardless of whether it’s your first or your 10th.
What is the .NET Framework
C# relies heavily on something called the .NET Framework. The .NET Framework is a large and powerful
platform.
The .NET Framework primarily consists of two parts. The first part is the Common Language Runtime,
often abbreviated as the CLR. The CLR is a virtual machine — a special type of software program that
functions as though it is an actual computer. C# code is actually executed by the CLR, instead of being run
by the actual physical computer. There are, of course, benefits and drawbacks to this kind of a setup, but it
turns out to be a good idea for everything except low-level stuff, like an operating system or a device
driver. C# is a bad choice for things like that. (Try C or C++ instead.)

I should also point out that while running your code through a virtual machine may make it a bit slower, in
most cases, this isn’t enough to matter. In some situations, it could actually end up being faster. In other
cases, you can call outside code that not running on a virtual machine. The bottom line is, don’t stress too
much about if the CLR will be fast enough.
Because C# code runs on the .NET Framework, the process your code goes through before executing is a
little more complex than what I described earlier.
Rather than just a single step to compile your source code to binary executable code, it goes through
two steps. Here’s what happens:

 The C# compiler turns your source code into Common Intermediate Language (CIL or IL for short).
 This IL code is packaged into an .exe file or .dll file, which can be shared with others.
 When it is time to run your program, the IL code in your .exe or .dll will be handed off to the CLR to
run it.
 As your program is running, the CLR will look at the IL code and compile it into binary executable
code that the computer it is running on understands. For each block of code you have, this will only
happen once each time you run your program — the first time it needs it. This is called Just-in-Time
compiling, or JIT compiling.
Why C#?
C# has many other reasons for being popular and in demand. Few of the reasons are mentioned below:
1.Easy to Start: C# is a high-level language so it is closer to other popular programming languages
like C, C++, and Java and thus becomes easy to learn for anyone.
2.Widely used for developing Desktop and Web Application: C# is widely used for developing
web applications and Desktop applications. It is one of the most popular languages that is used in
professional desktop. If anyone wants to create Microsoft apps, C# is their first choice.
3.Community: The larger the community the better it is as new tools and software will be developing
to make it better. C# has a large community so the developments are done to make it exist in the system
and not become extinct.
4.Game Development: C# is widely used in game development and will continue to dominate. C#
integrates with Microsoft and thus has a large target audience. The C# features such as Automatic
Garbage Collection, interfaces, object-oriented, etc. make C# a popular game developing language.
Application fields of C# programming language

1. Console applications

2. Web applications

3. Windows services

4. Video games

5. Backend Services

6. Web API
The structure of a C# program
The structure of a C# program consists of various components that
work together to define the behavior and logic of the program.
Here are the main components of a C# program:

1.Namespaces: A namespace is a container that groups related


classes and other types. Namespaces help to organize the code and
prevent naming conflicts.
2.Classes: A class is a template or blueprint for creating objects
that have similar properties and behavior. Classes are the basic
building blocks of C# programs, and all C# code is written within
a class.
3.Methods: A method is a set of instructions that performs a
specific task. Methods can be called from other parts of the
program to perform a specific action.
4.Statements: Statements are individual instructions that make up
a method or block of code. C# statements are terminated with a
semicolon (;).
5.Comments: Comments are used to document the code and explain what the code does. Comments are
ignored by the compiler and do not affect the execution of the program.
6.Directives: Directives are special instructions that are processed by the compiler to control how the
program is compiled. For example, the using directive is used to include namespaces in the code.
7.Entry point: The entry point is the main method that is executed when the program starts running. The
main method must be declared with a specific signature in order to serve as the entry point.

C# programs can be written using a variety of tools and environments, including Visual Studio, Visual
Studio Code, and .NET Core CLI. When the code is written and saved, it is typically compiled into an
executable file that can be run on a computer or device.
using System;

namespace ExampleNamespace // declaration of namespace


{
class Program
{
static void Main(string[] args) // entery point
{
Console.WriteLine("Hello, World!"); // Statement 1
int x = 5; // Statement 2
int y = 10; // Statement 3
int sum = Add(x, y); // Statement 4
Console.WriteLine("The sum of {0} and {1} is {2}", x, y, sum); // Statement 5
}

static int Add(int a, int b) // Method definition


{
return a + b;
}
}
}
Simple Types Classes, Members and Methods

byte, sbyte int, uint (32bit), • Everything is encapsulated in a class


Integer types (8bit), short, long, ulong
• Can have:
ushort (16bit) (64bit)
- Member data
double
- Member methods
Floating Point float (precision
(precision of
Types of 7 digits)
15–16 digits)

Exact decimal (28


significant
Numeric Type digits)

string (rich
Character char (single functionality,
Types character) by-reference
type)

bool (distinct
type, not
Boolean Type interchangeable
with int)
Data types and variables in C#
In C#, a data type is a classification of data that specifies the type of values that can be stored in a variable
or passed as an argument to a method. C# supports various data types that can be categorized as follows:
1.Value types: Value types store the actual value of the data. They are stored on the stack and include
primitive data types such as int, float, double, char, bool, and others.
2.Reference types: Reference types store the address of the value in memory, rather than the actual value.
They are stored on the heap and include classes, arrays, delegates, interfaces, and strings.
3.Pointer types: Pointer types store the memory address of another type. They are not commonly used in
C#.
C# also supports various modifiers that can be applied to data types, such as signed and unsigned, const,
readonly, and volatile.
Variables in C# are used to store values of different data types. Before using a variable, it must be declared
with a specific data type. The syntax for declaring a variable in C# is:

<data-type> <variable-name> = <value>


For example, to declare an integer variable called "myNumber" with the value of 10, the syntax would be:

int myNumber = 10;

Variables can also be initialized without an initial value, in which case they are assigned a default value
based on their data type.
In addition to declaring variables with a specific value, C# also provides a feature called "type inference"
which allows the compiler to automatically infer the data type of a variable based on the assigned value.
The syntax for using type inference is:

var <variable-name> = <value>;

For example, to declare a variable called "myString" with the value of "Hello, World!", the syntax
would be:

var myString = "Hello, World!";


Value Data Types
- int (numbers)
- long (really bug numbers)
- bool (true or false)
- char (Unicode characters)
- float (7-digit floating point numbers)
- string (multiple characters together)
using System;
Example for loops
class Program using System;
{
static void Main(string[] args) class Program
{ {
static void Main(string[] args)
// Value types
{
int i = 42; // Integer value type int num = 5; // calculate the factorial of 5
char c = 'a'; // Character value type int factorial = 1;
double d = 3.14; // Double-precision floating point value type
// using a for loop to calculate the factorial
Console.WriteLine("Value types:"); for (int i = 1; i <= num; i++)
{
Console.WriteLine($"i = {i}");
factorial *= i;
Console.WriteLine($"c = {c}"); }
Console.WriteLine($"d = {d}");
Console.WriteLine("Factorial of {0} is {1}", num, factorial);
// Reference types
string s = "Hello, world!"; // String reference type // using a while loop to count down from 10 to 1
object o = s; // Object reference type int count = 10;
while (count >= 1)
{
Console.WriteLine("\nReference types:"); Console.WriteLine(count);
Console.WriteLine($"s = {s}"); count--;
Console.WriteLine($"o = {o}"); }

// Pointer types // using a do-while loop to prompt the user to enter a number
between 1 and 10
int* ptr; // Declare a pointer to an integer int userInput;
int num = 10; // Declare an integer variable do
ptr = &num; // Assign the address of num to ptr {
Console.WriteLine("Please enter a number between 1 and 10:");
Console.WriteLine("\nPointer types:"); userInput = int.Parse(Console.ReadLine());
Console.WriteLine($"num = {num}"); } while (userInput < 1 || userInput > 10);
Console.WriteLine($"*ptr = {(*ptr)}"); Console.WriteLine("You entered: {0}", userInput);
} }
} }
Object-oriented programming in C#
Object-oriented programming (OOP) is a programming paradigm that focuses on modeling real-world
objects as software objects. C# is an object-oriented programming language that fully supports OOP
concepts such as encapsulation, inheritance, and polymorphism. Here are some key concepts and features
of OOP in C#:
1.Classes and Objects: A class is a blueprint for creating objects. It defines the properties, methods, and
events that are associated with the object. An object is an instance of a class, created using the "new"
keyword. In C#, classes are defined using the "class" keyword.
2.Encapsulation: Encapsulation is the process of hiding the internal details of an object from the outside
world. In C#, encapsulation is achieved using access modifiers such as public, private, protected, and
internal.
3.Inheritance: Inheritance is the process of creating a new class from an existing class. The new class
inherits the properties and methods of the existing class, and can also add its own properties and methods.
In C#, inheritance is achieved using the "extends" keyword.
4.Polymorphism: Polymorphism is the ability of objects of different classes to be used interchangeably. In
C#, polymorphism is achieved through inheritance and interfaces.
5.Abstraction: Abstraction is the process of hiding the
implementation details of a system and only exposing the
necessary information to the user. In C#, abstraction is achieved
through the use of abstract classes and interfaces.
6.Overloading and Overriding: Overloading is the process of
defining multiple methods with the same name but different
parameters. Overriding is the process of replacing a method in
the base class with a new implementation in the derived class.

Overall, object-oriented programming is a powerful paradigm that allows developers to create complex,
scalable, and maintainable software systems. By using the OOP concepts and features of C#, developers
can build robust and efficient applications.
// Encapsulation: hiding implementation details behind public // Overloading: creating methods with the same name but
methods and properties different parameters
public class EncapsulationExample public class OverloadingExample
{ {
private int _x;
public int Add(int a, int b)
{
public int X
{ return a + b;
get { return _x; } }
set { _x = value; }
} public int Add(int a, int b, int c)
} {
return a + b + c;
// Inheritance: creating a new class based on an existing class }
public class Animal }
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
} The virtual keyword indicates that this method can
public class Dog : Animal
be overridden by a derived class using the override
{ keyword. In other words, a derived class can provide
public override void MakeSound()
{ its own implementation of the MakeSound method
Console.WriteLine("Woof"); that overrides the implementation in the base class.
}
}
// Overriding: changing the implementation of a method in a subclass
public class OverridingExample
{
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}

public class Dog : Animal


{
public override void MakeSound()
{
Console.WriteLine("Woof");
}
}

public class Cat : Animal


{
public override void MakeSound()
{
Console.WriteLine("Meow");
}
}
}
Internal access modifier

// Define a class with internal members


internal class InternalClass
{
// Define an internal field
internal int InternalField;

// Define an internal method


internal void InternalMethod()
{
// ...
}
}

// This code is outside of the InternalClass's assembly and can't access its internal members
public class SomeOtherClass
{
public void DoSomething()
{
InternalClass myInternalClass = new InternalClass();
// This line will generate a compile-time error because InternalField and InternalMethod are internal
// myInternalClass.InternalField = 42;
// myInternalClass.InternalMethod();
}
}
Differences of private and protected access modifiers
class MyClass
{
protected int myProtectedField;

protected void MyProtectedMethod()


{
class MyClass
// Accessible within MyClass and any derived class
{
}
private int myPrivateField;
}
private void MyPrivateMethod()
{ class MyDerivedClass : MyClass
{
// Accessible only within MyClass
} void DoSomething()
{
}
// Can access myProtectedField and MyProtectedMethod here
myProtectedField = 42;
class MyDerivedClass : MyClass
MyProtectedMethod();
{
}
void DoSomething()
{ }
// Cannot access myPrivateField or MyPrivateMethod here
} class SomeOtherClass
{
}
void DoSomething()
{
MyClass myClass = new MyClass();
// Cannot access myProtectedField or MyProtectedMethod here
// myClass.myProtectedField = 42;
// myClass.MyProtectedMethod();
}
}
C# libraries and frameworks
C# libraries and frameworks are pre-built sets of code that can be used to simplify and accelerate the
development of C# applications. Here are some popular libraries and frameworks used in C#
programming:

1..NET Framework: The .NET Framework is a software framework that provides a large library of pre-
built classes and functions for developing C# applications. It includes a Common Language Runtime
(CLR) that manages memory and provides other services to C# programs, as well as a class library that
provides access to system functionality such as file I/O, network communications, and user interface
design.
2..NET Core: .NET Core is a cross-platform, open-source version of the .NET Framework that is
optimized for building modern, cloud-based applications. It includes a runtime, libraries, and tools for
building and deploying C# applications on Windows, Linux, and macOS.
3.ASP.NET: ASP.NET is a web application framework built on top of the .NET Framework that provides
a set of tools and libraries for building web applications and services using C#. It includes support for
creating web pages, handling user input, accessing databases, and more.
4.Entity Framework: Entity Framework is an object-relational mapping (ORM) framework that
simplifies database access in C# applications. It provides a set of classes and tools for working with
databases, including a data model designer, LINQ-to-SQL queries, and automatic code generation.
5.NUnit: NUnit is a unit testing framework for C# applications that provides a set of tools for writing and
executing automated tests. It supports a variety of test case styles, including parameterized tests, test
fixtures, and test suites.
6.Newtonsoft.Json: Newtonsoft.Json is a popular JSON library for C# that provides a set of classes and
tools for working with JSON data. It includes support for serializing and deserializing JSON objects,
LINQ-to-JSON queries, and JSON schema validation.

These libraries and frameworks can save developers a lot of time and effort by providing pre-built
functionality and tools for common programming tasks. By leveraging these resources effectively,
developers can focus on building the unique features of their applications without having to re-invent the
wheel for every project.
In this program, we use the System library, which using System;
using System.Collections.Generic;
is a standard library that comes with C#. We
namespace MyProgram
specifically use the System.Collections.Generic {
namespace to access the List<T> class, which class Program
{
allows us to create a list of integers. static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
We create a List<int> object called numbers and int sum = 0;
initialize it with some values. We then use a foreach (int num in numbers)
foreach loop to iterate over the elements in the list {
sum += num;
and compute their sum. Finally, we print out the }
sum using the Console.WriteLine method, which is Console.WriteLine("The sum of the numbers is: " + sum);
also part of the System library. }
}
}

This program demonstrates the use of libraries in C# to access pre-existing functionality and simplify the
development of new applications. By using the System library, we are able to take advantage of a wide range
of classes and methods that are already available to us.
Future of C# and its relevance in the industry
C# is a widely used programming language with a strong presence in enterprise and web development. As
such, it is likely to remain relevant in the industry for the foreseeable future. Here are some reasons why C#
is likely to continue to be important in the industry:

1..NET 6: The latest version of the .NET Framework, .NET 6, was released in November 2021. This
release includes a number of new features and improvements, such as enhanced support for web
development, better performance, and improved developer productivity. This is a testament to Microsoft's
continued investment in the .NET ecosystem and its commitment to making C# a key part of that
ecosystem.
2.Cross-platform development: C# is a cross-platform language, which means that it can be used to
develop applications for a wide range of operating systems, including Windows, Linux, and macOS. This
makes C# a good choice for developers who want to build applications that can run on multiple platforms.
3.Popular frameworks: As mentioned earlier, C# has a number of popular frameworks and libraries, such
as ASP.NET and Entity Framework, that are widely used in the industry. These frameworks make it easier
for developers to build complex applications quickly and efficiently.
4.Strong community: C# has a strong and active community of developers, which is reflected in the
many resources available for learning and development, including online courses, tutorials, and forums.
This community is likely to continue to drive innovation and growth in the language and its ecosystem.
5.Microsoft's continued investment: Microsoft is heavily invested in the success of C# and the .NET
ecosystem. This is reflected in the ongoing development of new tools, frameworks, and libraries, as well
as the company's continued support for developers through programs like Visual Studio Dev Essentials
and Azure for Students.

Overall, the future of C# looks bright, and it is likely to remain a relevant and important programming
language in the industry for years to come.
Thank you for attention

• https://slideplayer.com/slide/12793347/

• https://medium.com/c-sharp-language/the-c-beginners-guide-6a14af03ed85

• https://www.geeksforgeeks.org/c-sharp-tutorial/

• https://dotnet.microsoft.com/en-us/languages/csharp

• https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/

You might also like