A simple function call

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi,

Is there way in C# (or C#.net) to just define a function without having to
initialize the class every time. For instance a function that takes a
number, adds 2 and multiplies by 10. I know how to do it in a class. But
the problem with classes is that it has to be instantiated every time.

So for example in my code I want to be able to just call
xx = myfunctionname(2);

Thank you
Maziar A.
 
Maziar said:
Hi,

Is there way in C# (or C#.net) to just define a function without
having to initialize the class every time. For instance a function
that takes a number, adds 2 and multiplies by 10. I know how to do it
in a class. But the problem with classes is that it has to be
instantiated every time.


public class Test
{
public static int MyFunc(int i)
{
return (i+2)*10;
}
}


public class MainClass
{
public static void Main()
{
System.Console.WriteLine(Test.MyFunc(1));
}
}



--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
Is there way in C# (or C#.net) to just define a function without having to
initialize the class every time. For instance a function that takes a
number, adds 2 and multiplies by 10. I know how to do it in a class. But
the problem with classes is that it has to be instantiated every time.

No they don't. Yes, you must have a class. No it doesn't have to be
instantiated.

namespace myNameSpace
{
public class MyClass
{
public static int MyMethod(int val)
{
return val + 10;
}

};
}

call it like this:

int x = MyClass.MyMethod(3);


HTH
Brian W
 
Back
Top