草庐IT

const-ref

全部标签

c# - 在类 C# 中使用 ref

我想为我正在制作的类(class)提供一个特定的链表。我希望类写入该列表(例如通过.addLast())。我应该为此使用ref关键字吗?我对在C#中在哪里使用ref和out关键字感到有些困惑,因为所有classes都是在堆上动态分配的,并且我们实际上使用指针进行大多数操作。当然,out和ref关键字对于基元和结构是有意义的。还有,如果我不直接发送列表,而是发送一个包含列表的类呢?(它是internal并且需要),我还需要使用ref吗?或者如果我在函数之间传递它,例如:voidA(refLinkedListlist){B(list);}voidB(refLinkedListlist){_

c# - 只读 ("const"-like) C# 的函数参数

我有C++背景,习惯于将const关键字添加到函数定义中,以使对象以只读值传递。但是,我发现这在C#中是不可能的(如果我错了请纠正我)。经过一番谷歌搜索后,我得出的结论是,创建只读对象的唯一方法是编写一个仅具有“get”属性的接口(interface),然后将其传入。优雅,我必须说。publicinterfaceIFoo{IMyValInterfaceMyVal{get;}}publicclassFoo:IFoo{privateConcreteMyVal_myVal;publicIMyValInterfaceMyVal{get{return_myVal;}}}我会将它传递给:publi

c# - 使用指针和 ref 关键字引用值有什么区别

我有以下代码:classProgram{privateunsafestaticvoidSquarePtrParam(int*input){*input*=*input;}privatestaticvoidSquareRefParam(refintinput){input*=input;}privateunsafestaticvoidMain(){intvalue=10;SquarePtrParam(&value);Console.WriteLine(value);intvalue2=10;SquareRefParam(refvalue2);Console.WriteLine(value

c# - 使用 out 和 ref 参数时的装箱和拆箱

当方法接受ValueType的out/ref参数时是否会发生装箱/拆箱? 最佳答案 对于ref关键字它已经在MSDN上提到了那:Donotconfusetheconceptofpassingbyreferencewiththeconceptofreferencetypes.Thetwoconceptsarenotthesame.Amethodparametercanbemodifiedbyrefregardlessofwhetheritisavaluetypeorareferencetype.Thereisnoboxingofava

c# - C# 7.0 中的 Ref 返回限制

我试图理解以下摘自官方博客文章的摘录,该文章介绍了C#7.0中与引用返回有关的新功能。Youcanonlyreturnrefsthatare“safetoreturn”:Onesthatwerepassedtoyou,andonesthatpointintofieldsinobjects.Reflocalsareinitializedtoacertainstoragelocation,andcannotbemutatedtopointtoanother.遗憾的是,该博文没有给出任何代码示例。如果有人可以通过实际示例和解释进一步阐明以粗体突出显示的限制,我们将不胜感激。提前致谢。

c# - 如何在 C# 中访问属性或 const 的 Description 属性?

如何访问常量或属性的Description属性,即publicstaticclassGroup{[Description("Specifiedparent-childrelationshipalreadyexists.")]publicconstintParentChildRelationshipExists=1;[Description("Userisalreadyamemberofthegroup.")]publicconstintUserExistsInGroup=2;}或publicstaticclassGroup{[Description("Specifiedparent-c

javascript - 所以我使用 Javascript const 关键字,在 IE 中会发生什么?

我知道const关键字已经在除IE10版本之外的浏览器中全面实现,但它可行吗?如果有人在IE10上跳转到我的站点MDN有关const关键字的文档在底部提供了一个方便的图表,它告诉我不仅IE,而且许多移动浏览器也不支持它。我应该废弃它并使用var吗? 最佳答案 以Babel为例,它是ECMAScript2015(ES6)到ECMAScript5的转译器。如果你写:consta=123;输出:"usestrict";vara=123;如果在不受支持的浏览器上破坏您的代码的可能性还不够,我认为应该是。

javascript - React Native _this2.refs.myinput.focus 不是函数

使用React-Native,我有一个从TextInput扩展而来的自定义组件,如下所示:TextBox.js...render(){return();}...MyScene.js(导入TextBox.js)...render(){render({this.refs.MySecondInput.focus();}}/>);}当我构建应用程序并在专注于MyFirstInput时按下键盘上的下一步时,我希望MySecondInput成为焦点,但我得到了错误:_this2.refs.MySecondInput.focusisnotafunction可能是什么错误?是不是和this的作用域有关

javascript - 我如何使用 refs 来更改 ReactJS 中的样式类?

我正在尝试在颜色值更改时更改div的背景。这是我接收颜色值的函数:ChangeColor(oColor){this.props.ChangeColor(oColor);console.log("Refs:",this.refs.colorPicker.className);},这是CSS类.colorPicker{padding-right:25px;background:#000;display:inline;margin-right:5px;}这是我的div元素,其背景需要在运行时更新。我不确定refssynatx,所以请帮助解决这个问题。谢谢。 最佳答

javascript - 渲染后获取 React.refs DOM 节点宽度,仅当宽度值发生变化时才触发重新渲染

我正在尝试获取refDOM元素的宽度并设置state,然后在组件render中使用。问题来了,因为这个宽度在用户输入时发生变化,当我在componentDidUpdate中尝试setState时,它开始无限循环,我的浏览器崩溃了。我在这里创建了一个fiddlehttp://jsbin.com/dizomohaso/1/edit?js,output(打开控制台获取一些信息)我的想法是;组件安装,setState:refs.element.clientWidth用户输入数据,触发rendershouldComponentUpdate仅当new.state不等于old时才返回true.状态。