آموزش برنامه نویسی با سی شارپ C# - قسمت 12 – this
زمان مطالعه: 1 دقیقه
زمان ویدیو:
5m23s
لینک یوتیوب
لینک آپارات

آشنایی با this
Car honda = new(120f);
honda.Drive();
Car bmw = new(160f);
bmw.Drive();
class Car
{
public float speed = 99f;
public Car(float speed)
{
this.speed = speed;
}
public void Drive()
{
Console.WriteLine($"Driving {speed}kmph...");
}
}
استفاده از this در return
public Car IncreaseSpeed(float speed) {
this.speed += speed;
return this;
}
Car honda = new(120f);
honda.Drive();
honda.IncreaseSpeed(10);
honda.Drive();
Car honda = new(120f);
honda.Drive();
honda.IncreaseSpeed(10).Drive();
honda.IncreaseSpeed(100).Drive();
honda.IncreaseSpeed(10).Drive();
Car honda = new(120f);
honda.IncreaseSpeed(10).IncreaseSpeed(20).IncreaseSpeed(30).Drive();
Car honda = new(120f);
honda.IncreaseSpeed(10);
honda.IncreaseSpeed(20);
honda.IncreaseSpeed(30);
honda.Drive();
استفاده از this به عنوان ورودی متد ها
class Car
{
public float speed = 99f;
Parking parking = new();
public Car(float intialSpeed)
{
this.speed = intialSpeed;
}
public void ParkMe()
{
parking.Park(this);
}
}
بی معنی بودن استفاده از this یه سری از جاها
public Car(float intialSpeed)
{
this.speed = intialSpeed;
}
public float GetSpeed()
{
return this.speed;
}