Variables and Parameters

A variable is a storage location that has modifiable value
A variable can be local, parameter (value, ref, out), field (instance or static) or array element

Stack and Heap
It is a place where variables and constants reside

  1. Stack
    Block of memory for storing local variable and parameters. Logically grows and shrinks as the function enters and exits.
  2. Heap
    Is the memory where objects are stored. Whenever a new object is created, it is allocated on the heap, and a reference to that object is returned. It also stores static fields and during run time the garbage collector deallocates objects to free memory

Definite Assignment
It is impossible to access uninitialized memory

Rules for Assignment

  1. Local variables must be assigned a value before they can be read.
  2. Function arguments must be supplied when a method is called (unless marked
    as optional
  3. All other variables (such as fields and array elements) are automatically initialized
    by the runtime.

Default Values
All type instance have default value

Type Default Value
All reference types null
All numeric and enum types 0
char type ‘\0’
bool type false

Use default keyword to get the default value, mostly useful with generics

decimal d = default (decimal);

Parameters

Sequence of arguments which needs to be provided for that method

you can control how parameters are passed with ref and out modifiers

Parameter modifier Passed by Assignment
NONE Value going in
ref Reference going in
out Reference going out

Passing arguments by value

  • For default arguments are passed by value (create a copy)
  • For reference only reference is copied not object

The ref modifier

  • Pass by reference
  • Essential in implementing a swap method
class Test
{
static void Foo (ref int p)
{
p = p + 1; // Increment p by 1
Console.WriteLine (p); // Write p to screen
}
static void Main()
{
int x = 8;
Foo (ref x); // Ask Foo to deal directly with x
Console.WriteLine (x); // x is now 9
}
}

The out modifier

  1. Need not be assigned before going into the function
  2. Must be assigned before it comes out of the function
  3. Used to get multiple return values back from a method
class Test
{
static void Split (string name, out string firstNames,out string lastName)
{
      int i = name.LastIndexOf (' ');
      firstNames = name.Substrings (0, i);
      lastName = name.Substrings (i + 1);
}

static void Main()
{
     string a, b;
     Split ("Stevie Ray Vaughan", out a, out b);
     Console.WriteLine (a); // Stevie Ray
     Console.WriteLine (b); // Vaughan
}

}

The params modifier

  • Method can accept any number of arguments of a particular type
  • Parameter type must be declared as an array
class Test
{
static int Sum (params int[] ints)
{
int sum = 0;
for (int i = 0; i < ints.Length; i++)
sum += ints[i]; // Increase sum by ints[i]
return sum;
}
static void Main()
{
int total = Sum (1, 2, 3, 4);
Console.WriteLine (total); // 10
}
}

or

int total = Sum (new int[] { 1, 2, 3, 4 } ); 

Optional parameters
A parameter is optional if it specifies a default value in its declaration

void Foo (int x = 23) { Console.WriteLine (x); }

Optional parameters may be omitted when calling the method:
Foo(); // 23


Mandatory parameters must occur before optional parameters in both the method
declaration and the method call (the exception is with params arguments, which still
always come last).

Named arguments

Instead of identifying argument by position , argument can be identified by name and in any order.

void Foo (int x, int y) { Console.WriteLine (x + ", " + y); }
void Test()
{
Foo (x:1, y:2); // 1, 2
}

You can mix named and positional arguments:
Foo (1, y:2);

Named arguments are particularly useful in conjunction with optional parameters.

void Bar (int a = 0, int b = 0, int c = 0, int d = 0) { ... }
Bar (d:3);

var—Implicitly Typed Local Variables

var can be used if he compiler is able to infer the type implicitly typed variables are statically typed

Leave a Reply

Your email address will not be published. Required fields are marked *