国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Syntax of C# Extension Methods
Working of C# Extension Methods
Examples to Implement C# Extension Methods
Example #3
Conclusion
Recommended Article

C# Extension Methods

Sep 03, 2024 pm 03:32 PM
c# c# tutorial

As per the literal meaning of extension, the additional methods are called C# Extension Methods using which the additional methods can be added without doing any changes or inheriting or restructuring the original struct, class, or interface and we can add such extension methods to the custom classes created by us, the classes of .NET framework or to the classes from third party or interfaces and these extension methods are accessible throughout the flow of the program by including the namespaces in which they are defined and it is a static method of a special kind defined in a static class.

Syntax of C# Extension Methods

Defining the namespace, class, and extension method.

Syntax:

namespace namespace_name
{
public static class class_name
{
public static bool extension_method_name(parameters_list)
{
//Blocks of code
}
}
}

Where namespace_name is the name of the namespace within which the extension method is defined.

Class_name is the name of the static class in which the extension method is defined.

Extension_method_name is the name of the extension method.

The parameter list is the list of parameters with the first parameter being the type of the operator the method is going to operate on which will have a prefix this keyword.

Working of C# Extension Methods

  • The Extension methods are the custom methods that are created additionally and are not part of the original class.
  • A namespace is defined within which a static class is defined and then the extension method is defined within the static class. By using the namespace within which the extension method is defined, the method can be used throughout the application.
  • The extension method is a special case of the static method defined inside a static class whose first parameter is the type of the operator it is going to operate on with a prefix this keyword.
  • The extension methods can be included in .NET framework classes, custom classes, structs or interfaces, third-party classes.

Examples to Implement C# Extension Methods

Below are the examples of C# Extension Methods

Example #1

C# program to demonstrate the Extension method in a program to compare two integers:

?Code:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to compare two integers is defined
public static bool extensionmethodname(this intstr, intval)
{
return str>val;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
intstri = 565;
//extension method defined in another static class is called here
bool z = stri.myExtensionMethod(200);
Console.WriteLine("The result of the comparison is: {0}", z);
Console.ReadLine();
}
}
}

Output:

C# Extension Methods

Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to compare two integers is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the result of the comparison of two integers. The output of the snapshot is as shown in the snapshot above.

Example #2

C# program to demonstrate the Extension method in a program to find out the length of a string:

?Code:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to find out the length of a string is defined
public static intextensionmethodname(this string str)
{
return str.Length;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
string stri = "ShobhaShivakumar";
//extension method defined in another static class is called here
int z = stri.extensionmethodname();
Console.WriteLine("The length of the string obtained by using extension method is: {0}", z);
Console.ReadLine();
}
}
}

Output:

C# Extension Methods

Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to compute the length of the string passed as a parameter to it, is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the length of the string passed as a parameter to it as the result. The output of the snapshot is as shown in the snapshot above.

Example #3

Code:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to add two numbers is defined
public static intextensionmethodname(this intstr, intval)
{
return str+val;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
intstri = 100;
//extension method defined in another static class is called here
int z = stri.extensionmethodname(200);
Console.WriteLine("The result of addition of two numbers obtained by using extension method is: {0}", z);
Console.ReadLine();
}
}
}

Output:

C# Extension Methods

Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to add two numbers passed as a parameter to it, is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the result after adding the two numbers.

Conclusion

In this tutorial, we understand the concept of C# Extension Methods through definition, its syntax, and working through programming examples and their outputs.

Recommended Article

This is a guide to C# Extension Methods. Here we discuss the Introduction to C# Extension Methods and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –

  1. Random Number Generator in C#
  2. Static Constructor in Java
  3. TextWriter in C#
  4. Static Constructor in C#

The above is the detailed content of C# Extension Methods. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how?Random Number Generator work, concept of pseudo-random and secure numbers.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

C# vs. C  : History, Evolution, and Future Prospects C# vs. C : History, Evolution, and Future Prospects Apr 19, 2025 am 12:07 AM

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

How to change the format of xml How to change the format of xml Apr 03, 2025 am 08:42 AM

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ??such as Python. Be careful when modifying and back up the original files.

Palindrome in C# Palindrome in C# Sep 03, 2024 pm 03:34 PM

Guide to Palindrome in C#. Here we discuss the introduction and logic behind palindrome in C#? along with the various methods with its code.

See all articles