问候,
我得到传递值和传递参考之间的差异.但是通过ref传递引用(例如数组)并按值传递数组是我无法理解的.如何通过引用传递引用?
int[] myArray = {1,2,3};
PassByVal(myArray);
PassByRef(ref myArray);
PassByVal(int[] array)
{ array = new int[] {7,8,9}; // will not work }
PassByRef(ref int[] array)
{ array = new int[] {10,11,12}; } // will work
Run Code Online (Sandbox Code Playgroud) 如果我们有两个方法,一个按值返回一个变量,另一个按引用返回,哪个具有最高的性能?
myObj.Method1(out var);
Run Code Online (Sandbox Code Playgroud)
要么
var = myObj.Method2();
Run Code Online (Sandbox Code Playgroud)
我想第一个版本效率更高但是,这是否意味着你应该总是构建通过引用返回值的方法?或者有没有理由按值返回变量?
谢谢.
我最近重构了一些代码,现在有一个静态实用程序类,其方法如下:
const int x = 1;
public static string doWork(ref DataTable dt)
{
decimal total = 0;
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
decimal annual = decimal.Parse(row["Cost"].ToString());
total += decimal.Round(annual, 2);
}
return String.Format("{0:C}", total);
}
Run Code Online (Sandbox Code Playgroud)
我为了清晰起见简化了这个例子......
我可能会遇到任何不良影响这样做,并在ASP.NET应用程序背后的代码中调用doWork方法被许多用户击中?任何人都知道或有一个参考,我可以阅读有关静态方法的性能如何工作的方法?这会成为任何类型的瓶颈吗?
编辑:
是的我道歉这不是一个很好的例子,所以让我们说更像这样的事情:
const int x = 1;
public static string doWork(ref DataTable dt)
{
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
row["Cost"] = 0.0;
}
}
Run Code Online (Sandbox Code Playgroud)
你说的是A)我实际上甚至不需要这里的ref,因为Datatable已经通过ref和B传递了.通过"漏斗"对单个静态方法的所有调用来完全没有达到性能.
我有一个std ::类列表,并希望删除标记为删除的条目.我正在使用std :: remove_if和erase.
class MyClass
{
bool isDone(MyData& myData)
{
return myData.isDone();
}
void removeIfDone(std::list<MyData>& myList)
{
std::list<MyData>::iterator it =
remove_if(myList.begin(), myList.end(),
boost::bind(&MyClass::isDone, this, _1));
myList.erase(it, myList.end());
}
};
Run Code Online (Sandbox Code Playgroud)
我正在一个小型处理器上运行,内存分配和释放非常昂贵.此删除在我的应用程序中调用new并删除数千次.
我以前在使用boost::ref非平凡变量作为绑定参数时使用过,但在这种情况下,我认为它可能是仿函数本身的创建和破坏,或者是造成问题的复制.
我想做点什么
boost::bind(&MyClass::isDone, boost::ref(this), boost::ref(_1));
Run Code Online (Sandbox Code Playgroud)
我找不到有关正在创建和销毁的内容的文档.所以我的简单问题是如何提高效率呢?
如何验证使用Moq调用"CallWithRef"方法?
public interface ITest
{
void CallWithoutRef(string value, List<string> errors);
void CallWithRef(string value, ref List<string> errors);
}
public class Foo
{
private ITest testInterface;
public Foo(ITest testInterface)
{
this.testInterface = testInterface;
}
public void DoStuff(string value)
{
var errorList = new List<string>();
testInterface.CallWithoutRef(value, errorList);
testInterface.CallWithRef(value, ref errorList);
}
}
[TestMethod]
public void VerifyTestInterfaceCalls()
{
var expectedValue = Path.GetRandomFileName();
var mockTestInterface = new Mock<ITest>();
var foo = new Foo(mockTestInterface.Object);
foo.DoStuff(expectedValue);
mockTestInterface.Verify(x => x.CallWithoutRef(expectedValue, It.IsAny<List<string>>()));
// Test fails here:
var errorList = …Run Code Online (Sandbox Code Playgroud) 我有这条线:
$stmt->bind_result('d', $keyarray['payment_gross']);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
致命错误:无法在第35行的/home/star1231/public_html/pdt.php中通过引用传递参数1
我读了关于这个错误的主题,我没有找到有用的东西,任何人都知道这里有什么问题?
根据定义,ref关键字必须在传递之前初始化.而out参数必须在从函数返回之前初始化.
以下是我的片段.
public void TestRef(ref string n)
{
}
public void TestOut(out string n)
{
n = "Hello"; //if I don't initialize, I gets compile time error. & That's right.
}
Run Code Online (Sandbox Code Playgroud)
现在在调用方法时.
string name;
TestOut(out name);//fine
TestRef(ref name) // why not throwing error.
Run Code Online (Sandbox Code Playgroud)
在尝试调用TestRef()时的上述调用中,我没有初始化name参数.但根据我的理解,ref参数必须在传递之前初始化.
它构建和运行没有错误.
我需要在几行中显示视频(卡片)的缩略图,并专注于第一个缩略图.我使用嵌套地图进行了显示.代码基本上遍历视频数组并返回多行视频.
我们如何专注于渲染的第一个元素?我想我们需要得到第一个要关注的元素的参考.但是我们如何在这里设置ref并在另一个函数中引用它?
const CategoryGrid = props => {
return (
<HotKeys handlers={handlers}>
<div className="grid-container">
<p className="title"> {props.title.toUpperCase()} </p>
<div className="grid">
{
props.videos.map((rowvideos,index) => {
var rowVideoArr = [];
rowvideos.map((video,key)=>{
rowVideoArr.push(<div key={video.id} className="fleft card-container grow">
<Card key={video.id} title={video.name} background={video.thumbnailUrl}/>
</div>)
})
return(<div className="row" key={index}>{rowVideoArr}</div>)
})
}
</div>
</div>
</HotKeys>
);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Vue 访问 DOM 元素,使用该$refs功能,但无法使其正常工作。
我的元素如下所示。PlateId 是动态生成的,因此它不会总是相同的数字:
<textarea :ref="plateId + '-notes'">
Run Code Online (Sandbox Code Playgroud)
我的 Vue 函数如下所示:
/* This does not work */
addNotes: function(plateId) {
console.log(this.$refs.plateId + '-notes');
}
Run Code Online (Sandbox Code Playgroud)
每当我运行此代码并激活该功能时,它只会undefined在我的控制台中读取。我也试过这个,它也不起作用并且读取未定义:
/* This does not work */
addNotes: function(plateId) {
var plateIdNotes = plateId + '-notes';
console.log(this.$refs.plateIdNotes);
}
Run Code Online (Sandbox Code Playgroud)
替换var为const(我正在使用 ES6 并转换代码)也不起作用:
/* This does not work */
addNotes: function(plateId) {
const plateIdNotes = plateId + '-notes';
console.log(this.$refs.plateIdNotes);
}
Run Code Online (Sandbox Code Playgroud)
我知道ref正在正确绑定到元素,因为当我在下面执行此操作时,我可以在控制台中看到所有其他引用以及plateId-notes 引用:
/* This works …Run Code Online (Sandbox Code Playgroud)