Professional Documents
Culture Documents
Object-Oriented Fundamentals In C#
Language Fundamentals
• A C# program consists of the following parts
– Namespace declaration
– Namespace declaration
– A class
– Class methods
– Class attributes
– A Main method
– Statements and Expressions
– Comments
• Let us look at a simple code that prints the words "Hello World":
– First start new project as follows and write the following program:
– Click on start button all program Microsoft visual studio 2010
Microsoft visual studio 2010 select visual C # New Project
select visual c # select consol application(give name, location and
solution name) finally click on ok button.
Cont…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// my first program in C#
System.Console.Out.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Let us look at the various parts of the given program:
1. The first line of the program using System; -
– the using keyword is used to include the System namespace in the program.
– A program generally has multiple using statements.
– System is a namespace, which is a scheme that provides a way to group similar classes.
– For instance, we use the console class, which is a part of the system namespace
2. The next line has the namespace declaration.
– A namespace is a collection of classes. The ConsoleApplication1 namespace contains the
class Program.
– It is used to organize classes
– It is designed for providing a way to keep one set of names separate from other. The
class names declared in one namespace does not conflict with the same class name
declared in another.
3. The next line has a class declaration,
– The class contains the data and method definitions that your program uses.
– Classes generally contain multiple methods.
– Methods define the behavior of the class. However, this class has only one method
Main.
Cont…
4. The next line defines the Main method, which is the entry point for
all C# programs.
– The Main method states what the class does when executed.
5. System.Console.Out.WriteLine("Hello World");
– System is a namespace
– Console define the attributes of a collection of similar “Console” objects
– Out is an object that represents the screen. The out object was created and
endowed with the method WriteLine (). Not all object have the WriteLine ()
method.
– The WriteLine () method prints a line of output on the screen. Where as
Write() method outputs one or more values on the screen without a new
character (this means any subsequent output will be printed in the same
line).
6. The last line Console.ReadKey();
– It is for the VS.NET Users. This makes the program wait for a key press and it
prevents the screen from running and closing quickly when the program is
launched from Visual Studio .NET.
Cont…
• It is worth to note the following points:
– C# is case sensitive.
– All statements and expression must end with a
semicolon (;).
– The program execution starts at the Main method.
– Unlike Java, program file name could be different
from the class name.
Comments
• In large programs it becomes difficult to remember why certain
steps were included and the role of certain variables and methods
• Program comments are nonexecuting statements that you add to
document a program
• Comments is a text that is put somewhere in the code for a human
to read, that the computer/ compiler ignores
• You can also comment out various statements in a program to
debug and observe the effects of the program with the statement
or statements commented out
• You should uses comment to describe what you are doing.
• Write a good comments is a part of writing a good code .
• There are three types of comments in C#:
– Single Line comments(// comment )
– Multiple line comments /Block comments ( /* comment */)
– XML-documentation /tags comments(/// xml tag)
Example
Compiling and Executing a Program
• In C# programming using Visual Studio .NET it is possible to
compiling and executing a program from the command line or
using Visual Studio IDE
• C# programs can also be written using the Visual Studio IDE
(instead of a text editor). This approach offers many
advantages including:
– Some of the code you need is already created for you
– The code is displayed in color, so you can more easily identify parts of
your program
– If error messages appear when you compile your program, you can
double-click on an error message and the cursor will move to the line
of code that contains the error
– Other debugging tools are available
Variables
• A data item is classified as either constant or variable
• Constant data items cannot vary
• Variable data items can hold different values at different points of time
• During the execution of a program, data is temporally stored in memory.
• A variable is the name given to a memory location holding a particular
type of data.
• So, each variable has associated with it’s a data type and value.
• All data items in a C# program have a data type
• Data type describes the format and size of a piece of data
• C# provides 14 basic or intrinsic types
• The most commonly used data types are: int, double, char, string, and
bool
• Each C# intrinsic type is an alias, or other name for, a class in the System
namespace
Variable Declaration
• It includes:
1. The data type that the variable will store
2. An identifier that is the variable’s name (start by a letter or underscore followed
by any of the combination of letter, number or underscore )
3. An optional assignment operator and assigned value when you want a variable to
contain an initial value
4. An ending semicolon
• The general syntax to declare a variable is:
Data_Type Variable_Name;
• E.g. double myage;
int i;
char sex;
string name
• Declaring of variables of the same type be separated by commas.
• In C# (like other modern languages), you must declare variables before using
them.
• E.g. int x, y;
double myage =25.5,yourage =19.75;
Constant Variables or Symbols
• Constants are variables whose values, once defined, can not be
changed by the program.
• Constant variables are declared using the const keyword, like:
const double PI = 3.142;
• Constant variables must be initialized as they are declared. It is a
syntax error to write:
const int MARKS;
• It is conventional to use capital letters when naming constant
variables.
• After declaration, to use the declared variable you must initialize it
using assignment operator.
• General syntax : Variable_Name= value;
• E.g. int myage;// declaration
myage =25; //initialization
or int myage =25;
Using the Integral Data Types
• In C#, there are nine integral data types:
byte (8+), sbyte (8), short (16), ushort (16+), int (32),
uint (32+), long (64), ulong (64+), and char (16)
• char is used for characters like ‘A’ or ‘b’, while the
other eight are used to store whole numbers
• You use variables of type int to store integers
• When you assign a value to a numeric variable, you
do not type any commas:
– int cost = 1000; // NOT int cost = 1,000;
Operators in C#
Arithmetic Operators
• Several common arithmetic operators are allowed in C#.
Operand Description Examples
+ Add 45+2: the result is 47
- Subtract 45-2: the result is 43
* Multiply 45*2: the result is 90
Divide 45/2: the result is 22( not 22.5)
% Remainder or modulo 45%2: the result is 41
• When you divide two integers (using the division operator or the modulus operator),
the result is ALWAYS an integer
• Operator precedence refers to the order in which parts of a mathematical expression
are evaluated
Compound assignment operators
Operand Description Examples
+= Add and assign the result i+=2: means i=i+2;
-= Subtract and assign the result i-=2: means i=i-2;
*= Multiply and assign the result i*=2: means i=i*2;
/= Divide and assign the result i/=2: means i=i/2;
%= Remainder/ modulo and assign the result i%=2: means i=i%2;
Cont…
Increment/Decrement Operators: (++) and (--)
• The auto increment (++) and auto decrement (--) operators provide a convenient
way of, respectively, adding and subtracting 1 from a numeric variable.
• E.g.:
– if x was 10 and if x++ is executed then x will automatically changed to 11.
• Prefix and Postfix:
– The prefix type is written before the variable. whereas the postfix type appears after the
variable name.
• Eg. ++ myAge prefix; myAge ++ postfix
• Prefix and postfix operators cannot be used at once on a single variable:
• The prefix operator is evaluated before the assignment, and the postfix operator is
evaluated after the assignment.
• E.g.
int k = 5;
(auto increment prefix) y= ++k + 10; //gives 16 for y
(auto increment postfix) y= k++ + 10; //gives 15 for y
(auto decrement prefix) y= --k + 10; //gives 14 for y
(auto decrement postfix) y= k-- + 10; //gives 15 for y
Cont…
Relational Operators
• Relational operators are used for comparison purposes in conditional statements. Common
relational operators in C# are:
Operand Description
== Equality check
!= Un-equality check
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
• Unlike the class fields that are unique to each instance, the
methods of the class are shared
• Although the methods are shared, there must be a difference
between two method calls (from different objects), because
each returns a different value (based on their unique fields)
• When a method is called, you automatically pass a reference
to the memory of the object into the method
• The implicitly passed reference is the this reference
Understanding the this Reference
• You can create a constructor that sets the same value for all
objects instantiated. The program can eventually call the
method of the object to set the value. However, this might
not always be the most efficient technique.
Passing Parameters to Constructors