Académique Documents
Professionnel Documents
Culture Documents
ataSet
Y
Y
Ñ
Today we will learn the language fundamentals of C#. We will explore the data types in C#,
using variables, operators, flow control statements like if.. else, looping structure and how to use
arrays.
c
o
There are two kinds of data types in C#.
Value types are passed to methods by passing an exact copy while Reference types are passed to
methods by passing only their reference (handle). Implicit data types are defined in the language
core by the language vendor, while explicit data types are types that are made by using or
composing implicit data types.
s we saw in the first issue, implicit data types in .Net compliant languages are mapped to types
in the Common Type System (CTS) and CLS (Common Language Specification). ùence, each
implicit data type in C# has its corresponding .Net type. The implicit data types in C# are:
6
Y
YYYYYYYYYYYYYYYYYY
Y
Y
Y
YY
YYYYYY
YYYYYYYYYYYYYYY
YY
Y
Y
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
YY
Y
Y
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
Y
YYYY
Y
YYYYYY YYYYYYYYYYYYYYYY
Y
Y
YY
YYYYY YYYYYYYYYYYYYY
Y Y
YY Y Y
YYYYY! "YYYYYYYYYYYYYY#Y
Y$% "Y Y$% " Y
YYYY! "YYYYYYYYYYYYY
%Y
Y
YY Y"%$Y
Implicit data types are represented in language using keywords, so each of the above is a
keyword in C# (Keyword are the words defined by the language and can not be used as
identifiers). It is worth noting that string is also an implicit data type in C#, so
is a
keyword in C#. The last point about implicit data types is that they are value types and thus
stored on the stack, while user defined types or referenced types are stored using the heap.
stack is a data structure that store items in a list in first out (LIF) fashion. It is an area of
memory supported by the u and its size is determined at the compile time. heap
consists of memory available to the program at run time. Reference types are allocated using
memory available from the heap dynamically (during the execution of program). The garbage
collector searches for non-referenced data in heap during the execution of program and returns
that space to perating System.
ü
uring the execution of a program, data is temporarily stored in memory. variable is the name
given to a memory location holding a particular type of data. So, each variable has associated
with it a data type and a value. In C#, variables are declared as:
YYYY1 Y -2Y13
24Y
e.g.,
YYYY
Y
4Y
The above line will reserve an area of 4 bytes in memory to store an integer type values, which
will be referred to in the rest of program by the identifier 'i'. You can initialize the variable as
you declare it (on the fly) and can also declare/initialize multiple variables of the same type in a
single statement, e.g.,
YYYY YY
#YYYYYY5Y
4Y
YYYY Y-
YYY5Y *%Y3
Y5Y&$*(4Y
YYYY
YY
YYYYYYYY5Y 4Y
In C# (like other modern languages), you must declare variables before using them. lso, there is
the concept of " efinite ssignment" in C# which says "local variables (variables defined in a
method) must be initialized before being used". The following program won't compile:
YYYY
Y3
Y
X'Y
YYYY6Y
YYYYYYYY
Y4YYYYYYYYYYYYY
YYYYYYYY77YY5Y4Y
YYYYYYYY*8
9
X'4YYYY77Y
Y
YYYY:Y
But, if you un-comment the 2nd line, the program will compile. C# does not assign default
values to local variables. C# is also a type safe language, i.e., values of particular data type can
only be stored in their respective (or compatible) data type. You can't store integer values in
Boolean data types like we used to do in C/C++.
Constant variables must be initialized as they are declared. It is a syntax error to write:
YYYY Y
Y<#=4Y
lthough it is not mandatory to follow this convention, it is highly recommended that you
strictly follow the convention. Microsoft no longer supports ùungarian notation, like using
iMarks for integer variable. lso, using the underscore _ in identifiers is not encouraged.
Several common arithmetic operators are allowed in C#.
Y
@YYYYYYYYYX'Y
YYYYYYYYYX
'Y
AYYYYYYYYYX
-'Y
7YYYYYYYYYX
3
'Y
BYYYYYYYYYX
Y
Y'Y
@@YYYYYYYYX
Y Y'Y
YYYYYYYYX
Y Y'Y
Y
Y
Y 4Y
-Y
-Y
6Y
YYYYY<
?-
Y
YYYY6Y
YYYYYYYY77Y>Y-
Y,Y YYY
Y-
YY
YYYYYYYY77Y@YYAY7YBY@@YY
YYYYYYYY
Y3
Y
X'Y
YYYYYYYY6Y
YYYYYYYYYYYY77Y
YY
%Y
%Y
YYYYYYYYYYYY77Y
-
YYY-
Y
YYYYYYYYYYYY
Y5%Y
5%Y-
5%Y54YY
YYYYYYYYYYYY Y
54YYYYYYYY77Y
YY
3
Y
YYYYYYYYYYYY
YY5Y%YY5Y4Y77Y-
Y3
Y
YYYYYYYYYYYYYYYYYYYYYY5YY@Y4YYYYY
YYYYYYYYYYYY
YYY5YYY4YYYYY
YYYYYYYYYYYY-
YYYYYY5YYAY4YYYYY
YYYYYYYYYYYY
YYYYY5YY7Y4Y
YYYYYYYYYYYY77Y
YY$7Y
YYYYYYYYYYYYYYYYYYY5Y$YBY4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX Y5Y6:%YY5Y6: %Y%Y'4Y
YYYYYYYYYYYY*8
9
X'4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX YYYYYYYYY6:YY6:Y
Y6: %Y
YYYYYYYYYYYYYY%Y%Y'4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX 0
YY6:YY6:Y
Y6: %Y
YYYYYYYYYYYYYY%Y%Y
'4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX ;
YYYYY6:YY6:Y
Y6: %Y
YYYYYYYYYYYYYY%Y%Y-
'4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX
YYYY6:YY6:Y
Y6: %Y
YYYYYYYYYYYYYY%Y%Y
'4Y
YYYYYYYYYYYY*8
9
X'4Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX #
Y,Y$Y
Y
3
Y Y
YYYYYYYYYYYYYY6:Y
Y6: %Y%Y'4Y
YYYYYYYYYYYY@@4YYYYYYYYY77Y
YY YY
YYYYYYYYYYYY4YYYYYYYYY77Y
YY YY
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX Y5Y6:%YY5Y6: %Y%Y'4Y
YYYYYYYY:Y
YYYY:Y
:Y
lthough the program above is quite simple, I would like to discuss some concepts here. In the
*8
9
X' method, we have used format-specifiers 6
: to indicate the position of
variables in the string.
YYYY*8
9
Y
YYYYYYX YY6:YY6:Y
Y6: %Y
YYYYYY%Y%Y'4Y
ùere, 6:, 6: and 6: will be replaced by the values of the , and variables. In
6
:,
specifies that (i+1)th variable after double quotes will replace it when printed to the
Console. ùence, 6: will be replaced by the first one, 6: will be replaced by the second
variable and so on...
nother point to note is that num1++ has the same meaning as:
YYYYY5YY@Y4Y
or,
YYYYY@5Y4Y
Y
The compiler will first assign num1 to num2 and then increment num1 by 1.
ssignment operators are used to assign values to variables. Common assignment operators in
C# are:
Y
5YYYYYYYYYX
-Y
'Y
@5YYYYYYYYX
3Y
'Y
5YYYYYYYYX
3Y
'Y
A5YYYYYYYYX
-
3Y
'Y
75YYYYYYYYX
3
Y
'Y
B5YYYYYYYYXY
'Y
The equals (=) operator is used to assign a value to an object. Like we have seen
YYYY YYYY
;
Y5Y4Y
assigns the value 'false' to the isPaid variable of Boolean type. The left hand and right hand side
of the equal or any other assignment operator must be compatible, otherwise the compiler will
complain about a syntax error. Sometimes casting is used for type conversion, e.g., to convert
and store a value in a variable of type double to a variable of type int, we need to apply an
integer cast.
YYYY YY CY5Y&*" 4Y
YYYY77Y
CY,
Y YY Y&Y
YYYY
YYYYY
CYYYY5YX
'Y C4Y
f course, when casting there is always a danger of some loss of precision; in the case above, we
only got the 4 of the original 4.67. Sometimes, the casting may result in strange values:
YYYY
YYYYYY
CYYYY5Y$4Y
YYYY
YY
CYYYY5YX
'Y
C4YYYYY
YYYY77Y
CY,Y YY Y$ $"Y
Variables of type short can only take values ranging from -32768 to 32767, so the cast above can
not assign 32800 to
C. ùence
C took the last 16 bits (as a short consists of
16 bits) of the integer 32800, which gives the value -32736 (since bit 16, which represents the
value 32768 in an int, now represents -32768).
It won't get compiled and the compiler will generate a syntax error.
Relational operators always result in a Boolean statement; either true or false. For example if we
have two variables
YYYY
YY5Y%YY5Y"4Y
then,
YYYYY55YYY77YY
YYYYYD5YYY77Y
Y
YYYYY2YYYY77YY
YYYYY1YYYY77Y
Y
YYYYY15YYY77Y
Y
YYYYY25YYY77YY
nly compatible data types can be compared. It is invalid to compare a with an
, so if
you have
YYYY
YYYYY
Y5Y4Y
YYYY YYYY Y5Y
4Y
you cannot compare i and b for equality (i==b). Trying to do so will result in a syntax error.
Ñ
"
These operators are used for logical and bitwise calculations. Common logical and bitwise
operators in C# are:
Y
EYYYYYYYYYX
,
Y<F0'Y
±YYYYYYYYYX
,
Y?#'Y
GYYYYYYYYYX
,
YH?#'Y
DYYYYYYYYYX
,
YF?>'Y
EEYYYYYYYYX 9
Y
Y
Y
Y<F0'Y
±±YYYYYYYYX 9
Y
Y
Y
Y?#'Y
The operators &, | and ^ are rarely used in usual programming practice. The NT operator is
used to negate a Boolean or bitwise expression like:
YYYY YYYY YYYYY5Y4Y
YYYY YYYY YYYY5YD 4Y
YYYY77Y Y,Y Y
Y
In the above statement we used paranthesis to group our conditional expressions and to avoid
any ambiguity.
You can use & and | operators in place of && and || but for combining conditional expressions,
&& and || are more efficient because they use "short circuit evaluation". For example, if in the
expression X
2$YEEYI1',
2$ evaluates to false, the second expression I1 won't be checked
and false will be returned (when using N , if one of the participant operands is false, the whole
operation will result in false). ùence, one should be very careful when using assignment
expressions with && and || operators. The & and | operators don't do short circuit evaluation and
do execute all the comparisons before returning the result.
There are some other operators present in C#. short description of these is given below:
Y
11YYYYYYYYX Y
Y
,
Y-
'Y
22YYYYYYYYX
Y
Y
,
Y-
'Y
*YYYYYYYYYX
YY
Y I 'Y
JKYYYYYYYYX
.
Y-
YY
Y
YYYYYYYYYYYY
YY
'Y
X'YYYYYYYYX Y-
'Y
LMYYYYYYYYX
Y-
'Y
ll operators are not treated equally. There is a concept of "operator precedence" in C#. For
example:
YYYY
Y
Y5YY@Y$YAY"4Y
YYYY77Y
Y,Y YY Y$Y
3 will be multiplied by 6 first then the result will be added to 2. This is because the multiplication
operator * has precedence over the addition operator +. For a complete table of operator
precedence, consult MS N or the .Net framework documentation.
Condition checking has always been the most important construct in any language right from the
time of the assembly language days. C# provides conditional statements in the form of the
if...else statement. The structure of this statement is:
YYYY
XY.-
'Y
YYYYYYYY Y
Y YY Y
YYYYY
YYYYYYYY Y
Y YY Y
In the above example, the console message will be printed only if the expression i==5 evaluates
to true. If you would like to take some action when the condition does not evaluate to true, then
you can use else clause:
YYYY
X
55'Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX >Yd%Y!Y
Y Y* '4Y
YYYYY
YYYYYYYY*8
9
Y
YYYYYYYYYYX
***8Y,
Y!Y YL '4Y
nly the first message will be printed if i is equal to 5. In any other case (when i is not 5), the
second message will be printed. If you want to use a block of statements (more than one
statement) under if or else, you can enclose your block in {} brackets:
YYYY
X
55'Y
YYYY6Y
YYYYYYYYIY5Y
A4Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX >Yd%Y!Y
Y Y* '4Y
YYYY:Y
YYYYY
YYYY6Y
YYYYYYYYIY5Y
74Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX
***8Y,
Y!Y YL '4Y
YYYY:Y
I would always recommend to use { } brackets to enclose the statements following if and else
even if you only have a single statement. It increases readability and prevents many bugs that
otherwise can result if you neglect the scope of if and else statements.
ùere Y
X
55"' is executed only if the first condition
55 is false, and else at line ´ will
be executed only if the second condition
55" (line 5) executes and fails (that is, both the first
and second conditions fail). The point here is at line ´ is related to
on line 5.
Since
*** is also an statement, you can use it under other if...else statement (nesting),
like:
YYYY
X
2'YYYYYYYYY77Y
YY
YYYY6Y
YYYYYYYY
X
55"'YYYY77Y
Y$Y
YYYYYYYY6Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX ?%Y"Y
YY Y* '4Y
YYYYYYYY:Y
YYYYYYYYYYYYYYYY77Y
Y Y
YYYYYYYY6Y
YYYYYYYYYYYY*8
9
Y
YYYYYYYYYYYYYYX ?-DY!Y
Y YY Y Y"D '4Y
YYYYYYYY:Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX >Yd%Y!Y
Y Y
Y Y* '4Y
YYYY:Y
YYYYYYYYYYYYYYYY77Y
Y$Y
YYYY6Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX
***8Y,
Y!Y Y
YYYYYYYYYYY
YY YL '4Y
YYYY:Y
"
If you need to perform a series of specific checks, ,
*** is present in C# just for this.
The general structure of the ,
*** statement is:
YYYY,
X
Y
Y
Y.-
'Y
YYYY6Y
YYYYYYYYY .-
MY
YYYYYYYYYYYY Y
YYYYYYYYYYYY
Y
YI-Y Y
YYYYYYYY77YY
YY Y
YYYYYYYY***Y
YYYYYYYY MY
YYYYYYYYYYYY Y
YYYYYYYYYYYY
Y
YI-Y Y
YYYY:Y
The program must be supplied with an integer command line argument. First, compile the
program (at the command line or in Visual Studio.Net). Suppose we made an exe with name
"SwitchCaseExample.exe", we would run it at the command line like this:
YYYYM2,
N.-YY
YYYYY -YYX ,'YYY
Y
Y
or,
YYYYM2,
N.-Y$&Y
YYYYY -YY
Y
Y Y%YYY$Y
If you did not enter any command line arguments or gave a non-integer argument, the program
will raise an exception:
YYYYM2,
N.-Y
YN.-
MY *!.? ?#N.-
MY
YY!.Y,Y
Y Y YY Y
*Y
YYY Y
-*,
N.-*
Y
YYYYX
JKY
!- 'Y
YY
YYYMO3
Y
Y-
I O,
N.-OY
YYYYY,
N.-*YM
Y(Y
Let's get to internal working. First, we converted the first command line argument
(
!- JK) into an
variable
- . For conversion, we used the static ;
X' method
of the
data type. This method takes a
and returns the equivalent integer or raises an
exception if it can't. Next we checked the value of input variable using a switch statement:
YYYY,
X
- 'Y
YYYY6Y
YYYY***Y
YYYY:Y
Later, on the basis of the value of input, we took specific actions under respective case
statements. nce our case specific statements end, we mark it with the break statement before
the start of another case (or the default) block.
YYYYY$MYYYYYYY77Y
Y
Y
Y$Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX Y -Y$YX
'YY
Y
YYYYYYYYYYY
Y
'4Y
YYYYYYYY
4YYYY77Y Y YY,
Y Y
If all the specific checks fail (input is none of 1,2 and 3), the statements under executes.
default: // if it is not any of the above Console.WriteLine ("You typed a number other than 1, 2
and 3"); break; // get out of switch block
There are some important points to remember when using the switch...case statement in C#:
YYYYY
MYYYYYYYY77Y
%Y .Y
Y
YYYYY ;
MY
YYYYYYYY
Y5Y <
4Y
YYYYYYYY*8
9
X ;
Y
YY<
Y
'4Y
YYYYYYYY
4Y
YYYY MY
YYYYYYYY
Y5Y
P 4Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX
PY
Y
3
'4Y
YYYYYYYY
4Y
Y The end of the case and default statements is marked with
(or ) statement. We
don't use 6: brackets to mark the block in ,
*** as we usually do in C#
Y C# does not allow fall-through. So, you can't leave or without
statement (as you can in Java or C/C++). The compiler will detect and complain about
the use of fall-through in the ,
*** statement.
Y The
statement transfers the execution control out of the current block.
Y Statements under will be executed if and only if all the case checks fail.
Y It is not necessary to place at the end of ,
*** statement. You can
even place the default block before the first case or in between cases; will work
the same regardless of its position. ùowever, making the last block is
conventional and highly recommended. f course, you can't have more than one
block in a single ,
***.
Ñ#
Loops are used for iteration purposes, i.e., doing a task multiple times (usually until a
termination condition is met)
Ñ
The most common type of loop in C# is the for loop. The basic structure of a
loop is exactly
the same as in Java and C/C++ and is:
YYYY
X
4Y
4Y
7
'Y
YYYYYYYY Y
Y YY Y
YYYYYYYYY
Y6:Y
Y
Lets see a
loop that will write the integers from 1 to 10 to the console:
YYYY
X
Y
54Y
154Y
@@'Y
YYYY6Y
YYYYYYYY*8
9
Y
YYYYYYYYYYX !Y Y-%Y Y3YY
Y
Y6:* %Y
'4Y
YYYY:Y
t the start, the integer variable i is initialized with the value of 1. The statements in the for loop
are executed while the condition (i<=10) remains true. The value of i is incremented (i++) by 1
each time the loop starts.
If you don't use the {} brackets, the statement immediate following for() will be treated as the
iteration statement. The example below is identical to the one given above:
X
Y
54Y
154Y
@@'Y
YYYY*8
9
Y
YYYYYYX !Y Y-%Y3YY
Y
Y6:* %Y
'4Y
I will again recommend you to always use {} brackets and proper indentation.
If you declare a variable in
X''s assignment, its life (scope) will only last inside the loop and
it will die after the loop body terminates (unlike some implementations of C++). ùence if you
write:
X
Y
54Y
154Y
@@'Y
6Y
YYYY*8
9
Y
YYYYYYX !Y Y-%Y3YY
Y
Y6:* %Y
'4Y
:Y
@@4YYYYYYYYY77Y
YY
terminates the loop and transfers the execution to a point just outside the for loop:
X
Y
54Y
154Y
@@'Y
6Y
YYYY
X
2'Y
YYYY6Y
YYYYYYYY
4Y
YYYY:Y
YYYY*8
9
Y
YYYYYYX !Y Y-%Y3YY
Y
Y6:* %Y
'4Y
:Y
The loop will terminate once the value of i gets greater than 5. If some statements are present
after break, break must be enclosed under some condition, otherwise the lines following break
will become unreachable and the compiler will generate a warning (in Java, it's a syntax error).
X
Y
5$4Y
14Y
@@'Y
6Y
YYYY
4YYYY77Y,
%Y*8
9
Y
YYYYYYYYYYYYYY77YX
'4Y
Y
YY
YYYY*8
9
X
'4Y
:Y
ignores the remaining part of the current iteration and starts the next iteration.
X
Y
54Y
154Y
@@'Y
6Y
YYYY
X
55'Y
YYYY6Y
YYYYYYYY
4Y
YYYY:Y
YYYY*8
9
Y
YYYYYYX !Y Y-%Y3YY
Y
Y6:* %Y
'4Y
:Y
Console.WriteLine will be executed for each iteration except when the value of i becomes 5. The
sample output of the above code is:
YYYY!Y Y-%Y3YY
Y
Y*Y
YYYY!Y Y-%Y3YY
Y
Y*Y
YYYY!Y Y-%Y3YY
Y
Y$*Y
YYYY!Y Y-%Y3YY
Y
Y&*Y
YYYY!Y Y-%Y3YY
Y
Y"*Y
YYYY!Y Y-%Y3YY
Y
Y *Y
YYYY!Y Y-%Y3YY
Y
Y*Y
YYYY!Y Y-%Y3YY
Y
Y(*Y
YYYY!Y Y-%Y3YY
Y
Y*Y
" Ñ
The general structure of a do...while loop is
YYYYY
YYYYYYYY Y
Y YY Y
YYYY,
X Y.-
'4Y
The statements under will execute the first time and then the condition is checked. The loop
will continue while the condition remains true. The program for printing the integers 1 to 10 to
the console using the ***,
loop is:
Y
54Y
Y
6Y
YYYY*8
9
Y
YYYYYYX !Y Y-%Y3YY
Y
Y6:* %Y
'4Y
YYYY
@@4Y
:Y,
X
15'4Y
" Ñ
The ,
loop is similar to the ***,
loop, except that it checks the condition before
entering the first iteration (execution of code inside the body of the loop). The general form of a
while loop is:
YYYY,
XY.-
'Y
YYYYYYYY Y
Y YY Y
c
n rray is a collection of values of a similar data type. Technically, C# arrays are a reference
type. Each array in C# is an object and is inherited from the System.rray class. rrays are
declared as:
YYYY1 Y -2YJKY1
2Y5YY
YYYYYY,Y1 Y -2J1
PYY
2K4Y
The size of an array is fixed and must be defined before using it. lthough you can use variables
to define the size of array:
YYYY
Y
PY5Y4Y
YYYY
YJKY
Y5Y,Y
J
PK4Y
It is also possible to define arrays using the values it will hold by enclosing values in curly
brackets and separating individual values with a comma:
YYYY
YJKY
Y5Y6%Y%Y$%Y&%Y:4Y
This will create an array of size 5, whose successive values will be 1, 2, 3, 4 and 5.
ùere we used the
loop to iterate through the array and the *8
9
X' method to
print each individual element of the array. Note how the indexing operator [] is used.
The above program is quite simple and efficient, but we had to hard-code the size of the array in
the
loop. s we mentioned earlier, arrays in C# are reference type and are a sub-class of the
*<
Class. This class has lot of useful properties and methods that can be applied to
any instance of an array that we define. Properties are very much like the combination of getter
and setter methods in common bject riented languages. Properties are context sensitive,
which means that the compiler can un-ambiguously identify whether it should call the getter or
setter in any given context. We will discuss properties in detail in the coming lessons.
*<
has a very useful read-only property named Length that can be used to find the
length, or size, of an array programmatically. 0sing the 9 property, the
loop in the
above program can be written as:
YYYY
X
Y
54Y
1
*9 4Y
@@'Y
YYYY6Y
YYYYYYYY*8
9
X
J
K'4Y
YYYY:Y
This version of looping is much more flexible and can be applied to an array of any size and of
any data-type.
Now we can understand the usual description of Main(). Main is usually declared as:
Y3
Y
X
YJKY
'Y
The command line arguments that we pass when executing our program are available in our
programs through an array of type
identified by the
string array.
Ñ
There is another type of loop that is very simple and useful to iterate through arrays and
collections. This is the
loop. The basic structure of a foreach loop is:
Let's now make our previous program to iterate through the array with a foreach loop:
YYYY77Y
Y YYY
Y
YQY
YYYY
Y3
Y
X'Y
YYYY6Y
YYYYYYYY77Y
YY
P
Y
YYYYYYYY77YY
YY -Y
Y
YYYYYYYY
YJKY
Y5Y6$%Y %Y%Y&%Y":4YY
YYYYYYYY77Y
Y
Y Y
Y
YYYYYYYY77YY-
YY Y
YYYYYYYY
X
Y
Y
Y
'Y
YYYYYYYY6Y
YYYYYYYYYYYY*8
9
X
'4Y
YYYYYYYY:Y
YYYY:Y
We specified the type of elements in the collection (int in our case). We declared the variable (i)
to be used to hold the individual values of the array 'integers' in each iteration.
Y The variable used to hold the individual elements of array in each iteration (i in the above
example) is readonly. You can't change the elements in the array through it. This means
that
will only allow you to iterate through the array or collection and not to
change the contents of it. If you wish to perform some work on the array to change the
individual elements, you should use a
loop.
Y
can be used to iterate through arrays or collections. By a collection, we mean
any class, struct or interface that implements the !N
interface. (Just go through
this point and re-read it once we complete the lesson describing classes and interfaces)
Y The
class is also a collection of characters (implements !N
interface
and returns
value in
property). The following code example demonstrates
this and u all the characters in the string.
YYYY
Y3
Y
X'Y
YYYY6Y
YYYYYYYY
YY5Y /
PY# 4Y
YYYYYYYY
X
YY
Y'Y
YYYYYYYY6Y
YYYYYYYYYYYY*8
9
X'4Y
YYYYYYYY:Y
YYYY:Y
º
$%!
1. ùow were we able to call the Parse method on int? int is a data type and not a class.
2. Write a program that asks a user for 5 numbers and prints the average of these numbers.
3. Write a program that asks for 5 names from the user. Find and print the name which has the
most characters in it.
Iterate through the string and find the number of letters, digits and punctuation characters in it
and u these to the console.
6. What is the basic difference between int and uint, & and &&, Camel and Pascal Notation,
do...while and while loop, break and continue statement?
$ Ñ#$&!
!
'o
#"
(
!
$
)$ *
$+
"
*(ÑM(,#
(-#Ñ(.
The architecture of .Net applications in × is designed to look very similar to traditional
Windows applications. When you compile your .Net uu , the assembly (executable .exe or
library .dll) file is generated. This file contains Win32 header, metadata and manifest and the
MSIL code of modules in the assembly. You execute your application like any other Windows
program by running the exe file. The Win32 header part of the exe invokes the CLR and passes a
pointer to the entry point of your application to CLR. Now, CLR uses the JIT compiler to
compile your code into a native executable as the functions are called. ùence, the .Net parts
(framework, CLR, JITers, MSIL) do exist, but Microsoft has made the whole process transparent
to the u .
In Visual Studio, right click the project in the solution explorer and select properties. ùere
(Common Properties - eneral - Startup), you can select which class in your program has Main()
or which class's Main is to be used as the entry point of your program.
/"
$
!
$
$ ü $$ üo.
To compile your program select Build - Build Solution or press Ctrl+Shift+B. To execute your
program, select ebug - Start Without ebug or press Ctrl+F5.
+&!
Next time, we will be discussing how to deal with classes in objects in C#: