C# .NET - How to pass var variable to the function as parameter in c#?

Asked By Eswaran Radhakrishnan on 14-Oct-09 08:24 AM
Hi all,

How to pass var variable to the function as parameter in c#?

Thanks
r.e

reply

Deepak Sonawane replied to Eswaran Radhakrishnan on 14-Oct-09 08:27 AM
Hi,

In C#, parameters can be passed either by value or by reference. Passing parameters by reference allows function members (methods, properties, indexers, operators, and constructors) to change the value of the parameters and have that change persist. To pass a parameter by reference, use the ref or out keyword. For simplicity, only the ref keyword is used in the examples of this topic. For information on the difference between ref and out, see http://msdn.microsoft.com/en-us/library/14akc2c7%28VS.71%29.aspx, http://msdn.microsoft.com/en-us/library/t3c3bfhx%28VS.71%29.aspx, and http://msdn.microsoft.com/en-us/library/szasx730%28VS.71%29.aspx.

This topic includes the following sections:

  • http://msdn.microsoft.com/en-us/library/0f66670z%28VS.71%29.aspx#vclrfpassingmethodparameters_valuetypes
  • http://msdn.microsoft.com/en-us/library/0f66670z%28VS.71%29.aspx#vclrfpassingmethodparameters_referencetypes
For more detail check this link:
http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx

pass var variable to the function as parameter in c#

DL M replied to Eswaran Radhakrishnan on 14-Oct-09 08:50 AM
you can visit this articles
http://csharp.net-tutorials.com/basics/function-parameters/

Re

Ravenet Rasaiyah replied to Eswaran Radhakrishnan on 14-Oct-09 10:05 AM
Hi

In Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:

Here more details

http://msdn.microsoft.com/en-us/library/bb383973.aspx

thank you
You can use the IEnumerable<T> object,
[)ia6l0 iii replied to Eswaran Radhakrishnan on 14-Oct-09 02:12 PM
Like:

var inputVar = GetData();
TestMethod(inputVar);

private static void TestMethod(IEnumerable<string> input)
{
`....
}