回 帖 发 新 帖 刷新版面

主题:为什么&&在c# 2005编译器里被看做是是运算符?

[color=FF0000]using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.textusername.Text = "";
        this.textuserpwd.Text = "";
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (this.textusername.Text = "a"  && this.textuserpwd.Text = "a[color=00FF00]")//就是这行[/color]        {
            this.lbMessage.Text = "登入成功";
        }
        else
        {
            this.lbMessage.Text = "登入失败";
        }
    }
}[/color]


[b]为什么&&在c# 2005编译器里被看做是是运算符?而在c#2003编译器里就不会报错?我想要达到"和"的效果应该用什么关键字?[/b]

回复列表 (共5个回复)

沙发

 if (this.textusername.Text = "a"  && this.textuserpwd.Text = "a")
這句應該改成
if (this.textusername.Text == "a"  && this.textuserpwd.Text == "a")
就對了

板凳

if(textusername.Text == "a" && textuserpwd.Text == "a")
或者
if(textusername.Text == "a" & textuserpwd.Text == "a")

都可以。C# 的判断对 &, && 不敏感

3 楼

To vrace:

C#也对&和&&敏感:&会同时检查两元,&&在检查左元为false时会直接返回false而不检查右元。

4 楼

&&在C#中是一个运算符,表示“与”运算

5 楼

3楼的果然是高人...又学到一个Point

我来回复

您尚未登录,请登录后再回复。点此登录或注册