C # Extension methods (Extend method)

Source: Internet
Author: User

C # Extension methods (Extend method)


Add a method to the class without changing the original class.
1, the extension method must be written in a static class
2, the extension method must be a static method, although it is a static method, but this expansion method is extended for the object, can only be called by the object.
public static class name
{
public static return value method name (this is the type object name to extend [, parameter list])
{

}
}


Static Class Extend
{
public static TSource test123<tsource> (this ienumerable<tsource> source, Func<tsource, bool> predicate)
{

foreach (TSource item in Source)
{
if (predicate (item))
{
return (item);
}
}
throw new Exception ("not Found!");
}
}


List<int> ints =new list<int> {1,2,3,4,5,6};

int nret = INTs. Test123 (o = > 5);//nret=6

Attached: extension classes for NHibernate

Using NHibernate;
Using Nhibernate.type;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Linq.Expressions;
Using System.Runtime.CompilerServices;

Namespace Nhibernate.linq
{
public static Class Linqextensionmethods
{
public static iqueryable<t> cacheable<t> (this iqueryable<t> query);
public static iqueryable<t> cachemode<t> (this iqueryable<t> query, CacheMode CacheMode);
public static iqueryable<t> cacheregion<t> (this iqueryable<t> query, string region);
public static T-mappedas<t> (This t parameter, IType type);
public static iqueryable<t> Query<t> (this isession session);
public static iqueryable<t> Query<t> (this istatelesssession session);
public static iqueryable<t> Query<t> (this isession session, String entityname);
public static iqueryable<t> Query<t> (this istatelesssession session, String entityname);
public static iqueryable<t> timeout<t> (this iqueryable<t> query, int Timeout);
public static ienumerable<t> tofuture<t> (this iqueryable<t> query);
public static ifuturevalue<t> tofuturevalue<t> (this iqueryable<t> query);
public static ifuturevalue<tresult> Tofuturevalue<t, tresult> (this iqueryable<t> query, Expression <func<iqueryable<t&gt, tresult>> selector);
}
}

C # Extension methods (Extend method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.