c# - Odd return syntax statement -


i know may sound strange don't know how search syntax in internet , not sure means.

so i've watched on morelinq code , noticed method

public static ienumerable<tsource> distinctby<tsource, tkey>(this ienumerable<tsource> source,         func<tsource, tkey> keyselector, iequalitycomparer<tkey> comparer)     {         if (source == null) throw new argumentnullexception(nameof(source));         if (keyselector == null) throw new argumentnullexception(nameof(keyselector));          return _(); ienumerable<tsource> _()         {             var knownkeys = new hashset<tkey>(comparer);             foreach (var element in source)             {                 if (knownkeys.add(keyselector(element)))                     yield return element;             }         }     } 

what odd return statement? return _(); ?

this c# 7.0 supports local functions....

public static ienumerable<tsource> distinctby<tsource, tkey>(        ienumerable<tsource> source,         func<tsource, tkey> keyselector, iequalitycomparer<tkey> comparer)     {         if (source == null) throw new             argumentnullexception(nameof(source));         if (keyselector == null) throw               new argumentnullexception(nameof(keyselector));          // executing _localfunction()         return _localfunction();           // new inline method,          // return within within scope of         // method         ienumerable<tsource> _localfunction()         {             var knownkeys = new hashset<tkey>(comparer);             foreach (var element in source)             {                 if (knownkeys.add(keyselector(element)))                     yield return element;             }         }     } 

current c# func<t>

public static ienumerable<tsource> distinctby<tsource, tkey>(        ienumerable<tsource> source,         func<tsource, tkey> keyselector, iequalitycomparer<tkey> comparer)     {         if (source == null) throw new             argumentnullexception(nameof(source));         if (keyselector == null) throw               new argumentnullexception(nameof(keyselector));          func<ienumerable<tsource>> func = () => {             var knownkeys = new hashset<tkey>(comparer);             foreach (var element in source)             {                 if (knownkeys.add(keyselector(element)))                     yield return element;             }        };          // executing func         return func();       } 

the trick is, _() declared after used, fine.

pratical use of local functions

above example demonstration of how inline method can used, if going invoke method once, of no use.

but in example above, mentioned in comments phoshi , luaan, there advantage of using local function. since function yield return not executed unless iterates it, in case method outside local function executed , parameter validation performed if no 1 iterate value.

many times have repeated code in method, lets @ example..

  public void validatecustomer(customer customer){        if( string.isnullorempty( customer.firstname )){            string error = "firstname cannot empty";            customer.validationerrors.add(error);            errorlogger.log(error);            throw new validationerror(error);       }        if( string.isnullorempty( customer.lastname )){            string error = "lastname cannot empty";            customer.validationerrors.add(error);            errorlogger.log(error);            throw new validationerror(error);       }        ... on  , on...    } 

i optimize with...

  public void validatecustomer(customer customer){        void _validate(string value, string error){            if(!string.isnullorwhitespace(value)){                // can reference customer here               customer.validationerrors.add(error);                errorlogger.log(error);               throw new validationerror(error);                               }       }        _validate(customer.firstname, "firstname cannot empty");       _validate(customer.lastname, "lastname cannot empty");       ... on  , on...    } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -