ref

Difference between ref and out parameters in .NET

What is the difference between ref and out parameters in .NET? What are the situations where one can be more useful than the other? Can anybody illustrate with a code snippet where one can be used and another can't? ...

Using "ref" and/or "out" for Object type

I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies from 2.0 for now), and I was trying to optimize some parts of the code. As it deals a lot with runtime callable wrappers, which need to be released, I ended up to create a utility method which loops until all references are released. The signature of the method ...

Latex: hyperref to individual longtable rows

Hi, I have a few longtables that stretch several pages and I want to use pageref and hyperref to link to these rows. But whatever I try, the links always refer to the start of the table. When I look into the aux file, the labels all seem to be re-defined into table.[number of table]. I tried putting invisible dummy figures into the tab...

Whats the difference between the 'ref' and 'out' keywords?

I'm creating a function where I need to pass an object so that it can be modified by the function. What is the difference between: public void myFunction(ref MyClass someClass) and public void myFunction(out MyClass someClass) Which should I use and why? ...

When to use ref and when it is not necessary in C#.

I have a object that is my in memory state of the program and also have some other worker functions that I pass the object to to modify the state. I have been passing it by ref to the worker functions. However I came across the following function. byte[] received_s = new byte[2048]; IPEndPoint tmpIpEndPoint = new IPEndPoint(IPAddress.An...

passing in object by ref

What is the difference between public function Foo(ref Bar bar) { bar.Prop = 1; } public function Foo(Bar bar) { bar.Prop = 1; } essentially what is the point of "ref". isn't an object always by reference? ...

C#: How to pass null to a function expecting a ref?

I've got the following function: public static extern uint FILES_GetMemoryMapping( [MarshalAs(UnmanagedType.LPStr)] string pPathFile, out ushort Size, [MarshalAs(UnmanagedType.LPStr)] string MapName, out ushort PacketSize, ref Mapping oMapping, out byte PagesPerSector);...

Ref Abuse: Worth Cleaning Up?

I have inherited some code that uses the ref keyword extensively and unnecessarily. The original developer apparently feared objects would be cloned like primitive types if ref was not used, and did not bother to research the issue before writing 50k+ lines of code. This, combined with other bad coding practices, has created some situa...

In C#, where do you use "ref" in front of a parameter?

There are a number of questions already on the definition of "ref" and "out" parameter but they seem like bad design. Are there any cases where you think ref is the right solution? It seems like you could always do something else that is cleaner. Can someone give me an example of where this would be the "best" solution for a problem? ...

Safe to assume all C# variables initialized by new are references?

I am learning about C# refs right now. Is it safe to assume that all variables that are assigned with a new are references and not values? Example: SomeType variable = new SomeType() ...

C#: Passing address of public variable

When i try to pass the address of a public variable like this: ML.Register("Radius", &lBeacons[i].Radius, 0.0f, 200.0f, 10.0f); I get this error: error CS0212: You can only take the address of an unfixed expression inside of a fixed statement initializer The Register function looks like this: public unsafe void Register(string des...

C# 'ref' keyword, performance.

If you have a Bitmap object that needs to be passed to numerous methods (about 10), and finally to an event where it shall finally be disposed of after its used, would it be (performance wise) beneficial to pass it to every one of those methods by reference instead of value? Passing by value the object is copied, passing by reference its...

Why is PERSON not a ref class???

I don't understand why compiler thinks PERSON is NOT a ref class: : error C2811: 'Runner' : cannot inherit from 'Person', a ref class can only inherit from a ref class or interface class I tried.... adding mscorlib.dll to the header files: #using..etc...<> - didn't work. making Person an abstract class - didn't work (Im gla...

why iterator methods can't take either 'ref' or 'out' parameters?

I tried this earlier today: public interface IFoo { IEnumerable<int> GetItems_A( ref int somethingElse ); IEnumerable<int> GetItems_B( ref int somethingElse ); } public class Bar : IFoo { public IEnumerable<int> GetItems_A( ref int somethingElse ) { // Ok... } public IEnumerable<int> GetItems_B( ref in...

Clojure Vector of Refs

What's the simplest way to create a vector of distinct refs? Using (repeat 5 (ref nil)) will return a list, but they will all reference the same ref: user=> (repeat 5 (ref nil)) (#<Ref@16ef71: nil> #<Ref@16ef71: nil> #<Ref@16ef71: nil> #<Ref@16ef71: nil> #<R ef@16ef71: nil>) Same result with (replicate 5 (ref nil)): user=> (replicat...

Assigning out/ref parameters in Moq

Is it possible to assign an out/ref parameter using Moq (3.0)? I've looked at using Callback(), but Action<> does not support ref parameters because it's based on generics. I'd also preferably like to put a constraint (It.Is) on the input of the ref parameter, though I can do that in the callback. I know that Rhino Mocks supports this ...

Are ref and out in C# the same a pointers in C++?

I just made a Swap routine in C# like this: static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } It does the same thing that this C++ code does: void swap(int *d1, int *d2) { int temp=*d1; *d1=*d2; *d2=temp; } So are the ref and out keywords like pointers for C# without using unsafe code...

Why are ref parameters not contravariant?

This works: EndPoint endPoint = new IPEndPoint(_address, _port); _socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint); But this does not: IPEndPoint endPoint = new IPEndPoint(_address, _port); _socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint); (Note the type of endPoint) Which seems odd. Why does...

Refering to a table in LaTeX

How can you refer to a table number such that you get Table 7 for instance? Sample data Taulu \ref{table:kysymys} lorem lorem ipsun. \begin{table} \label{table:kysymys} \begin{tabular}{| p{5cm} | p{5cm} | p{5cm} |} -- cut -- \end{tabular} \end{table} I get Taule 2.5 lorem lorem ipsun. where 2.5 is the chapter number. ...

What is the purpose of the "out" keyword at the caller (in C#)?

When a C# function has an output parameter, you make that clear as follows: private void f(out OutputParameterClass outputParameter); This states that the parameter does not have to be initialized when the function is called. However, when calling this function, you have to repeat the out keyword: f(out outputParameter); I am wonde...