Professional Documents
Culture Documents
LESSON 1
TOPICS
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals.Valid types are
int and long. Which type you should use, depends on the numeric value.
Floating point types represents numbers with a fractional part, containing one or more decimals. Valid types are
float and double.
INTEGER TYPES
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the
int data type is the preferred data type when we create variables with a numeric value
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is
used when int is not large enough to store the value. Note that you should end the value with an "L":
FLOATING POINT TYPES
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
The float and double data types can store fractional numbers. Note that you should end the value with an "F" for
floats and "D" for doubles:
FLOATING POINT TYPES
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
BOOLEANS
A boolean data type is declared with the bool keyword and can only take the values true or false:
CHARACTERS
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A'
or 'c':
STRINGS
The string data type is used to store a sequence of characters (text). String values must be surrounded by double
quotes:
CLASSES
C# CLASS AND OBJECTS
A class is like a blueprint of a specific object that has certain attributes and features. For example, a car should
have some attributes such as four wheels, two or more doors, steering, a windshield, etc. It should also have some
functionalities like start, stop, run, move, etc. Now, any object that has these attributes and functionalities is a car.
Here, the car is a class that defines some specific attributes and functionalities. Each individual car is an object of
the car class. You can say that the car you are having is an object of the car class.
Likewise, in object-oriented programming, a class defines some properties, fields, events, methods, etc. A class
defines the kinds of data and the functionality their objects will have.
DEFINE A CLASS
In C#, a class can be defined by using the class keyword. Let's define a class named 'Student'.
A class can contain one or more constructors, fields, methods, properties, delegates, and events. They are called
class members. A class and its members can have access modifiers such as public, private, protected, and internal,
to restrict access from other parts of the program.
FIELD
A class can have one or more fields. It is a class-level variable that holds a value. Generally, field members should
have a private access modifier used with property.
PROPERTY
From C# 3.0 onwards, property declaration has been made easy if you don't want to apply some logic in getter
or setter. Using auto-implemented property, you don't need to declare an underlying private field. C# compiler
will automatically create it in IL code.
In the above example, backing private field for the FirstName and LastName will be created internally by the
compiler. This speed up the development time and code readability.
METHOD
A method can contain one or more statements to be executed as a single unit. A method may or may not return
a value. A method can have one or more input parameters.
The following defines the Sum method that returns the sum of two numbers.
METHOD
The following method doesn't return anything and doesn't have any parameters. The return type is void.
METHOD
You can create one or more objects of a class. Each object can have different values of properties and field but
methods and events behaves the same.
In C#, an object of a class can be created using the new keyword and assign that object to a variable of a class
type. For example, the following creates an object of the Student class and assign it to a variable of the Student
type.
OBJECTS OF A CLASS
You can now access public members of a class using the object.MemberName notation.
OBJECTS OF A CLASS
You can create multiple objects of a class with different values of properties and fields.
NAMESPACES
C# NAMESPACE
In the human world, a contract between the two or more humans binds them to act as per the contract. In the
same way, an interface includes the declarations of related functionalities. The entities that implement the
interface must provide the implementation of declared functionalities.
In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods,
properties, indexers, and events. However, it cannot contain instance fields.
C# - INTERFACE
The following interface declares some basic functionalities for the file operations.
The above declares an interface named IFile. (It is recommended to start an interface name with the letter "I" at
the beginning of an interface so that it is easy to know that this is an interface and not a class.) The IFile interface
contains two methods, ReadFile() and WriteFile(string).
C# - INTERFACE
IMPLEMENTING AN INTERFACE
C# 8.0 allows private, protected, internal, public, virtual, abstract, sealed, static, extern, and partial modifiers in an
interface.
Here, you will learn about exception handling in C# using try, catch, and finally blocks.
Exceptions in the application must be handled to prevent crashing of the program and unexpected result, log
exceptions and continue with other functionalities. C# provides built-in support to handle the exception using try,
catch & finally blocks.
EXCEPTION HANDLING IN C#
If there isn't an inner catch block that matches with raised exception
type, then the control will flow to the outer catch block until it finds
an appropriate exception filter. Consider the following example.
In the right example, an exception of type DivideByZeroException
will be raised. Because an inner catch block handles only the
NullReferenceTypeException, it will be handle by an outer catch
block.
CASTING
C# TYPE CASTING
Type casting is when you assign a value of one data type to another type.
In C#, there are two types of casting:
IMPLICIT CASTING
Implicit casting is done automatically when passing a smaller size type to a larger size type:
EXPLICIT CASTING
Explicit casting must be done manually by placing the type in parentheses in front of the value:
TYPE CONVERSION METHODS
It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean,
Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):
STRUCTURES
C# - STRUCT
In C#, struct is the value type data type that represents data structures. It can contain a parameterized
constructor, static constructor, constants, fields, methods, properties, indexers, operators, events, and nested
types.
struct can be used to hold small data values that do not require inheritance, e.g. coordinate points, key-value pairs,
and complex data structure.
STRUCTURE DECLARATION
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It
makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when
referring to the day in a week.
An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant
names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the
weekdays.
C# ENUMERATIONS TYPE - ENUM
The enum can be of any numeric data type such as byte, sbyte,
short, ushort, int, uint, long, or ulong. However, an enum
cannot be a string type.
Specify the type after enum name as : type. The following
defines the byte enum.
ACCESS AN ENUM