What is the meaning of the keyword "this"?

Very short C # function.

    public static int SizeInBytes(this byte[] a)
    {
        return sizeof (int) + a.Length*sizeof (byte);
    }

What does the keyword "this" mean in this function? What is equivalent to this keyword in C ++? Also, what is this function trying to accurately calculate?

+5
source share
2 answers

He marks the method as an extension method .

The extension method allows you to extend the functionality of any class, even if it is sealed.

Example:

public static class StringExtensions
{
    public static bool IsEmpty (this string s)
    {
        return s == string.Empty;
    }
}

, this.

++..... . ++ , ++, .

+8

, #. ++.

, byte[], buffer, - (, ), :

int buffSize = buffer.SizeInBytes();

- ( ), byte[] . , ++, , #.

+7

All Articles