参数c#中此关键字的用法

约翰·谭

我有一堂课:

public static class PictureBoxExtensions
{
    public static Point ToCartesian(this PictureBox box, Point p)
    {
        return new Point(p.X, p.Y - box.Height);
    }

    public static Point FromCartesian(this PictureBox box, Point p)
    {
        return new Point(p.X, box.Height - p.Y);
    }
}

我的问题是,与省略关键字相比,this关键字前面PictureBox的关键字有什么用?

马苏德·穆罕默迪(Masoud Mohammadi)

扩展方法的调用类似于实例方法,但实际上是静态方法。实例指针“ this”是一个参数。

并且:您必须在要调用该方法的适当参数之前指定this-keyword。

public static class ExtensionMethods
{
    public static string UppercaseFirstLetter(this string value)
    {
        // Uppercase the first letter in the string this extension is called on.
        if (value.Length > 0)
        {
            char[] array = value.ToCharArray();
            array[0] = char.ToUpper(array[0]);
            return new string(array);
        }
        return value;
    }
}

class Program
{
    static void Main()
    {
        // Use the string extension method on this value.
        string value = "dot net perls";
        value = value.UppercaseFirstLetter(); // Called like an instance method.
        Console.WriteLine(value);
    }
}

有关更多信息,请参见http://www.dotnetperls.com/extension

**编辑:尝试以下示例,并再次添加评论

pb.Location=pb.FromCartesian(new Point(20, 20)); 

查看结果**

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox pb = new PictureBox();
            pb.Size = new Size(200, 200);
            pb.BackColor = Color.Aqua;
            pb.Location=pb.FromCartesian(new Point(20, 20));
            Controls.Add(pb);
        }
    }

    public static class PictureBoxExtensions
    {
        public static Point ToCartesian(this PictureBox box, Point p)
        {
            return new Point(p.X, p.Y - box.Height);
        }

        public static Point FromCartesian(this PictureBox box, Point p)
        {
            return new Point(p.X, box.Height - p.Y);
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C#中“内部”关键字的实际用法

来自分类Dev

C#中的“ This”关键字

来自分类Dev

static关键字及其在C ++中的各种用法

来自分类Dev

C#构造函数重载(使用此关键字)

来自分类Dev

此C#语句的VB.NET版本中是否需要未选中的关键字?

来自分类Dev

python中'nonlocal'关键字的用法

来自分类Dev

javascript中const关键字的用法

来自分类Dev

Java中易变的关键字用法

来自分类Dev

使用 IN 关键字 c# 参数化查询

来自分类Dev

在C#中静态方法的形式参数中使用“ this”关键字

来自分类Dev

在 Javascript 中修改此关键字

来自分类Dev

关键字参数中的参考参数

来自分类Dev

函数参数中的 C++ 枚举关键字

来自分类Dev

python setup.py中“ provides”关键字参数的用法

来自分类Dev

在C#中模拟F#with关键字

来自分类Java

Java中的C#'var'关键字等效吗?

来自分类Java

与Java中的C#关键字“ as”等效

来自分类Dev

了解C#中的非托管关键字

来自分类Dev

在C#中,'volatile'关键字是否仍然无效?

来自分类Dev

在C#中取消定义关键字

来自分类Java

关键字用法实例

来自分类Java

Java关键字this的用法

来自分类Java

方法参数中的final关键字

来自分类Dev

Swift参数中的默认关键字

来自分类Dev

在Racket中打包关键字参数?

来自分类Python

Python中的动态关键字参数?

来自分类Dev

模板参数中的const关键字

来自分类Dev

/ signup'user_password'中的Django TypeError是此函数的无效关键字参数

来自分类Linux

yocto食谱中“ inherit”关键字和“ DEPENDS”关键字的用法