0% found this document useful (0 votes)
14 views3 pages

Parameters

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Parameters

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Parameters(8-Feb-2024)

Parameters are used to pass values or variable references to methods.


The parameters of a method get their actual values from the arguments that are specified when the
method is invoked.

There are four kinds of parameters:

i. value parameters,
ii. reference parameters,
iii. output parameters, and
iv. parameter arrays.

A value parameter is used for passing input arguments.


 A value parameter corresponds to a local variable that gets its initial value from the argument that was
passed for the parameter.
 Modifications to a value parameter don't affect the argument that was passed for the parameter.
 Value parameters can be optional, by specifying a default value so that corresponding
arguments can be omitted.

A reference parameter is used for passing arguments by reference.


 The argument passed for a reference parameter must be a variable with a definite value.
 During execution of the method, the reference parameter represents the same storage location as the
argument variable.
 A reference parameter is declared with the ref modifier. The following example shows the use of ref
parameters

An output parameter is used for passing arguments by reference.


 It's similar to a reference parameter, except that it doesn't require that you explicitly assign a value to
the caller-provided argument.
 An output parameter is declared with the out modifier.
 The following example shows the use of out parameters

A parameter array permits a variable number of arguments to be passed to a method.


 A parameter array is declared with the params modifier.
 Only the last parameter of a method can be a parameter array, and the type of a parameter array
must be a single-dimensional array type. The Write and WriteLine methods of the System.Console
class are good examples of parameter array usage.
 They're declared as follows.
 Within a method that uses a parameter array, the parameter array behaves exactly like a
regular parameter of an array type.
 However, in an invocation of a method with a parameter array, it's possible to pass either a single
argument of the parameter array type or any number of arguments of the element type of the
parameter array.
 In the latter case, an array instance is automatically created and initialized with the given arguments.
This example

Iteration statements - for , foreach , do , and while

The iteration statements repeatedly execute a statement or a block of statements.


 The for statement executes its body while a specified Boolean expression evaluates to true .
 The foreach statement enumerates the elements of a collection and executes its body for each
element of the collection
 The do statement conditionally executes its body one or more times.
 The while statement conditionally executes its body zero or more times.
 At any point within the body of an iteration statement, you can break out of the loop
using the break statement.
 You can step to the next iteration in the loop using the continue statement.

the elements of the for statement:

The initializer section that is executed only once, before entering the loop. Typically, you declare and
initialize a local loop variable in that section. The declared variable can't be accessed from outside the for
statement.
The initializer section in the preceding example declares and initializes an integer counter variable: int i = 0

The condition section that determines if the next iteration in the loop should be executed.
If it evaluates to true or isn't present, the next iteration is executed; otherwise, the loop is exited.
The condition section must be a Boolean expression.
The condition section in the preceding example checks if a counter value is less than three: i < 3;

The iterator section that defines what happens after each execution of the body of
the loop.
The iterator section in the preceding example increments the counter: i++
The body of the loop, which must be a statement or a block of statements.

 The iterator section can contain zero or more of the following statement expressions,
separated by commas:
1. prefix or postfix increment expression, such as ++i or i++
2. prefix or postfix decrement expression, such as --i or i--
3. assignment
4. invocation of a method
5. await expression
6. creation of an object by using the new operator

If you don't declare a loop variable in the initializer section, you can use zero or more of
the expressions from the preceding list in the initializer section as well.
The following example shows several less common usages of the initializer and iterator sections:
 assigning a value to an external variable in the initializer section, invoking a method in
both the initializer and the iterator sections, and changing the values of two variables in
the iterator section:

All the sections of the for statement are optional. For example, the following code
defines the infinite for loop:
C#
for ( ; ; )
{
//...
}
The foreach statement

The foreach statement executes a statement or a block of statements for each element in an instance of
the type that implements
 the System.Collections.IEnumerable or
 System.Collections.Generic.IEnumerable<T> interface, as the following example shows:
Started time:11:30 A.M---9-Feb 2024
The foreach statement isn't limited to those types. You can use it with an instance of
any type that satisfies the following conditions:
A type has the public parameterless GetEnumerator method. The GetEnumerator
method can be a type's extension method.
The return type of the GetEnumerator method has the public Current property and
the public parameterless MoveNext method whose return type is bool .
If the enumerator's Current property returns a reference return value ( ref T where T is
the type of a collection element), you can declare an iteration variable with the ref or
ref readonly modifier, as the following example shows:

..Incomplete : await foreach

var keyword:

can use the var keyword to let the compiler infer the type of an iteration variable in
the foreach statement.
Note: Type of var can be inferred by the compiler as a nullable reference type,
depending on whether the nullable aware context is enabled and whether the type
of an initialization expression is a reference type.

You can also explicitly specify the type of an iteration variable

You might also like