What the hell! C# question

Miscellaneous Forums/General Discussion/What the hell! C# question

I have SCOURED the help & manual and cannot find any way of doing default function parameters in c#

Please tell me this is possible.

No. Use overloading.

noooooooooo! :O



I need to reprogram my brain it seems.

Function/method overloading is much more useful than default arguments.

Im sure it is. I just never used it. The example in the c# reference looks more complicated than i need.

For example how would i do this in c#:

void myfunc (int a, int b=10)
{
}

I assume i would need something like:

void myfunc (int a, 10);
void myfunc (int a, int b)
{
}

void myfunc (int a)
{
myfunc(a, 10);
}

So.. where is b initialised & given type?

what if i had a func with like 10 default params???

If you have a function taking more than 10 arguments and 10 of them taking default values, you are going the wrong way.
[edit]
For your example, here's what you need:
int add(int a, int b) { return a + b; }
int add(int a) { return add(a, 10); }
[/edit]

Alright mabe 10 is a bit many and i would rarely need more than a few, but device initialization in irrlicht for example have the required ones (resolution, depth) and then about 6 or seven other defaults for renderer, shadows and other things that dont need applying a value too unless needed.

What is the alternative if a function has to initialise many things, most of which can be standard defaults? I guess i could create a separate defaults structure for it, but thats a bit of a pain in the A. (unless there is another way you can enlighten me about)

edit for edit:
Thanks, i will experiment some more. I shall maintain my state of horror for now :)

Yeah maybe put the configuration in some sort of associative array structure and pass that along. Then let the function use what is in the array or default values otherwise. (That is, first assign default values and then overwrite with what's in the array.)

Or check if it is in the array and otherwise use default like in this pseudo code.
function setScreenMode(configuration)
{
  width  = (isSet(configuration['width']) ? configuration['width'] : DEFAULT_SCREEN_WIDTH);
  height = (isSet(configuration['height']) ? configuration['height'] : DEFAULT_SCREEN_HEIGHT);
}


Or, if you really need to pass a lot of information, such as configuration, make it as a class. (this is all C++ tho, but i'm pretty sure it works in C#).
class EngineInitialization
{
public:
    int x; // (bad practice; for sake of simplicity)
    int y;
    EngineInitialization();
}

then, in the constructor, initialize to your default params:
EngineInitialization::EngineInitialization()
{
    x = 1280;
    y = 960;
}

then, in your codez, you call to it;
int main()
{
    EngineInitialization init;
    init.y = 1024;
    engineInit( init );
}

there you go.

thanks for the help everyone.

Still, to me it seems a longwinded and slow way of just passing a few values. I guess i am just old fasioned :/


Still, to me it seems a longwinded and slow way of just passing a few values. I guess i am just old fasioned :/



Only if you're a slow typer. :)

Honestly though, creating monolithic methods with a lot of parameters isn't best practice if you're intending to develop an application that isn't brittle to change or will be maintained by more than one developer.

Generally speaking, you end up spending twice as much, if not more time maintaining applications written this way.

What seems like a lot of typing up front saves you a lot of time later. :)

D4NM4N, use (Iron)Python... ;-p

Yeah, I've actually found that when a lot of default parameters are used, it's usually a sign that something needs to be improved. I was also disappointed to find that C# doesn't do default parameters, but I guess it's better overall - it often forces me to think of more creative, easy to use methods of setting the values. However, I still use default values (in my C++ code) occasionally because it's a handy time saver when normally you'd have to waste time making an overloaded function.

Optional parameters are probably ok if you have 1 of them and you never have to change your output type for the method.

If you start to get more than that, your client code can start to get really ugly.

For instance, if a method/function has many optional parameters and in your call to it, you only want to change the very last one, you end up doing stuff like this:

* You put the default values in yourself to get to that parameter at the end you want to change (which defeats the purpose of having the default parameters in the first place). If you end up doing this, you have to bleed default value information into the client code that should only be in the method itself. Should you ever have to change the default parameter values in the method, you then have to hunt through all of the client code to change those as well .

* Sometimes the language lets you name the optional parameter variable and assign your value to that. This eliminates the above problem. However, you now have new issues. Should you want to rename your parameter variable, you have to hunt through client code again. In C#, if you start using some of the more OO-related concepts and structures such as interfaces and delegates, the method's "signature" is more important than variable names themselves. (Which is most likely the main reason why Java and C# opt for overloading rather than optionals.)

When C# was originally created, the designers behind it weren't terribly interested in "syntactical sugar" or VB6-ish behavior in the compiler. For example, C#'s implementation of generics is a truely type-safe version of it, as opposed to java's implementation which magically adds the appropriate casts to objects in the generated byte code.

C# today isn't really like that anymore. They're constantly adding short cut methods to the code parser (vars, lambda expressions vs. delegates, auto-properties, etc). I'd say optional parameters would be in that realm, so I wouldn't be surprised if it's implemented some day.

D4NM4N, use (Iron)Python... ;-p

I did look at python a while back and read it was only interpreter based, that put me right off.
Does it have a proper compiler or JIT platform?

IronPython is the .Net implementation of Python. (JIT via .Net CLR compiler)
http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython


Jython is another similar product (JIT via java byte code compiler)
http://www.jython.org/Project/


If you want improved performance in standard python, you could use Psyco. (Though beware that you have to adopt some pretty strict coding standards to gain benefit from it)
http://psyco.sourceforge.net/

Or if you want parameter lists that can be completely customized with optional arguments, preassigned arguments, and keyword arguments

such as

GraphicsInit( height = 640 ,width = 480, depth, someparam1, someparam2 = 5)

with calls such as these possible

GraphicsInit( :depth = 32 :someparam1 = 40 ) => 640,480,32,40,5

GraphicsInit( 1024, 768 :depth 16) 1024,768, 16, nil, 5

etc

use Lisp. You can even treat parameters and function calls as data to be processed with Lisp's extremely powerful macro system (nothing at all like macros in C derived languages)


Lisp, the language that all other languages are still trying to catch up to, but never will due to their clunky syntax. :p

I disagree, Lisp is horrid. There are just some things that shouldn't be implemented in other languages.

use Lisp.
Or Python.

Lisp, the language that all other languages are still trying to catch up to, but never will due to their clunky syntax. :p
More like due to them having syntax in the first place.

I disagree, Lisp is horrid.
Yay! Seriously tho' Lisp has a lot of potential, if it were at-all human readable. But then it would practically be Python, and then all the Lisp elitists couldn't sit around feeling all smug because of their ability to read Brainfuck (yes it is a real programming language, yes it is Turing complete, yes it is a stab at Lisps lack of [reasonable] syntax).

I like it so far. Apart from the problems of parameter passing, mono c# has impressed me so far. Is like a friendly C++.
It takes out all the things i hated about using C++ (especially strings. i HATE strings in C/C++)

Still i think the non inclusion of default parameters is blää but will have to get used to the overloading i guess.

The way an app (depending on libs) 'just works' on windows and linux is fantastic and that is my primary interest in it.

use Lisp.
Hahaha not a chance!