Use of "This" Keyword in Formal Parameters for Static Methods in C#

Use of this keyword in formal parameters for static methods in C#

This is an extension method. See here for an explanation.

Extension methods allow developers to add new methods to the public
contract of an existing CLR type, without having to sub-class it or
recompile the original type. Extension Methods help blend the
flexibility of "duck typing" support popular within dynamic languages
today with the performance and compile-time validation of
strongly-typed languages.

Extension Methods enable a variety of useful scenarios, and help make
possible the really powerful LINQ query framework... .

it means that you can call

MyClass myClass = new MyClass();
int i = myClass.Foo();

rather than

MyClass myClass = new MyClass();
int i = Foo(myClass);

This allows the construction of fluent interfaces as stated below.

Usage of this keyword inside parameter c#

The extension method is called like an instance method, but is actually a static method. The instance pointer "this" is a parameter.

And:
You must specify the this-keyword before the appropriate parameter you want the method to be called upon.

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);
}
}

see http://www.dotnetperls.com/extension for more info.

**Edit: try below example once and again with commenting

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

to see the result**

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);
}
}
}

How does C# this keyword work to qualify argument in static method?

GetFormattedText() is not a normal method, but an extension method.

You can call it like this:

 document.GetFormattedText()

or this:

 ClassName.GetFormattedText(document)

Why keyword 'this' cannot be used in a static method?

Because this points to an instance of the class, in the static method you don't have an instance.

The this keyword refers to the current instance of the class. Static member functions do not have a this pointer

You'll notice the definition of a static member is

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object

Which is why this has nothing to point to.

What does the this keyword in a method declaration mean?

It's an extension method. Extension methods allow you to extend a type. It's great for types like string for which you don't have access to the source code.

With the example you provided, instead of calling

string test = "foo";
var result = StaticClass.WordCount(test);

You could use it as follows:

string test = "foo";
var result = test.WordCount();

Trivia: LINQ is implemented using extension methods, and in fact was the primary reason extension methods were added to the .NET framework.

Is using this keyword with every class variable of class is good practice?

It wont effect your compile time, or your run time.
it's not a good practice neither.

the reason for using this keyword is to separate between class props then to another properties inside a particular section.

If your talking about code convention and styling, just adopt Microsofts one:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions

you should read Microsoft documentation:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this



Related Topics



Leave a reply



Submit