0% found this document useful (0 votes)
4 views53 pages

Introduction To C#

C# is a versatile, object-oriented programming language developed by Microsoft, designed to create a wide range of applications on the .NET Framework. Key features include simplicity, robustness, security, portability, and high performance, making it suitable for various platforms. The document also covers C# syntax, data types, operators, and control structures, providing foundational knowledge for programming in C#.

Uploaded by

Nandan Goud
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)
4 views53 pages

Introduction To C#

C# is a versatile, object-oriented programming language developed by Microsoft, designed to create a wide range of applications on the .NET Framework. Key features include simplicity, robustness, security, portability, and high performance, making it suitable for various platforms. The document also covers C# syntax, data types, operators, and control structures, providing foundational knowledge for programming in C#.

Uploaded by

Nandan Goud
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

Introduction to C#

Module-3
Introduction

• C# (pronounced "C-Sharp") is a versatile, object-oriented


programming language developed by Microsoft, built on the .NET
Framework, allowing developers to create a wide range of
applications like web, desktop, mobile, and cloud-based software;
• It was first released in 2000 and it has become one of the most
widely used languages for building Windows applications,
web services, and more.
• C# combines the power of C and C++ with the simplicity of
languages like Java and Visual Basic..
History of C#

• C# was developed by Anders Hejlsberg at Microsoft as part of


the .NET initiative.
• The language was introduced as a part of the .NET Framework
in 2000, aiming to provide a simple and modern language that
would overcome the limitations of other languages like C++
and Visual Basic.
• C# was designed to be a safe, efficient, and easy-to-use
language for building a wide range of applications.
Key Features
1. Object-Oriented Programming (OOP)
• C# is an object-oriented programming language means it encourages the use of classes and
objects.
• The core concepts of OOP in C# are:
• Abstraction: Hiding the complex implementation details and exposing only the essential features.

• Encapsulation: Bundling the data and the methods that operate on that data within a class.

• Inheritance: Allowing one class to inherit the properties and methods of another class.

• Polymorphism: Enabling objects to take many forms and allows methods to behave differently
depending on the object calling them.
2. Simplicity
• C# was designed to be simple and easy to use.
• Its syntax is clean, and many complex features found in other languages,
such as pointers, are either minimized or avoided altogether. Like Java or
C++, C# is similar but simpler.
3. Robustness
• C# is a robust language that ensures high reliability and easy
debugging.
• Features like exception handling, garbage collection, and type safety
contribute to its reliability allows developers to write secure and error-
free code.
4. Security
• C# provides built-in security features like type safety, which
prevents many types of errors and security vulnerabilities.
• The Common Language Runtime (CLR) checks type safety and
makes sure that code does not access memory outside its
boundaries.
5. Portability
• C# is part of the .NET ecosystem, which allows us to write
code that can be executed on different platforms.
• C# code can now be run on platforms like Windows, macOS,
and Linux, making it highly portable.
6. High Performance
• C# code is compiled into intermediate language (IL) code,
which is then executed by the CLR.
• The use of Just-In-Time (JIT) compilation allows C# programs
to run efficiently, and optimizations in recent versions of the
.NET framework have improved performance.
7. Scalable and Updateable
• C# is automatic scalable and updateable programming
language.
• For updating application we delete the old files and update
them with new ones.
8. Component Oriented
• C# is component oriented programming language.
• It is the predominant software development methodology used
to develop more robust and highly scalable applications.
C# Keywords
• Keywords are predefined sets of reserved words that have special
meaning in a program.
• The meaning of keywords can not be changed, neither can they be
directly used as identifiers in a program.
C# Identifiers
• Identifiers are the name given to entities such as variables, methods,
classes, etc.
• They are tokens in a program which uniquely identify an element.
• For Example:
int sum;
Sum -identifier
Rules for Naming an Identifier

• An identifier can not be a C# keyword.


• An identifier must begin with a letter, an underscore or @ symbol.
• The remaining part of identifier can contain letters, digits and
underscore symbol.
• Whitespaces are not allowed. Neither it can have symbols other than
letter, digits and underscore.
• Identifiers are case-sensitive.
So, getName, GetName and getname represents 3 different
identifiers.
Here are some of the valid and invalid
identifiers:
Literals
• Literals are the way in which the values that stored in variables
are represented
Integer Literals
• An integer Literal is a sequence of digits. There are two types of
Integers.
1. Decimal Integers:-It consists of a set of digits from 0 to 9 and
include minus sign.
Ex. 141 -151023

Note:- Commas,Embedded space and non digits characters are not


permitted b/w digits.

2. Hexadecimal Literals:-A digits that starts with ox or OX is


known as Hexadecimal.
Real Literals
• The Numbers that includes decimal sign is knows as Real Literals or
A whole number followed by a decimal point and the fraction part is
known as Real Literals.
Ex.
0.0777 -0.85 2345
325. -.34 .87
0.89e5 34e-3 1.3e+5 1.4E2 1.9E-5
• A floating point Literal may be four types.
1. whole number
2. an exponent
3. fractional part
4. decimal part
Boolean Literals:- There are two types of Boolean Literal
values in c#.
• true
• false
Single character Literals:-A single character Literals contains
a single character enclosed within a single quotes(' ').
Ex.
• 'a','8',' ;',' ' etc
String Literals:-A string Literals is a sequence of characters
enclosed within double quotes(" ").
• Ex.
• "HELLO","23","4"."a" etc
• Backslash Character Literals:-There are some backslash
character Literals which is shown in the Table below:-
C# Simple Program
• [Link]("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.
• 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 which 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.
• 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.
C# Variable

• A variable is a name of memory location. It is used to


store data. Its value can be changed and it can be
reused many times.
• It is a way to represent memory location through
symbol so that it can be easily identified.
• The basic variable type available in C# can be
categorized as:
C# Data Types

• A data type specifies the type of data that a variable can


store such as integer, floating, character etc.
• There are 3 types of data types in C# language.
Value Data Type

• The value data types are integer-based and floating-


point based. C# language supports both signed and
unsigned literals.
• There are 2 types of value data type in C# language.
• 1) Predefined Data Types - such as Integer, Boolean,
Float, etc.
• 2) User defined Data Types - such as Structure,
Enumerations, etc.
• The memory size of data types may change according
to 32 or 64 bit operating system.
Reference Data Type

• The reference data types do not contain the actual data


stored in a variable, but they contain a reference to the
variables.
• If the data is changed by one of the variables, the other
variable automatically reflects this change in value.
• There are 2 types of reference data type in C# language.
1) Predefined Types - such as Objects, String.
2) User defined Types - such as Classes, Interface.
C# operators

• An operator is simply a symbol that is used to perform


operations.
• There are following types of operators to perform
different types of operations in C# language.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Ternary Operators
• Null-Coalescing Operator
Arithmetic Operators

• Arithmetic operators are used to perform common


mathematical operations:
int x = 10;

Assignment Operators

Assignment operators are used to assign values to variables.


int a=10;
Relational Operator
• Relational operators are used to compare two values (or
variables).
• The return value of a comparison is either True or False.
Logical Operator
• Logical operators are used to determine the logic
between variables or values.
Ternary Operator

• The ternary operator is a shorthand for an if-else statement. It


evaluates a condition and returns one of two values depending
on whether the condition is true or false..
• condition ? if true : if false ;
• Example: (a>b)? “a”:”b”;
Null-Coalescing Operator

• null-coalescing operator is used when we want to put


a default value if the value of the variable is null.
Control Structures
A programming language uses control statements to control
• Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled.

the flow of execution of program based on certain


conditions. These are used to cause the flow of execution to
advance and branch based on changes to the state of a
program.
• Conditional Statements of C#
• if
• if-else
• if-else-if
• Nested if
• Switch
• Nested Switch
• If Statement
• The if statement checks the given condition. If the condition
evaluates to be true then the block of code/statements will
execute otherwise not.
• Syntax:
Example
• If-else Statement
• The if statement evaluates the code if the condition is true but
what if the condition is not true, here comes the else statement.
It tells the code what to do when the if condition is false.
• If-else-if ladder Statement
• The if-else-if ladder statement executes one condition from
multiple statements.
• The execution starts from top and checked for each if condition.
• The statement of if block will be executed which evaluates to
be true.
• If none of the if condition evaluates to be true then the last else
block is evaluated.
• Nested – If Statement
• If statement inside an if statement is known as nested if.
• if statement in this case is the target of another if or else
statement.
• When more than one condition needs to be true and one of
the condition is the sub-condition of parent condition, nested if
can be used.
• Switch statement is an alternative to long if-else-if ladders.
• The expression is checked for different cases and the one
match is executed.
• break statement is used to move out of the switch.
• If the break is not used, the control will flow to all cases below
it until break is found or switch comes to an end.
• There is default case (optional) at the end of switch, if none of
the case matches then default case is executed.
• Nested Switch
• Nested Switch case are allowed in C# .
• In this case, switch is present inside other switch case.
• Inner switch is present in one of the cases in parent switch.

You might also like