1

I understand in C# there is such thing as named parameters so that is a bit misleading. The question I have is what they should be called in general.

In my time using libraries in other languages I've sometimes run across predefined values that can be used within functions of a class.

object.myFunc(SPECIAL_VALUE);

It's usually named in all capitol letters and is a very specific value within the function. I figure it behaves much like a public static string but when I use this idea intellisense doesn't pick this up. Can someone inform me of the general name of this kind of parameter option and if they exist, pros and cons of using such a thing.

Thanks,

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • Possible duplicate of: [How to define named Parameters C#](http://stackoverflow.com/questions/12056639/how-to-define-named-parameters-c-sharp) – Aly Elhaddad Apr 01 '16 at 20:24
  • 1
    Capitalizing identifiers like that is a C language convention. C# does not have a pre-processor that permits defining values. Use a const or a readonly variable or an enum. Avoid looking for a single name for these three very different kinds of identifiers. – Hans Passant Apr 01 '16 at 21:38
  • Are you talking about the default values for parameters like `f(int x=1)` which are used when named parameters aren't specified? – John Alexiou Apr 02 '16 at 00:06

3 Answers3

0

Normally, those kind of constants in C# are declared and cased like this...

public const String MyConstantThingHere = "Some value";

In Java, it would be more like...

public static final String MY_CONSTANT_THING_HERE = "Some value";

I would simply call these public class constants. See HERE.

Also, for reference: http://www.dofactory.com/reference/csharp-coding-standards

You can also use static imports to refer to them...

See here: static imports in c#

Community
  • 1
  • 1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
0

I'm not really sure, what you mean. The concept of named parameters in C# is shown in this example:

namespace Testconsole {

public class obj {
    public int myFunc(int value1, int value2 = 4, int value3 = 8) {
        return value1 + value2 + value3;
    }
}

class Program {
    static void Main(string[] args) {
        obj adder = new obj();
        Console.WriteLine(adder.myFunc(value1: 1, value3: 1));
        Console.ReadLine();
    }
}
}

Testconsole returns 6: value1 is set with '1', value3 is set with '1', value2 is missing and therefore filled with default value '4'.

Also it is possible to pass a public static String into a Function:

namespace Testconsole {

    public class obj {
        public String myFunc(String mySpecialValue) {
            return "now I " + mySpecialValue;
        }
    }

    class Program {
        public static String SPECIAL_VALUE = "got it";

        static void Main(string[] args) {
            obj Learn = new obj();
            Console.WriteLine(Learn.myFunc(SPECIAL_VALUE));
            Console.ReadLine();
        }
    }
}

Console output:

Now I got it.
termigrator
  • 159
  • 3
  • 11
0

What you are referring to is a "constant." They serve two purposes:

They ensure that a value that has some meaning (a "special value", as you said) is declared in one place and re-used so that if it changes we change it in one place.

They make our code more readable. For example, we might want two always display the top four items from a list on a web page. And we might have an input that should always be four characters in length. If other people just read our code and see the number 4 here and there it might not be apparent what that number 4 means. We want our code to be as easy to understand as possible.

So when we declare

const int NUMBER_OF_ITEMS_TO_DISPLAY = 4

or

const string MAX_INPUT_LENGTH_SETTINGS_KEY = "maximumInputLength"

Then when we use those constants in our code other people can tell what they mean.

The opposite of using constants is called using "magic numbers" or "magic strings." They just appear in our code without context or explanation. It might just be value we arbitrarily picked, but declaring it as a constant gives it context and meaning.

The capital letters aren't required. They're just a convention that makes constants easy to recognize.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62