TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Extension Method

Use a custom extension method. Learn extension method syntax with examples.
Extension. An extension method has simplified calling syntax. It represents a static method as an instance method. Extension methods can lead to simpler syntax.
This keyword. An extension method uses the this-keyword in its parameter list. It must be located in a static class. We often use special "Extensions" classes.This
An example. Here is a custom extension method. Usually you will want to store your extension method class in a separate source file (like ExtensionMethods.cs) in the project.

Note: This file should store a static class with public static extension methods.

Public, private

Then: In the rest of your source code, you can invoke these extension methods in the same way as instance methods.

Static: An extension method must be static and can be public so you can use it anywhere in your source code.

Static

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

C# program that uses extension method on string using System; public static class ExtensionMethods { public static string UppercaseFirstLetter(this string value) { // Uppercase the first letter in the string. 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 Codex"; value = value.UppercaseFirstLetter(); Console.WriteLine(value); } } Output Dot net Codex
Second parameter. Here is an extension method that acts on ints. It receives a parameter—the multiplier for the operand. It returns another int so can be used in function call chains.Return
C# program that uses extension method, 2 parameters using System; static class Extensions { public static int MultiplyBy(this int value, int mulitiplier) { // Uses a second parameter after the instance parameter. return value * mulitiplier; } } class Program { static void Main() { // Ten times 2 is 20. // Twenty times 2 is 40. int result = 10.MultiplyBy(2).MultiplyBy(2); Console.WriteLine(result); } } Output 40
Notes, keyword. The only difference in the declaration between a regular static method and an extension method is the "this" keyword in the parameter list.

Important: If you want the method to receive other parameters, you can include those at the end.

LINQ. There are many extension methods available. These extension methods were written by Microsoft developers and are available in all C# programs targeting recent .NET Framework versions.

Tip: On most of the extension methods, you need to add the "using System.Linq" directive to the top of the code.

LINQ
A discussion. Extension methods can have many arguments. We can use variable "params" arguments with extension methods. Extension methods are static methods—so there is no performance loss.Params

So: Extension methods affect the high-level representation of the code, not the low-level implementation.

A summary. You can add extension methods to any type, even a value type. The original representation of the type does not change. Extension methods affect syntax, not execution.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf