C#语法问题 this的用法public class Users{private int _userid;private string _username;private string _userpwd;public int UserId{get { return this._userid; }set { this._userid = value; }}public string UserName{get { return this._username; }set {

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/11 21:07:59
C#语法问题 this的用法public class Users{private int _userid;private string _username;private string _userpwd;public int UserId{get { return this._userid; }set { this._userid = value; }}public string UserName{get { return this._username; }set {

C#语法问题 this的用法public class Users{private int _userid;private string _username;private string _userpwd;public int UserId{get { return this._userid; }set { this._userid = value; }}public string UserName{get { return this._username; }set {
C#语法问题 this的用法
public class Users
{
private int _userid;
private string _username;
private string _userpwd;
public int UserId
{
get { return this._userid; }
set { this._userid = value; }
}
public string UserName
{
get { return this._username; }
set { this._username = value; }
}
public string UserPwd
{
get { return this._userpwd; }
set { this._userpwd = value; }
}
public Users()
{
//
// TODO:在此处添加构造函数逻辑
//
}
public Users(int UserId,string UserName,string UserPwd)
{
this._userid = UserId;
this._username = UserName;
this._userpwd = UserPwd;
}
public Users(SqlDataReader Dr)
{
this._userid = Convert.ToInt32(Dr["UserId"]);
this._username = Dr["UserName"].ToString();
this._userpwd = Dr["UserPwd"].ToString();
}
上面代码中用了很多this 谁能讲一下啊是什么用意 在什么情况下一定要加上this
不要复制给我帮助中公式般回答 通俗的讲一下this的具体用法

C#语法问题 this的用法public class Users{private int _userid;private string _username;private string _userpwd;public int UserId{get { return this._userid; }set { this._userid = value; }}public string UserName{get { return this._username; }set {
你这里声明了一个类Users:
public class Users
那么,在类中的代码里使用this,指代的就是 Users 这个类.
比如说this._userid,指的就是Users类下面的成员int _userid(你已经在代码里声明过了成员int _userid:private int _userid;)
至于在什么情况下一定要加上this,我们看一下这个代码(以你的代码为例稍加修改).
public Users(SqlDataReader Dr)
{
int _userid;
_userid = 0;
this._userid = Convert.ToInt32(Dr["UserId"]);
this._username = Dr["UserName"].ToString();
this._userpwd = Dr["UserPwd"].ToString();
}
这个方法是你类里的构造函数之一,其中我加了一个局部变量_userid.
那么这个局部变量就和类里的成员_userid重名了.单独使用_userid = 0;进行操作是操作的是Users(SqlDataReader Dr)方法里的局部变量_userid,而this._userid = Convert.ToInt32(Dr["UserId"]);操作的是本类的成员_userid而不是局部变量.
但如果不存在重名时可以不用加this,不过作为一种良好的编程习惯,建议访问类成员时都使用this进行定位.一防日后加入局部变量时发生重名导致操作错误.
注:this所调用的是本类的实例化的该对象,对本类的其他实例没有影响.

C#语法问题 this的用法public class Users{private int _userid;private string _username;private string _userpwd;public int UserId{get { return this._userid; }set { this._userid = value; }}public string UserName{get { return this._username; }set { C#语法的问题 get set的用法public class Users { private int _userid; private string _username; private string _userpwd; public int UserId { get { return this._userid; } set { this._userid = value; } } public string UserName { get { return this C# where语法问题public static T AsActual(this T persistable)where T :IPersistablepublic interface IRepositoryFactory{T GetRepository()where T :class;}上面两种函数后带where的写法是什么意思?与LinQ有关? 举几个例子说明C#中的this关键字的用法 C#的语法指什么? C# 中return 的用法. 英语问题It’s reported that he made public at this decision at the last cabinet meeting.It’s reported that he made public at this decision at the last cabinet meeting.这里的public的词性是什么?made public at this decision这个用法 C#中 this 的用法 及 意义举个例子好不? c#中this的作用 c#中as的用法、意思?as是干什么用的?具体语法是什么?/// /// /// /// 要绑定的控件/// 用于绑定的数据源public static void Bind(WebControl wc,object dataSource){if (wc is GridView){GridView gv = wc as GridView;gv.DataSource C# this和C++this 的区别? 回文数的C#问题. c#中this的用法using System;namespace CallConstructor{public class Car{int petalCount = 0;String s = null;Car(int petals){petalCount = petals;Console.WriteLine(Constructor w/int arg only,petalCount = + petalCount);}Car(String s,int petals):th 请举例说明C#中委托的用法? C# Missing.Value的用法是什么? c# base的意义public abstract class B { public B(D t) { Console.WriteLine(B); } public void K() { Console.WriteLine(K); } } public class C : B { public C(String 关于C#中delegate的用法this.Invoke((EventHandler)(delegate{dataGridView.DataSource = dt;}));delegate有什么作用? 求一个简单的C的语法class A{ public:A(){};A(int i){};}A a(1);//这是什么用法?