什么是这三种方式清除文本框的区别? [英] What is the difference between these three ways to clear a Textbox?

查看:99
本文介绍了什么是这三种方式清除文本框的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是下面三种方式清除文本框的内容之间有点混乱。我与WPF工作,发现所有的工作,但我无法找到的差异。

I am bit confused between the below three ways to clear the contents of a textbox. I am working with WPF and found All are working, but I am unable to find the difference.

可有人请给我一些例子解释一下吗?

Can someone please explain to me about it with some examples?


  • txtUserName.Clear();

  • txtUserName.Text =的String.Empty;

  • txtUserName.Text =;

  • txtUserName.Clear();
  • txtUserName.Text = string.Empty;
  • txtUserName.Text = "";

推荐答案

清除()方法并不仅仅是删除从文本框的文字。它会删除所有内容并重置文字选择和尖为@ syned的回答很好地显示

The Clear() method does more than just remove the text from the TextBox. It deletes all content and resets the text selection and caret as @syned's answer nicely shows.

对于 txtUserName.Text =; 例如,如果一个尚不在字符串池中存在,并且将其设置为文本框架将创建一个空字符串对象属性。但是,如果字符串已在应用程序已被使用,那么框架将使用从池中此值。

For the txtUserName.Text = ""; example, the Framework will create an empty string object if one does not already exist in the string pool and set it to the Text property. However, if the string "" has been used already in the application, then the Framework will use this value from the pool.

对于 txtUserName.Text =的String.Empty; 为例,该框架将不会创建一个空的字符串对象,而不是指的是一个空字符串常量,这个设置为文本属性。

For the txtUserName.Text = string.Empty; example, the Framework will not create an empty string object, instead referring to an empty string constant, and set this to the Text property.

在性能测试中,它已被证明(在在C#我应该使用的String.Empty或的String.Empty或?后),真的是后者的两个例子之间没有差别非常有用。调用清除()方法肯定是最慢的,但是这显然是因为它有其他工作要做,以及清除文本。即使如此,在这三个选项之间性能上的差异仍几乎无法察觉

In performance tests, it has been shown (in the In C#, should I use string.Empty or String.Empty or ""? post) that there really is no useful difference between the latter two examples. Calling the Clear() method is definitely the slowest, but that is clearly because it has other work to do as well as clearing the text. Even so, the difference in performance between the three options is still virtually unnoticeable.

这篇关于什么是这三种方式清除文本框的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆