optional-parameters

Setting the default value of a C# Optional Parameter

≡放荡痞女 提交于 2019-11-26 17:47:49
问题 Whenever I attempt to set the default value of an optional parameter to something in a resource file, I get a compile-time error of Default parameter value for 'message' must be a compile-time constant. Is there any way that I can change how the resource files work to make this possible? public void ValidationError(string fieldName, string message = ValidationMessages.ContactNotFound) In this, ValidationMessages is a resource file. 回答1: No, you will not be able to make the resource work

Should you declare methods using overloads or optional parameters in C# 4.0?

ぃ、小莉子 提交于 2019-11-26 17:23:00
I was watching Anders' talk about C# 4.0 and sneak preview of C# 5.0 , and it got me thinking about when optional parameters are available in C# what is going to be the recommended way to declare methods that do not need all parameters specified? For example something like the FileStream class has about fifteen different constructors which can be divided into logical 'families' e.g. the ones below from a string, the ones from an IntPtr and the ones from a SafeFileHandle . FileStream(string,FileMode); FileStream(string,FileMode,FileAccess); FileStream(string,FileMode,FileAccess,FileShare);

Check inside method whether some optional argument was passed

こ雲淡風輕ζ 提交于 2019-11-26 17:13:15
问题 How do I check if an optional argument was passed to a method? public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint was passed) return; } Another approach is to use Nullable<T>.HasValue (MSDN definitions, MSDN examples): int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } 回答1: Well, arguments are

Optional delegates in C# [duplicate]

好久不见. 提交于 2019-11-26 16:43:55
问题 This question already has an answer here: Default value on generic predicate as argument 3 answers This is a simple example of two extension methods overloads public static class Extended { public static IEnumerable<int> Even(this List<int> numbers) { return numbers.Where(num=> num % 2 == 0); } public static IEnumerable<int> Even(this List<int> numbers, Predicate<int> predicate) { return numbers.Where(num=> num % 2 == 0 && predicate(num)); } } I'd like to be able to merge them into one, by

How do I handle null or optional dll struct parameters in C#

纵然是瞬间 提交于 2019-11-26 16:40:51
How do I deal with optional struct arguments in dll methods called from C# using pinvoke? For example, the lpSecurityAttributes parameter here should be passed null when absent. The correct way of passing struct 's seems to be using ref , but it cannot have optional parameters, or take null in general. What ways are there to achieve this? You have a few options 1) Use a class instead of a struct I think this method is the easiest. Simply declare the struct as a class : [StructLayout(LayoutKind.Sequential)] public class CStruct { //member-list } and then declare your method: [DllImport("mydll

Default value of function parameter

筅森魡賤 提交于 2019-11-26 15:45:42
1. int Add (int a, int b = 3); int Add (int a, int b) { } 2. int Add (int a, int b); int Add (int a, int b = 3) { } Both work; which is the standard way and why ? If you put the declaration in a header file, and the definition in a separate .cpp file, and #include the header from a different .cpp file, you will be able to see the difference. Specifically, suppose: lib.h int Add(int a, int b); lib.cpp int Add(int a, int b = 3) { ... } test.cpp #include "lib.h" int main() { Add(4); } The compilation of test.cpp will not see the default parameter declaration, and will fail with an error. For this

Passing an empty array as default value of an optional parameter [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 15:25:47
This question already has an answer here: Setting the default value of a C# Optional Parameter 3 answers How does one define a function that takes an optional array with an empty array as default? public void DoSomething(int index, ushort[] array = new ushort[] {}, bool thirdParam = true) results in: Default parameter value for 'array' must be a compile-time constant. You can't create compile-time constants of object references. The only valid compile-time constant you can use is null , so change your code to this: public void DoSomething(int index, ushort[] array = null, bool thirdParam =

How to pass a nullable type to a P/invoked function [duplicate]

北慕城南 提交于 2019-11-26 14:28:46
问题 This question already has an answer here : How do I handle null or optional dll struct parameters in C# (1 answer) Closed last year . I have a few p/invoked functions (but I'm rewriting my code at the moment so I'm tidying up) and I want to know how to use/pass a nullable type as one of the parameters. working with int types isn't a problem but given the following: [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr SetupDiGetClassDevs(ref Guid

How does one make an optional closure in swift?

こ雲淡風輕ζ 提交于 2019-11-26 12:38:54
问题 I\'m trying to declare an argument in Swift that takes an optional closure. The function I have declared looks like this: class Promise { func then(onFulfilled: ()->(), onReject: ()->()?){ if let callableRjector = onReject { // do stuff! } } } But Swift complains that \"Bound value in a conditional must be an Optional type\" where the \"if let\" is declared. 回答1: You should enclose the optional closure in parentheses. This will properly scope the ? operator. func then(onFulfilled: ()->(),

maven command line how to point to a specific settings.xml for a single command?

混江龙づ霸主 提交于 2019-11-26 11:55:15
问题 Is it possible to point to a specific settings file in order to override the default settings.xml being used by maven for a single command? Example: mvn clean install -Dparam # -> pass specific settings file path as param to override default \"home/.m2/settings.xml\" 回答1: You can simply use: mvn --settings YourOwnSettings.xml clean install or mvn -s YourOwnSettings.xml clean install 来源: https://stackoverflow.com/questions/25277866/maven-command-line-how-to-point-to-a-specific-settings-xml-for