Variables and Parameters in C#

A variable represents a storage location that has a modifiable value. A variable can be a local variable, parameter, field or array element.The stack and the heap are the places where variables and constants reside. Each has very different lifetime semantics. The stack is a block of memory for storing local variables and parameters. The stack automatically grows and shrinks as a function is entered and exited.

static int Factorial (int x)
{
if (x == 0) return 1;
return x * Factorial (x-1);
}

This method is recursive, meaning that it calls itself. Each time the method is entered, a new int is allocated on the stack, and each time the method exits, the int is deallocated.

The heap is a block of memory in which objects reside. Whenever a new object is created, it is allocated on the heap, and a reference to that object is returned. During a program's execution, the heap starts filling up as new objects are created. The runtime has a garbage collector that periodically deallocates objects from the heap, so your computer does not run out of memory. An object is eligible for deallocation as soon as nothing references it. In the following code, the StringBuilder object is created on the heap, while the sb reference is created on the stack:

static void Test()
{
StringBuilder sb = new StringBuilder();
Console.WriteLine (sb.Length);
}

After the Test method finishes, sb pops off the stack, and the StringBuilder object is no longer referenced, so it becomes eligible for garbage collection.Value type instances live wherever the variable was declared. If the instance was declared as a field within an object, or as an array element, that instance lives on the heap. You can't explicitly delete objects in C#, as you can in C++. An unreferenced object is eventually collected by the garbage collector.The heap is also used to store static fields and constants. Unlike objects allocated on the heap (which can get garbage collected), these will live until the application domain is torn down.

How to definite Assignment ?
C# enforces a definite assignment policy. In practice, this means that outside of an unsafe context, it's impossible to access uninitialized memory. Definite assignment has three implications:
  • Local variables must be assigned a value before they can be read.
  • Function arguments must be supplied when a method is called.
  • All other variables (such as fields and array elements) are automatically initialized by the runtime.
For example, the following code results in a compile-time error:

static void Main()
{
int x;
Console.WriteLine (x); // compile-time error
}

Fields and array elements are automatically initialized with the default values for their type. The following code outputs 0 because array elements are implicitly assigned to their default values:

static void Main()
{
int[] ints = new int[2];
Console.WriteLine (ints[0]); // 0
}

The following code outputs 0 because fields are implicitly assigned to a default value:

class Test
{
static int x;
static void Main() { Console.WriteLine (x); } // 0
}

How to set Parameters ?
A method has a sequence of parameters. Parameters define the set of arguments that must be provided for that method. In this example, the method Foo has a single parameter named p, of type int:

static void Foo (int p)
{
p = p + 1; // increment p by 1
Console.WriteLine(p); // write p to screen
}
static void Main() { Foo(8); }

You can control how parameters are passed with the ref and out modifiers.
How to pass arguments by value ?
By default, arguments in C# are passed by value, which is by far the most common case. This means a copy of the value is created when passed to the
method:

class Test
{
static void Foo (int p)
{
p = p + 1; // Increment p by 1
Console.WriteLine (p); // Write p to screen
}

static void Main( )
{
int x = 8;
Foo (x); // Make a copy of x
Console.WriteLine (x); // x will still be 8
}
}

Assigning p a new value does not change the contents of x because p and x reside in different memory locations.Passing a reference type object by value copies the reference, but not the object. In the following example, Foo sees the same StringBuilder object that Main instantiated, but has an independent reference to it. In other words, sb and fooSB are separate variables that reference the same StringBuilder object:

class Test
{
static void Foo (StringBuilder fooSB)
{
fooSB.Append ("test");
fooSB = null;
}

static void Main( )
{
StringBuilder sb = new StringBuilder( );
Foo (sb);
Console.WriteLine (sb.ToString( )); // test
}
}

Because fooSB is a copy of a reference, setting it to null doesn't make sb null. (If, however, fooSB was declared and called with the ref modifier, sb would become null.)

Note:
  1. You can't explicitly delete objects in C#, as you can in C++. An unreferenced object is eventually collected by the garbage collector.

  2. A parameter can be passed by reference or by value, regardless of whether the parameter type is a reference type or a value type.

0 comments:

Post a Comment

 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.