1

The thing is I need to pass a random variable to optional parameter. Anyone? :)

Something like this:

static void Creature(string optionalParam = randomVariable) {}
Bette
  • 31
  • 4
  • 2
    No. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values – madreflection Dec 08 '20 at 20:14
  • 2
    If that's what you need, the language has supported it a lot longer with method overloading. The overload that doesn't accept a parameter calls the one that does and passes `randomVariable`. – madreflection Dec 08 '20 at 20:16
  • No code is allowed in method signature yet, only constants like with `const int DefaultValue = 10; void Method(int value = DefaultValue)`. We cannot use method call, class instance member or any other thing in parameters. The method signature is a contract, the "interface" part as in C++ virtual table of the class definition. –  Dec 08 '20 at 20:23
  • In addition to answers with overloads, this may interest you in case of multiple parameters which when null you can assign them by code: https://stackoverflow.com/questions/65162175/pass-only-the-arguments-i-want-in-a-function/65162282#65162282 –  Dec 08 '20 at 20:50

3 Answers3

2

Optional parameters are compile time constants, so you can't have a random (runtime generated) value as an optional parameter value.

What you could do, as @madreflection eludes to, is create 2 overloaded methods: one that will accept the randomValue you pass it and second one without that parameter that generates a Random number and then calls the first overload, passing that random value along. Make sense?

Dan Csharpster
  • 2,662
  • 1
  • 26
  • 50
  • I believe what the OP really meant here was "arbitrary" rather than "random". It doesn't appear to be about generating values. – madreflection Dec 08 '20 at 20:26
  • It seems to solve the OP case, perhaps you can add some code sample. –  Dec 08 '20 at 20:30
  • I think we need more information on what he's trying to do here. Since its a compile time constant, he can't have the optionalParam's default value be random or arbitrary, so his approach isn't going to work, but if we can understand his actual need here, we could suggest a different approach. – Dan Csharpster Dec 08 '20 at 20:31
  • I think the overload suggestion (thanks for the shout-out, by the way) would solve just about any such use case. If not, it would be a very narrow edge case (and quite likely a design mistake that needs to be rethought), and yes, we would need to know more. – madreflection Dec 08 '20 at 20:33
  • Okay, more info: "Need to assign a random variable (in this case a string but doesn't matter) if a parameter is not specified." – Bette Dec 08 '20 at 20:42
  • That doesn't change anything. A default value *must* be one of the options stated on the page I linked on the question. – madreflection Dec 08 '20 at 20:53
  • So in that case, don't make it an optional parameter, just make it a string parameter at the end. If nothing is passed to it, it will default to null. Then in the method, you can coalesce that parameter to a random string. There's plenty of easy ways to generate one as evidenced below. Just coalesce it to that random string method call. https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings – Dan Csharpster Dec 08 '20 at 21:00
2

You can only do this with overloads

class Foo 
{
    static Random rng = new Random();
    static string RandomString()=> $"A{rng.Next(0,1000)}";

    static void Creature() => Creature(RandomString())
    static void Creature(string argument) {}
}
JAlex
  • 1,486
  • 8
  • 19
0

You can do the below with [optional] keyword. by default optionalParam value will be Null if you do not pass anything else it will hold the passing value. I hope it will clear about optional parameter. Reference: https://www.geeksforgeeks.org/different-ways-to-make-method-parameter-optional-in-c-sharp/