多少上下文“;这”;关键字在C#中有?
C#中的关键字多少上下文“;这”;关键字在C#中有?,c#,this,C#,This,C#中的关键字this与其他语言略有不同。我在谷歌上搜索了一段时间,但我找不到一篇能概括所有内容的文章 如何在C#中使用this?您可以这样使用它: 假设您有一个类,其中您有一个变量,该变量的名称与您给某个方法的参数的名称相同,当您希望引用该方法之外的变量时,在该类中您使用this.variable 例如: class C: private str var; public C(string var) { this.var = var; } 正如我
this
与其他语言略有不同。我在谷歌上搜索了一段时间,但我找不到一篇能概括所有内容的文章
如何在C#中使用
this
?您可以这样使用它:
假设您有一个类,其中您有一个变量,该变量的名称与您给某个方法的参数的名称相同,当您希望引用该方法之外的变量时,在该类中您使用this.variable
例如:
class C:
private str var;
public C(string var)
{
this.var = var;
}
正如我所说,this.var
指的是“外部”字符串,var
指的是方法的cointext中的字符串,在本例中是构造函数
“this”有两种含义,您(我假设)熟悉“this”,它是指您所在类的当前实例的关键字,然后作为扩展方法中的关键字: e、 g
在后一种情况下,“this”关键字用于表示您正在向强类添加扩展方法 什么是“这”?它是许多面向对象编程语言中用来指代当前对象的关键字。它可以实现为指针(f.e.C++)或引用(f.e.Java),但在C#中有一些不同的含义 课程
class Foo
{
// with “this” modifiler we can use it like instance method of Person
public static void SayName(this Person person)
{
Console.WriteLine(“My name is {0}”, person.Name);
}
}
struct MyVeryOwnInteger
{
private int x;
public int Gimme()
{
// inside struct this is treated as a variable
// not reference to current structure
return this.x;
}
我们可以在3种不同的上下文中使用“this”
- 打电话给其他承包商
- 请参阅实例字段或方法
- 传递当前对象
public class Person
{
private string name;
public Person()
{
name = "John";
}
public Person(string name)
: this() // call other constructor
{
// this is used to qualify the field
// “name” is hidden by parameter
this.name = name;
}
public string Name
{
get { return name; }
}
private void sayHi()
{
Console.WriteLine("Hi");
Foo.SayName(this); //use this to pass current object
}
public void Speak()
{
this.sayHi(); // use this to refer to an instance method
Console.WriteLine("Want to come up and see my etchings? ");
}
}
class Foo
{
public static void SayName(Person person)
{
Console.WriteLine("My name is {0}", person.Name);
}
}
索引器
class Foo
{
// with “this” modifiler we can use it like instance method of Person
public static void SayName(this Person person)
{
Console.WriteLine(“My name is {0}”, person.Name);
}
}
struct MyVeryOwnInteger
{
private int x;
public int Gimme()
{
// inside struct this is treated as a variable
// not reference to current structure
return this.x;
}
为类型定义索引器,使其具有类似数组的语义
public int this[int index]
{
get { return array[index]; }
set { array[index] = value; }
}
扩展方法
class Foo
{
// with “this” modifiler we can use it like instance method of Person
public static void SayName(this Person person)
{
Console.WriteLine(“My name is {0}”, person.Name);
}
}
struct MyVeryOwnInteger
{
private int x;
public int Gimme()
{
// inside struct this is treated as a variable
// not reference to current structure
return this.x;
}
结构
class Foo
{
// with “this” modifiler we can use it like instance method of Person
public static void SayName(this Person person)
{
Console.WriteLine(“My name is {0}”, person.Name);
}
}
struct MyVeryOwnInteger
{
private int x;
public int Gimme()
{
// inside struct this is treated as a variable
// not reference to current structure
return this.x;
}
@亚当C++也一样。除了C++之外,它是一个指针而不是一个引用。为什么不搜索C的“这个”关键字?“亚当,不,它不是一个引用或者变量。answer@Jay是的,我这样做了,我分享了我收集到的信息。如果有人能解释投票失败的原因,我会非常高兴。