Parameter Modifiers in C#

 

In C++, we can pass by value, reference (pointers are also there) when we need to pass some data to a C++ function.

In the case of C# also we’ve the same facilities.(Pointers are still there). They’re named as ref, out and params.

Let’s take a look at ref keyword. As the name indicates, “ref” stands for reference. If the formal parameter (ref variable) modified inside the function, it will be also get reflected in the actually parameter. (Same as C++ reference). In C#, the object must be initialized before passing as a reference. The sytanx is clean in C# because we’re specifying the parameter modifier at both function definition and at the time of usage.

[sourcecode language='C#']
static void Foo(ref int nData)
{
nData++;
}
static void Main(string[] args)
{
int x = 10;
Foo(ref x);
int y;
Foo(ref y); // ERROR: Uninitialized variables
}
[/sourcecode]
Let’s take a look at out keyword. Out behaves similar to ref keyword but there’s no need to initialize the object passing to function. Even the object is initialized, inside the function, we’ve to re-initialize object. We can’t exclude this step.

In C++, there’s no constraint to use the reference variable inside the function. We can omit with/ without conditional statements. But in C# if you use out keyword, you will have to initialize the object inside the calling function. If you want the same behavior of C++ reference, it’s better to use ref keyword.

Ref and out keywords are same at the compilation time but behaves different at run time. So that you can’t overload “ref” and “out” with similar function signature.

[sourcecode language='C#']
static void Foo(ref Math m){}
static void Foo(out Math m){} // ERROR: can’t overload with ref and out keyword
[/sourcecode]

params are allows to pass pass arbitrary number of parameters to this function. Params keyword has the following constraints. There should be only one params argument as function parameter and it should be appeared as the last parameter of a function(or no other parameters can be passed after a param variable)
See the same sample from MSDN

[sourcecode language='c#']
using System;
public class MyClass
{
public static void UseParams(paramsparams int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, ‘a’, “test”);

// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
[/sourcecode]

 

Template Arugment Deducing issue With Visual C++ 6

 

One of my team mate was trying to “templatize” the function she wrote. She Immediately came to me and shown a sample piece of code where she failed to get the input from the console window.

The code was as follows

template
void Foo(int nArraySize)
{
T* pArray = new T[nArraySize];
int nIdx;
cout<<”Enter the items to be”<<endl;
for ( nIdx = 0;nIdx < nArraySize;nIdx++)
{
cin>>pArray[nIdx];
}

cout<<”The array contains”<<endl;
for ( nIdx = 0; nIdx < nArraySize;nIdx++)
{
cout<<pArray[nIdx]<<endl;
}
}
int main()
{
int x;
cin >> x;
switch( x )
{
case 1:
Foo( 3 );
break;
case 2:
Foo ( 3 );
break;
default:
break;
}
}

I’m calling the functions from Switch case, not that the type of the argument is not passing. When I compiled the source using Visual C++ 6, I got following error

1
Enter the items
10.233
The array contains
-858993460
0
10

After entering 10.233, error occurred in the stream input. This exits the for loop which reads the elements for the array.

The root cause of this issue is basic_istream::operator << () function replaced with the int version irrelevant to the type of the function we’ve instantiated.


Ok let’s try with more cases to prove the hypothesis add one more switch case

case 3:
Foo ( 3 );
break;.

Now the see the output

1
Enter the items
123.23
The array contains
1
2
3

In the above test, I still tried the float version. Instead compiler still taking inputs as char which is the last instantiation of function inside the switch case.

I believe this as a problem of Microsoft Visual C++ 6 compiler. There’s no meaning in talking about the unsupported compiler and IDE.
Microsoft Corrected this issue in their latest compiler Visual C++ 2008 (not sure about VS 2005 and 2003 )
I also tried the same source code with other compilers like Intel C++ Compiler and Dev C++ compiler. It’s working perfectly fine. The problem is only with the old version of Visual C++ compiler.
Please feel free to share your view points and hypothesises.

 

Learning .NET with C#

 

Learning .NET becomes an important in my professional life. I’m basically a native programmer and really comfortable with C++/MFC/Win32 World. But seems it’s really necessary to learn the booming technologies and languages because I’m basically a Software Engineer.
I believe choosing a programming language is the end side of Software engineering. We can architect a new concept/idea and design it well. Choosing the programming languages is consists of many factors. It depends upon the domain we’re representing, availability of resources, cost for the compilers and other related development product, support & maintenance factor etc… Still the programming language has some impact on particular domains.
I could not say that I can program only with C/C++. Rather I’m comfortable with C/C++.even the domain switching is not so easy, I believe we’ve to keep the pace with the industry. Many of my friends shared their story, if they get a U.S Project, they will be asking for either Java or C# platform. I don’t know whether this is a global trend or not.
One of our Program Manager Anil, came forward to take up the task to teach the basic C# with his short trainings.  Being a Program Manager he’s much interested in the new technologies always keeping his technical skills up-to-date with current trends.
Anyway I started learning this managed things (I really hate that I don’t have any control over the memory). Hope to see some upcoming beginners posts on C# and .NET.
There are lots of tutorials available to learn C#. Many excellent books like CLR via C#, Pro C# 2008 and .NET 3.5 Platform. So I’m not gonna some in-depth posts on C# but still I try to provide some information comparing C++