为什么我需要在声明和调用中使用'ref'关键字.
void foo(ref int i)
{
}
Run Code Online (Sandbox Code Playgroud)
例如,考虑以上功能.如果我在没有ref关键字的情况下调用它
foo(k);
Run Code Online (Sandbox Code Playgroud)
它会给我错误:
必须使用'ref'关键字传递参数'1'
为什么仅仅在方法签名中指定它是不够的?
在正常情况下,可以通过调用new DelegateType函数并将函数作为参数传递,将F#函数转换为委托.但是当委托包含byref参数时,这是不可能的.例如代码:
type ActionByRef<'a> = delegate of 'a byref -> unit
let f (x:double byref) =
x <- 6.0
let x = ref 42.0
let d = new ActionByRef<_>(f)
Run Code Online (Sandbox Code Playgroud)
不会编译,给出以下错误:
此函数值用于构造其签名包含byref参数的委托类型.您必须使用带有1个参数的显式lambda表达式.
出现错误后,修改要使用的代码
let d = new ActionByRef<_>(fun x -> f(&x))
Run Code Online (Sandbox Code Playgroud)
作品.但我的问题是:为什么这有必要?为什么F#不允许从命名函数转换到此委托,但是从lambda转换是好的?
在研究另一个问题时,我发现了这种行为.我意识到byref只是为了与其他.Net语言兼容.
文档说
Run Code Online (Sandbox Code Playgroud)make_ref() -> ref()返回一个几乎唯一的引用.
返回的参考将在大约2 82次呼叫后重新发生; 因此它具有足够的实用性.
但我的眼睛告诉我,在VM重启之间,我可以轻松得到相同的参考:
[~] erl
Erlang R14B04 (erts-5.8.5)
1> make_ref().
#Ref<0.0.0.33>
2> make_ref().
#Ref<0.0.0.37>
^C
[~] erl
Erlang R14B04 (erts-5.8.5)
1> make_ref().
#Ref<0.0.0.33>
Run Code Online (Sandbox Code Playgroud)
那么,Erlang的Refs有多独特呢?当标记在mq或db中持久存在并且可能由不同的VM会话生成时,它们是否适合用作唯一的"标记"生成器.
我知道UUID可以用于此.众所周知,pids()是可重复的,可重用的,如果是序列化的,则绝不是唯一的,然后从持久存储加载.
问题是,什么是refs() - 更像是UUID或更像pids()?节点之间的refs()是否唯一?重启之间?有关于这个主题的官方信息吗?
好吧,我对ParameterInfo班级的属性感到困惑.
不幸的是,文档不是很清楚:示例显示了如何构建方法,但没有显示这些方法在C#中的外观.
Cane有人告诉更多关于这些属性:
DefaultValueHasDefaultValueIsInIsLcidIsOptionalIsOutIsRetval哪种组合导致什么方法参考.
我做了一个简单的程序,它给出了以下输出:
方法名称M1 void M1(object param)
IL签名:.method public hidebysig instance void M1(object param) cil managed
方法参数说明:
通过引用传递False
HasDefaultValue = False
IsIn = False
IsLcid = False
IsOptional = False
IsOut = False
IsRetVal = False
方法名称M2 void M2(object param = null)
IL签名.method public hidebysig instance void M2([opt] object param) cil managed
方法参数说明:
通过引用传递False
HasDefaultValue = True
DefaultValue = null
IsIn = False
IsLcid = False
IsOptional …
我定义了一个使用 ref 对象作为参数的方法。当我尝试使用引用列表调用它时,它告诉我无法从引用列表转换为引用对象。我做了很多搜索来找到答案。然而,大多数答案是“你不需要在这里参考”或者有解决方法。
即使使用 ref (Base)[Inherited],似乎也无法从“ref [Inherited]”转换为“ref [Base]”。不知道我说得对不对。
我想要的是在 set { } 块中仅写入 1 行来更改值并发送通知。有什么建议么?
class CommonFunctions
{
public static void SetPropertyWithNotification(ref object OriginalValue, object NewValue, ...)
{
if(OriginalValue!= NewValue)
{
OriginalValue = NewValue;
//Do stuff to notify property changed
}
}
}
public class MyClass : INotifyPropertyChanged
{
private List<string> _strList = new List<string>();
public List<string> StrList
{
get { return _strList; }
set { CommonFunctions.SetPropertyWithNotification(ref _strList, value, ...);};
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含以下代码的库:
double result = -1;
if (!string.IsNullOrEmpty(fieldName))
{
string value = HttpContext.Current.Request.QueryString[fieldName];
if (string.IsNullOrEmpty(value))
{
value = HttpContext.Current.Request.Form[fieldName];
}
if (string.IsNullOrWhiteSpace(value) || !double.TryParse(value.Trim(), out result))
{
result = -1;
}
}
return result;
Run Code Online (Sandbox Code Playgroud)
在我的Visual Studio 2015本地副本中,编译很好.上周构建服务器也会让这个编译没有问题.突然,周一构建服务器开始出错:
QueryParser.cs(449,53):错误CS1620:必须使用'ref'关键字[$ projectPath\Core40.csproj]传递参数2
当我进行切换(使用ref而不是out)时,构建服务器能够完成构建而不会出错,但Visual Studio将不再编译.
从我的相关文件的阅读,double.TryParse也一直采取了out关键字,所以必须有关于构建服务器的库的东西了,但我不知道什么或如何去诊断它.
经过下面的一些问题,我又回到了班级,并确认有多个实例decimal.TryParse(value, out result),int.TryParse(value, out result),float.TryParse(value, out result),long.TryParse(value, out result),和Guid.TryParse(value, out result).我也能用"浮动"替换"双",它工作得很好.这是特定的东西double.
显然,构建命令是:
msbuild /m /p:Configuration=Release /p:Platform="Any CPU" …Run Code Online (Sandbox Code Playgroud) In order to allow my parent component (JsonFetcher) to access values from my child component (Display), I tried using createRef() API that just came of this patch 16.3
Following the "Adding a Ref to a class Component" example in this document, here's what I tried in my code:
class JsonFetcher extends Component {
constructor(props) {
super(props);
this.child = React.createRef();
this.state = {
data: [],
}
}
componentDidMount() {
this.updateContent(this.props.mainUrl)
}
updateContent(mainUrl){
fetch(mainUrl)
.then((responseJsonAnyUrl) => responseJsonAnyUrl.json()) …Run Code Online (Sandbox Code Playgroud) 我是React.js的新手,刚才我正在学习refReact 的概念.他们在V16.3中有新的createRef API.我试图从REACT DOC这样学习这个 -
import React from "react";
export class MyComponent extends React.Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// Explicitly focus the text input using the raw DOM API
// Note: we're accessing "current" to get the DOM node
this.textInput.current.focus();
}
render() {
// tell React that we want to associate the <input> ref
// …Run Code Online (Sandbox Code Playgroud) 我想使用 ref,但是我无法在最初为 false 的条件渲染中使用它。
constructor(props) {
super(props);
this.state = {
filterActive: false,
}
this.firstRef = React.createRef();
}
Run Code Online (Sandbox Code Playgroud)
如果我在此使用 ref :-
{this.state.filterActive ?
<label ref={this.firstRef}>Time Range</label>
:null}
Run Code Online (Sandbox Code Playgroud)
引用为空。
我不知道如何使switch表达式产生一个ref值。
bool cond = true;
int a = 1, b = 2;
// This works
ref int c = ref cond ? ref a : ref b;
// But using a switch expression fails to compile.
// Error CS1525 Invalid expression term 'ref'
c = ref (cond switch { true => ref a, false => ref b });
Run Code Online (Sandbox Code Playgroud)
我的语法有误吗?这有可能吗?
无论是否包含外部,它都不会编译ref ( )。我bool只是为了快速说明问题而使用,但是我的实际用例并不是那么简单。