ref

Get a reference to a struct inside array

扶醉桌前 提交于 2019-11-28 05:06:00
I want to modify a field of a struct which is inside an array without having to set entire struct. In the example below, I want to set one field of element 543 in the array. I don't want to have to copy entire element (because copying MassiveStruct would hurt performance). class P { struct S { public int a; public MassiveStruct b; } void f(ref S s) { s.a = 3; } public static void Main() { S[] s = new S[1000]; f(ref s[543]); // Error: An object reference is required for the non-static field, method, or property } } Is there a way to do it in C#? Or do I always have to copy entire struct out of

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

余生长醉 提交于 2019-11-28 01:40:48
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 wondering what this is good for. Why is it necessary to repeat part of the function specification? Does anyone know? It means you know what you're doing - that you're acknowledging it's an out parameter. Do you really want the utterly different behaviour to happen

C# 'ref' keyword, performance

一笑奈何 提交于 2019-11-27 23:41:20
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 it's 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 not. (Performance is critical in this situation. The application needs to run as fast as possible.) Bitmap is a reference type. Passing a reference type by value does not copy the object, merely the reference to the object. There would be no performance

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

℡╲_俬逩灬. 提交于 2019-11-27 21:09:43
问题 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); Which I would like to call like this: FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, null, out PagePerSector); Unfortunately, I cannot pass null in a field that requires type ref Mapping and no cast I've

PHP and MySQLi - Cannot pass parameter 2 by reference in

左心房为你撑大大i 提交于 2019-11-27 16:05:25
i am trying to make a function which will check update and insert some datas but i am having an issue in the first step where the $stmt->bind_param is saying that is not passing parameters by reference or something like that. I searched over internet but nothing was around so i dont know what to do with it. I have attached below the function code: public function killTarget($killerid,$victimiid,$victimcode) { if ($this->checkUsercode($victimcode,$victimiid)) { $stmt = $this->_db->prepare("UPDATE users SET status =? WHERE user_id =?"); $stmt->bind_param("ii",0,$victimiid); if ($stmt->execute())

PL/SQL print out ref cursor returned by a stored procedure

跟風遠走 提交于 2019-11-27 07:25:38
How can I fetch from a ref cursor that is returned from a stored procedure (OUT variable) and print the resulting rows to STDOUT in SQL*PLUS? ORACLE stored procedure: PROCEDURE GetGrantListByPI(p_firstname IN VARCHAR2, p_lastname IN VARCHAR2, p_orderby IN VARCHAR2, p_cursor OUT grantcur); PL/SQL: SET SERVEROUTPUT ON; DECLARE TYPE r_cursor IS REF CURSOR; refCursor r_cursor; CURSOR grantCursor IS SELECT last_name, first_name FROM ten_year_pis WHERE year_added = 2010; last_name VARCHAR2(100); first_name VARCHAR2(100); BEGIN OPEN grantCursor; FETCH grantCursor INTO last_name, first_name; WHILE

Get a reference to a struct inside array

烂漫一生 提交于 2019-11-27 05:30:01
问题 I want to modify a field of a struct which is inside an array without having to set entire struct. In the example below, I want to set one field of element 543 in the array. I don't want to have to copy entire element (because copying MassiveStruct would hurt performance). class P { struct S { public int a; public MassiveStruct b; } void f(ref S s) { s.a = 3; } public static void Main() { S[] s = new S[1000]; f(ref s[543]); // Error: An object reference is required for the non-static field,

Why is list when passed without ref to a function acting like passed with ref?

对着背影说爱祢 提交于 2019-11-27 05:24:51
If I did not get this terribly wrong, this behaviour is strange for me. Rather than explaining, I'll post a sample code below and please tell me why does I get output x and not y. private void button1_Click(object sender, EventArgs e) { List<int> l = new List<int>() { 1, 2, 3 }; Fuss(l); MessageBox.Show(l.Count.ToString()); } private void Fuss(List<int> l) { l.Add(4); l.Add(5); } Output should, I assume would be 3. But I get the output as 5. I understand the output can be 5 if I do this: private void button1_Click(object sender, EventArgs e) { List<int> l = new List<int>() { 1, 2, 3 }; Fuss

C# Cannot use ref or out parameter inside an anonymous method body

本小妞迷上赌 提交于 2019-11-27 02:07:37
问题 I'm trying to create a function that can create an Action that increments whatever integer is passed in. However my first attempt is giving me an error "cannot use ref or out parameter inside an anonymous method body". public static class IntEx { public static Action CreateIncrementer(ref int reference) { return () => { reference += 1; }; } } I understand why the compiler doesn't like this, but nonetheless I'd like to have a graceful way to provide a nice incrementer factory that can point to

C# 'ref' keyword, performance

╄→尐↘猪︶ㄣ 提交于 2019-11-26 21:33:59
问题 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 it's 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 not. (Performance is critical in this situation. The application needs to run as fast as possible.) 回答1: Bitmap is a reference type. Passing a reference