Академический Документы
Профессиональный Документы
Культура Документы
Objective
What is C#
C# is an object-oriented programming language developed by Microsoft. That intends to be a simple, modern, and generalpurpose programming language for application development.
Object-Oriented Methodology
Object orientation is a software development methodology that is based on modeling a real-world system. An object oriented program consists of classes and objects.
Let us understand the termsclass and objects
Object-Oriented Methodology
Class Car
BMW
SUV
Mercedes
Objects
Car is flashing the lights to pass the message to the other car
Introducing C# (Contd.)
C#, also known as C-Sharp, is a programming language introduced by Microsoft. C# is specially designed to work with the Microsofts .NET platform.
Classes in C#
Consider the following code example, which defines a class:
public class HelloWorld {
}
}
Classes in C# (Contd.)
public class HelloWorld { public static void Main(string[] args) {
Classes in C# (Contd.)
public class HelloWorld { public static void Main(string[] args) {
Classes in C# (Contd.)
public class HelloWorld { public static void Main(string[] args) {
The Main() Function Is the entry point of an application Is used to create objects and invoke member functions
Classes in C# (Contd.)
public class HelloWorld { public static void Main(string[] args) {
System.Console.WriteLine("Hello, \n");
} }
Classes in C# (Contd.)
public class Helloworld { public static void Main(string[] args) {
The Escape Character Displays New line character. Other special characters can also be displayed such as \t, \b and \r
Overview of CTS
Establishes a Framework that enables cross-language integrations, type safety, and high performance code execution.
Provides an Object Oriented Model that supports the complete implementation of many programming languages.
Define set of rules that languages must follow, which helps ensures that object written in different languages can interact with each other.
Overview of CTS
Type
Value Type
Reference Type
Value types:
Reference types:
Directly contain their data Each has its own copy of data Operations on one cannot affect another
Store references to their data (known as objects) Two reference variables can reference same object Operations on one can affect another
Declaring Variables
A variable is a location in the memory that has a name and contains a value. A variable is associated with a data type that defines the type of data that can be stored in a variable.
Data Types in C# Represents the kind of data stored in a variable C# provides you with various built-in data types, such as: char int float double bool string
string
Name = Peter
float
int char
Marks = 83.56
Age = 23 Vowel = a
Data types in C# The following types of data types are supported by C#: Value types
int NumNum; Num=5; 5
Data types in C# The following types of data types are supported by C#: Reference types
string Str=Hello; Address H 0 E 1 Str L 2 L 3 O 4
Naming variables in C# The following rules are used for naming variables in C#: Must begin with a letter or an underscore Should not contain any embedded spaces or symbols Must be unique Can have any number of characters Keywords cannot be used as variable names
Family_Size
Gender
Initializing Variables in C# Specifies the value that needs to be stored in a variable. The value could be an integer, a decimal, or a character.
The using keyword is used to include the namespaces in the program. Comments are used to explain the code and are represented by // symbols. Member variables are used to store the data for a class. Member functions are declared inside the class that are used to perform a specific task.
The Execute class is used as a class from where the Car class can be instantiated.
3.
Using Operators
Applications use operators to process the data entered by a user. Operators in C# can be classified as follows:
Arithmetic operators Arithmetic Assignment operators Unary operators Comparison operators Logical operators
Arithmetic Operators
Arithmetic operators are the symbols that are used to perform arithmetic operations on variables. The following table describes the commonly used arithmetic operators.
Operator +
Example
If Y is equal to 20 and Z is equal to 2, X will have the value 22. Used to subtract two numbers X=Y-Z; If Y is equal to 20 and Z is equal to 2, X will have the value 18. * Used to multiply two numbers X=Y*Z; If Y is equal to 20 and Z is equal to 2, X will have the value 40. / Used to divide one number by another X=Y/Z; If Y is equal to 21 and Z is equal to 2, X will have the value 10. But, if Y is equal to 21.0 and Z is equal to 2, X will have the value 10.5. % Used to divide two numbers and return the remainder X=Y%Z; If Y is equal to 21 and Z is equal to 2, X will contain the value 1.
Operator = += X = 5; X+=Y;
Usage
-=
X-=Y;
Same as: X = X - Y;
*=
X*=Y;
Same as: X = X * Y;
/=
X/=Y;
Same as: X = X / Y;
%=
X%=Y;
Same as:
X = X % Y;
Unary Operators
Unary operators are used to increment or decrement the value of an operand by 1. The following table explains the usage of the increment and decrement operators.
Operator ++ ++Operand;
Usage
Example
If the initial value of X is 5, after the execution of the preceding statement, values of both X and Y will be 6. Y = X++; If the initial value of X is 5, after the execution of the preceding statement, value of X will be 6 and the value of Y will be 5.
--
Or,
Operand--; (Postdecrement)
by 1
Comparison Operators
Comparison operators are used to compare two values and perform an action on the basis of the result of that comparison. The following table explains the usage of commonly used comparison operators.
Operator
Usage
Description
Example (In the following examples, the value of X is assumed to be 20 and the value of Y is assumed to be 25)
<
bool Result; Result = X < Y; Result will have the value true.
>
bool Result; Result = X > Y; Result will have the value false.
<=
bool Result; Result = X <= Y; Result will have the value true.
>=
bool Result; Result = X >= Y; Result will have the value false.
Operator
Usage
Description
Example (In the following examples, the value of X is assumed to be 20 and the value of Y is assumed to be 25)
==
expression1 == expression2
!=
expression1 != expression2
Logical Operators
Logical operators are used to evaluate expressions and return a Boolean value. The following table explains the usage of logical operators.
Operator &&
Description Returns true if both expression1 and expression2 are true. bool Result; string str1, str2; str1 = Korea; str2 = France;
Example
Result= ((str1==Korea) && (str2==France)) Console.WriteLine (Result .ToString()); The message displays True because str1 has the value Korea and str2 has the value France. ! ! expression Returns true if the expression is false. bool Result int x; x = 20; Result=(!( x == 10)) Console.WriteLine(Result.ToString()); The message displays True because the expression used returns true.
Operator
Usage
Description
Example
||
expression1 || expression2
bool Result string str1, str2; str1 = Korea; str2 = England; Result= ((str1==Korea) || (str2== France)) Console.WriteLine (Result .ToString()); The message displays True if either str1 has the value Korea or str2 has the value France.
expression1 ^
bool Result;
expression2
expression1 or expression2 is
true. It returns false if both expression1 and expression2 are true or if both expression1 and expression2 are false.