在stringC#中的类型是引用类型,并通过值拷贝传递引用类型参数的参考,这样我就不需要使用ref修改器.但是,我需要使用ref修饰符来修改输入string.为什么是这样?
using System;
class TestIt
{
static void Function(ref string input)
{
input = "modified";
}
static void Function2(int[] val) // Don't need ref for reference type
{
val[0] = 100;
}
static void Main()
{
string input = "original";
Console.WriteLine(input);
Function(ref input); // Need ref to modify the input
Console.WriteLine(input);
int[] val = new int[10];
val[0] = 1;
Function2(val);
Console.WriteLine(val[0]);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个方法来接受对布尔标志的引用并修改它们.布尔值都是单独声明的(即不在可索引的数据结构中),并且方法的调用者应该能够决定修改哪些布尔值.
示例代码(这适用):
class Program
{
private static bool b1, b2, b3, b4, b5;
private static void doSomething(ref bool setTrue, ref bool setFalse, ref bool invert)
{
setTrue = true;
setFalse = false;
invert = !invert;
}
static void Main(string[] args)
{
Console.WriteLine("Pre: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5);
doSomething(ref b1, ref b3, ref b5);
Console.WriteLine("Post: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5);
}
}
Run Code Online (Sandbox Code Playgroud)
输出,如预期:
Pre: False, False, False, False, False
Post: True, …Run Code Online (Sandbox Code Playgroud) 我想知道为什么第一次呼叫Bar(ref object)不起作用而第二次呼叫不起作用.考虑到我以object任何一种方式传递一个类型,并传递一个匿名类型Foo(object)工作正常,似乎很傻.为什么ref,与内存位置有关的东西会影响调用Bar()?
请考虑以下代码段:
static void Foo(object obj)
{ }
static void Bar(ref object obj)
{ }
static void Main()
{
// Compiles
var a = new { };
Foo(a);
// Does not compile
var b = new { };
Bar(ref b);
// Compiles
object c = new { };
Bar(ref c);
}
Run Code Online (Sandbox Code Playgroud)
我在下面的答案中看到了如何编译代码的建议,但这不是我追求的.我想特别知道为什么ref在将匿名类型传递到正常Foo()工作时使其成为参数会阻止编译.
我有一个带有签名的修改方法
private bool Modify(ref MyClass obj);
Run Code Online (Sandbox Code Playgroud)
这将修改obj并表明其返回值的成功.Modify是不是重新分配引用(我知道这不起作用),只是修改实例字段,所以我想用它来做类似以下的事情:
foreach(MyClass obj in myList)
{
bool success = Modify(obj);
// do things depending on success
}
Run Code Online (Sandbox Code Playgroud)
我正在遇到一个问题,因为obj"没有使用ref关键字传递".但是,如果我像这样放入ref关键字:
bool success = Modify(ref obj);
Run Code Online (Sandbox Code Playgroud)
我得到"不能obj用作ref/ out因为它是'foreach迭代变量'.我知道foreach使用了一个不可变的迭代器,这就是为什么这不起作用.
我的问题是,做这样的工作最容易的替代方案是什么?
我试过用
foreach(int i = 0; i < myList.Count; i++)
{
bool success = Modify(ref myList[i]);
// do things depending on success
}
Run Code Online (Sandbox Code Playgroud)
但他们得到"属性或索引器可能不会作为ref参数传递".
谢谢你的帮助.
我试图理解为什么/如何在将refs返回给类成员的情况下返回.换句话说,我想了解运行时的内部工作原理,它可以保证实例成员的ref-return从CLR的内存安全方面起作用.
我引用的具体功能在ref-return文档中提到,该 文档具体说明:
返回值不能是返回它的方法中的局部变量; 它的范围必须超出返回它的方法.它可以是类的实例或静态字段,也可以是传递给方法的参数.尝试返回局部变量会生成编译器错误CS8168,"无法通过引用返回本地'obj',因为它不是ref本地."
这是一个完整编译和运行的代码片段,演示如何返回实例字段作为ref return:
using System;
using System.Diagnostics;
namespace refreturn
{
public struct SomeStruct {
public int X1;
}
public class SomeClass {
SomeStruct _s;
public ref SomeStruct S => ref _s;
}
class Program
{
static void Main(string[] args)
{
var x = new SomeClass();
// This will store a direct pointer to x.S
ref var s = ref x.S;
// And now the GC will be free to re-use this memory
x …Run Code Online (Sandbox Code Playgroud) 我有一个使用findDOMNode()的旧代码.
这是我的代码,其中someComponent1和Expand已经导入
在这里,我有一些疑问,我用findDOMNode()编写的代码工作得非常好但是因为它现在已被弃用,我想删除它.我已经浏览了很多文档,发现使用门户网站或引用而不是这个.我有一个理解,如果我使用ref然后变量get绑定也可以访问DOM元素,但我想我错了,因为它以这种方式工作.有人可以纠正我对此的理解
class classA extends Component {
componentDidMount() {
new Expand(ReactDOM.findDOMNode(this.expand))
// new Expand(this.expand)
}
render(){
return(
<someComponent1 className={style.container} ref={e => this.expand= e}/>
)
}
}
Run Code Online (Sandbox Code Playgroud) 我在使用ref带有样式组件的s 时遇到困难.当我尝试在我的类方法中访问它们时,我收到以下错误:
Edit.js:42 Uncaught TypeError:这.....包含不是一个函数
constructor(props) {
....
this.setWrapperRef = this.setWrapperRef.bind(this);
this.handleClickOutside = this.handleClickOutside.bind(this);
}
----------
setWrapperRef = (node) => {
this.wrapperRef = node;
}
handleEdit = (e) => {
e.preventDefault();
this.props.onEdit(this.props.id, this.state.title);
}
----------
<Wrapper onSubmit={this.handleEdit} ref={this.setWrapperRef}>
...
</Wrapper>
Run Code Online (Sandbox Code Playgroud)
我从这个问题中找到了代码
我在这做错了什么?
我正在通过数组映射,并且为每个项目显示一个带有文本的按钮。假设我希望单击按钮,下面的文本会将其颜色更改为红色。如何定位按钮的同级?我尝试使用ref,但是由于它是映射的jsx,因此只会声明最后一个ref元素。
这是我的代码:
class Exams extends React.Component {
constructor(props) {
super()
this.accordionContent = null;
}
state = {
examsNames: null, // fetched from a server
}
accordionToggle = () => {
this.accordionContent.style.color = 'red'
}
render() {
return (
<div className="container">
{this.state.examsNames && this.state.examsNames.map((inst, key) => (
<React.Fragment key={key}>
<button onClick={this.accordionToggle} className="inst-link"></button>
<div ref={accordionContent => this.accordionContent = accordionContent} className="accordionContent">
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aperiam, neque.</p>
</div>
</React.Fragment>
))}
</div>
)
}
}
export default Exams;
Run Code Online (Sandbox Code Playgroud)
如所解释的,结果是在每次单击按钮时,将定位到最后一个按钮所附的段落。
提前致谢
我正在使用具有可变时间范围的议程/日历应用程序。要显示当前时间的一行并显示已进行的约会块,我需要计算给定时间范围内一分钟对应多少像素。
因此,例如:如果议程在上午7点开始,在下午5点结束,则总时间为10小时。假设日历的正文高度为1000像素。这意味着每小时代表100像素,每分钟代表1.66像素。
如果当前时间是下午3点。我们距议程开始只有480分钟的路程。这意味着显示当前时间的行应该在距日历正文顶部79.68像素(480 * 1.66)的位置。
计算没有问题,但获得议程主体的高度。我当时在考虑使用React Ref来获取高度,但出现错误:ref.current is null
下面的一些代码:
class Calendar extends Component {
calendarBodyRef = React.createRef();
displayCurrentTimeLine = () => {
const bodyHeight = this.calendarBodyRef.current.clientHeight; // current is null
}
render() {
return (
<table>
<thead>{this.displayHeader()}</thead>
<tbody ref={this.calendarBodyRef}>
{this.displayBody()}
{this.displayCurrentTimeLine()}
</tbody>
</table>
);
}
}
Run Code Online (Sandbox Code Playgroud) I'm passing a System.Action as a parameter to a method which does some lengthy operation and want to add more stuff to the invocation list after the Action has been passed:
class Class1
{
private System.Action onDoneCallback;
void StartAsyncOperation(System.Action onDoneCallback)
{
this.onDoneCallback = onDoneCallback;
// do lengthy stuff
}
void MuchLater()
{
this.onDoneCallBack?.Invoke();
}
}
class Class2
{
public System.Action action;
void DoStuff()
{
action += () => print ("a");
new Class1().StartAsyncOperation(action);
}
{
// ... much later in another …Run Code Online (Sandbox Code Playgroud)