optional-parameters

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

南楼画角 提交于 2020-01-08 14:36:15
问题 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? 回答1: 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

C# 4 optional parameter

廉价感情. 提交于 2020-01-05 08:36:33
问题 Is C# 4 optional parameter implementation the same as VB.NET, the optional parameter is compiled on the call site(can cause versioning problems)? 回答1: Yes. 回答2: According to SamNg, C#'s default arguments are is compiled at the call site, similar to default parameters in C++. Yes, it would cause versioning problems. However, optional parameters should be used where it makes sense. In many cases, this means passing null or default-constructed class to a method or constructor. 来源: https:/

Can I use Named and Optional Arguments in ironpython

て烟熏妆下的殇ゞ 提交于 2020-01-04 03:24:07
问题 I hope to load .net dll in ironpython. But one of static functions in .net dll, has some Named and Optional Arguments. like, Draw(weight:w,height:h, Area=1) Only can I use full arguments? 回答1: Named and optional parameters are fully supported. .NET has had these for a long time for VB.NET support and so IronPython has supported that same way to do them since the beginning. The new C# syntax maps to the same underlying metadata as the old VB support. For calling you use f(x = 42) which is

Why can't I use string.Empty as an optional parameter contrary to empty quotation marks? [duplicate]

元气小坏坏 提交于 2020-01-03 11:02:33
问题 This question already has answers here : Why isn't String.Empty a constant? (4 answers) Closed 5 years ago . I'm refining my code and noticed that at some places I had optional parameters with the default value of an empty string. I changed that to a default value from the class for empty string and guess what! Apparently there's a difference between empty quotation marks and string.Empty . What the duck?! (typo intended) private void Khaboom(String parameter = "") { ... } private void

Why can't I use string.Empty as an optional parameter contrary to empty quotation marks? [duplicate]

混江龙づ霸主 提交于 2020-01-03 11:02:07
问题 This question already has answers here : Why isn't String.Empty a constant? (4 answers) Closed 5 years ago . I'm refining my code and noticed that at some places I had optional parameters with the default value of an empty string. I changed that to a default value from the class for empty string and guess what! Apparently there's a difference between empty quotation marks and string.Empty . What the duck?! (typo intended) private void Khaboom(String parameter = "") { ... } private void

Why can't I use string.Empty as an optional parameter contrary to empty quotation marks? [duplicate]

眉间皱痕 提交于 2020-01-03 11:02:06
问题 This question already has answers here : Why isn't String.Empty a constant? (4 answers) Closed 5 years ago . I'm refining my code and noticed that at some places I had optional parameters with the default value of an empty string. I changed that to a default value from the class for empty string and guess what! Apparently there's a difference between empty quotation marks and string.Empty . What the duck?! (typo intended) private void Khaboom(String parameter = "") { ... } private void

Advanced Optional Parameters (c#) [duplicate]

廉价感情. 提交于 2020-01-03 08:42:15
问题 This question already has answers here : Setting the default value of a C# Optional Parameter (3 answers) Passing an empty array as default value of an optional parameter [duplicate] (3 answers) Closed 6 years ago . The below code would be quite cool if it worked. However, I can't get it to compile, so I'm assuming this isn't going to work in any form? public void foo(char[] bar = new char[]{'a'}) { } The next-best option is to just do public void foo(char[] bar = null) { if (bar==null) bar =

Accepting an optional parameter only as named, not positional

佐手、 提交于 2020-01-03 07:22:08
问题 I'm writing a PowerShell script that's a wrapper to an .exe. I want to have some optional script params, and pass the rest directly to the exe. Here's a test script: param ( [Parameter(Mandatory=$False)] [string] $a = "DefaultA" ,[parameter(ValueFromRemainingArguments=$true)][string[]]$ExeParams # must be string[] - otherwise .exe invocation will quote ) Write-Output ("a=" + ($a) + " ExeParams:") $ExeParams If I run with the a named param, everything is great: C:\ > powershell /command \temp

How to work with Firebase without allowing optional values

流过昼夜 提交于 2020-01-02 23:44:17
问题 I'm new to iOS development and I understand that allowing optional values when an object is initialized is not a 'good citizen' technique. That being said, I've read that it is good practice to always have values set, like this: class Item{ var name: String var color: String init(name: String, color: String) { self.name = name self.color = color } } This looks nice and tidy but how can I do something like that working with Firebase? Look what I've got so far: private func loadPosts(){

Odd method behaviour with optional first hash parameter and keyword_args

点点圈 提交于 2020-01-02 05:40:41
问题 I have the following method: def test(first_param = nil, keyword_arg: nil) puts "first_param: #{first_param}" puts "keyword_arg: #{keyword_arg}" end All the following calls do what I expect them to do: test(:something) #=> first_param: something # keyword_arg: test(nil, keyword_arg: :keyword_arg) #=> first_param: # keyword_arg: keyword_arg test({ first_param: :is_a_hash }, keyword_arg: :is_still_working) #=> first_param: {:first_param=>:is_a_hash} # keyword_arg: is_still_working But omitting