本文介绍了请知道为什么两个引用相同的字符串对象的情况下字符串(code下面写的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ddLlSTeXPT : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_Load(object sender, EventArgs e)
    {
          string pass="infoways";

          if(txtbx.Text==pass)
          Response.Write("hello U Logged In");
          else
          Response.Write("hello U cant log In");
    }
}

但它正确打印,如果在文本框中的文本是infoways。如何thesepoint以相同refrence的两个对象被分配不同的内存?

But it print correctly if the text in textbox is "infoways". How thesepoint to same refrence as the two objects are assigned different memory?

推荐答案

我不完全知道你问这里。你已经发布测试的code,看是否在文本框中的文本控制等同于字符串infoways。如果是的话,它会显示消息你好ü登录;如果不是,它显示消息你好你无法登录。您的code出现预期是工作。

I'm not entirely sure what you're asking here. The code that you've posted tests to see if the text in the TextBox control is equivalent to the string "infoways". If so, it displays the message "hello U Logged In"; if not, it displays the message "hello U can't log In". Your code appears to be working as expected.

== 运算符重载为字符串类,所以,当你写字符串1 ==字符串2 ,即基本上等同于 String.Equals(字符串1,字符串)。不像其他的对象,在 == 运营商并不比为字符串类型引用相等。正如解释的文档

The == operator is overloaded for the String class, so when you write string1 == string2, that is essentially equivalent to String.Equals(string1, string2). Unlike other objects, the == operator does not compare reference equality for String types. As explained by the documentation:

尽管string是引用类型,  相等运算符(==和!=)是  定义为比较的值  字符串对象,而不是引用。本  使得测试字符串是否相等更多  直观。

这篇关于请知道为什么两个引用相同的字符串对象的情况下字符串(code下面写的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:42