ref-parameters

How to convert recursive procedure with side effects on ref param to recursive function returning a list?

夙愿已清 提交于 2019-12-24 05:08:09
问题 It seems every time I go to write a recursive function I end up making it return void and using a ref parameter. I'd much rather be able to write a function that just returns a result list. Apologies if the answer is very simple - for some reason it elludes me. Here's the code I have now: public static void GetResrouces(string startURL, ref List<XDocument> result) { var doc = XDocument.Parse(GetXml(startURL)); // GetXml ommitted - returns xml string var xs = new XmlSerializer(typeof

Why doesn't 'ref' and 'out' support polymorphism?

徘徊边缘 提交于 2019-12-17 02:09:12
问题 Take the following: class A {} class B : A {} class C { C() { var b = new B(); Foo(b); Foo2(ref b); // <= compile-time error: // "The 'ref' argument doesn't match the parameter type" } void Foo(A a) {} void Foo2(ref A a) {} } Why does the above compile-time error occur? This happens with both ref and out arguments. 回答1: ============= UPDATE: I used this answer as the basis for this blog entry: Why do ref and out parameters not allow type variation? See the blog page for more commentary on

Passing an explicit cast as a ref parameter (C#)

泪湿孤枕 提交于 2019-12-10 12:46:54
问题 I have a class that is mostly a wrapper for a big array and some associated housekeeping. I have a function that takes a ref parameter. When I pass an instance of the class into the function, I want the array to get sent. I considered explicit casts. Let's say I have some function that has a byte[] ref parameter. public void SomeFunction(ref byte[] someBytes); And that I have some class with an overloaded explicit cast. class SomeClass { byte[] someBytes; public static explicit operator byte[

Why is an out parameter not allowed within an anonymous method?

蓝咒 提交于 2019-12-04 16:49:22
问题 This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this 回答1: In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as

When is the value of a C# 'out' or 'ref' parameter actually returned to the caller?

前提是你 提交于 2019-12-04 16:44:20
问题 When I make an assignment to an out or ref parameter, is the value immediately assigned to the reference provided by the caller, or are the out and ref parameter values assigned to the references when the method returns? If the method throws an exception, are the values returned? For example: int callerOutValue = 1; int callerRefValue = 1; MyMethod(123456, out callerOutValue, ref callerRefValue); bool MyMethod(int inValue, out int outValue, ref int refValue) { outValue = 2; refValue = 2;

Why is an out parameter not allowed within an anonymous method?

£可爱£侵袭症+ 提交于 2019-12-03 11:40:00
This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this JaredPar In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as ref parameters. The problem here originates with the effect of using a value declared outside the

When is the value of a C# 'out' or 'ref' parameter actually returned to the caller?

▼魔方 西西 提交于 2019-12-03 10:42:24
When I make an assignment to an out or ref parameter, is the value immediately assigned to the reference provided by the caller, or are the out and ref parameter values assigned to the references when the method returns? If the method throws an exception, are the values returned? For example: int callerOutValue = 1; int callerRefValue = 1; MyMethod(123456, out callerOutValue, ref callerRefValue); bool MyMethod(int inValue, out int outValue, ref int refValue) { outValue = 2; refValue = 2; throw new ArgumentException(); // Is callerOutValue 1 or 2? // Is callerRefValue 1 or 2? } Since ref and

How to convert out/ref extern parameters to F#

冷暖自知 提交于 2019-11-28 11:21:27
I've got a C# extern declaration that goes like this: [DllImport("something.dll")] public static extern ReturnCode GetParent(IntPtr inRef, out IntPtr outParentRef); How to translate that to F#? You can try something like the code below. I don't know what ReturnCode is, so the code below expects it is an integer. For any more complex type, you'll need to use [<Struct>] attribute as in the answer referenced by A-Dubb. type ReturnCode = int [<System.Runtime.InteropServices.DllImport("something.dll")>] extern ReturnCode GetParent(System.IntPtr inRef, System.IntPtr& outParentRef); To call the

How to convert out/ref extern parameters to F#

与世无争的帅哥 提交于 2019-11-27 06:13:50
问题 I've got a C# extern declaration that goes like this: [DllImport("something.dll")] public static extern ReturnCode GetParent(IntPtr inRef, out IntPtr outParentRef); How to translate that to F#? 回答1: You can try something like the code below. I don't know what ReturnCode is, so the code below expects it is an integer. For any more complex type, you'll need to use [<Struct>] attribute as in the answer referenced by A-Dubb. type ReturnCode = int [<System.Runtime.InteropServices.DllImport(

Why doesn&#39;t &#39;ref&#39; and &#39;out&#39; support polymorphism?

流过昼夜 提交于 2019-11-26 11:11:14
Take the following: class A {} class B : A {} class C { C() { var b = new B(); Foo(b); Foo2(ref b); // <= compile-time error: // "The 'ref' argument doesn't match the parameter type" } void Foo(A a) {} void Foo2(ref A a) {} } Why does the above compile-time error occur? This happens with both ref and out arguments. Eric Lippert ============= UPDATE: I used this answer as the basis for this blog entry: Why do ref and out parameters not allow type variation? See the blog page for more commentary on this issue. Thanks for the great question. ============= Let's suppose you have classes Animal ,