Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:56:41+00:00 2026-05-26T19:56:41+00:00

class my_class { public int add_1(int a, int b) {return a + b;} public

  • 0
class my_class
{
    public int add_1(int a, int b) {return a + b;}
    public func<int, int, int> add_2 = (a, b) => {return a + b;}
}

add_1 is a function whereas add_2 is a delegate. However in this context delegates can forfill a similar role.

Due to precedent and the design of the language the default choice for C# methods should be functions.

However both approaches have pros and cons so I’ve produced a list. Are there any more advanteges or disadvantages to either approach?

Advantages to conventional methods.

  • more conventional
  • outside users of the function see named parameters – for the add_2 syntax arg_n and a type is generally not enough information.
  • works better with intellisense – ty Minitech
  • works with reflection – ty Minitech
  • works with inheritance – ty Eric Lippert
  • has a “this” – ty CodeInChaos
  • lower overheads, speed and memory – ty Minitech and CodeInChaos
  • don’t need to think about public\private in respect to both changing and using the function. – ty CodeInChaos
  • less dynamic, less is permitted that is not known at compile time – ty CodeInChaos

Advantages to “field of delegate type” methods.

  • more consistant, not member functions and data members, it’s just all just data members.
  • can outwardly look and behave like a variable.
  • storing it in a container works well.
  • multiple classes could use the same function as if it were each ones member function, this would be very generic, concise and have good code reuse.
  • straightforward to use anywhere, for example as a local function.
  • presumably works well when passed around with garbage collection.
  • more dynamic, less must be known at compile time, for example there could be functions that configure the behaviour of objects at run time.
  • as if encapsulating it’s code, can be combined and reworked, msdn.microsoft.com/en-us/library/ms173175%28v=vs.80%29.aspx
  • outside users of the function see unnamed parameters – sometimes this is helpfull although it would be nice to be able to name them.
  • can be more compact, in this simple example for example the return could be removed, if there were one parameter the brackets could also be removed.
  • roll you’r own behaviours like inheritance – ty Eric Lippert
  • other considerations such as functional, modular, distributed, (code writing, testing or reasoning about code) etc…

Please don’t vote to close, thats happened already and it got reopened. It’s a valid question even if either you don’t think the delegates approach has much practical use given how it conflicts with established coding style or you don’t like the advanteges of delegates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:56:41+00:00Added an answer on May 26, 2026 at 7:56 pm

    First off, the “high order bit” for me with regards to this design decision would be that I would never do this sort of thing with a public field/method. At the very least I would use a property, and probably not even that.

    For private fields, I use this pattern fairly frequently, usually like this:

    class C
    {
        private Func<int, int> ActualFunction = (int y)=>{ ... };
        private Func<int, int> Function = ActualFunction.Memoize();
    

    and now I can very easily test the performance characteristics of different memoization strategies without having to change the text of ActualFunction at all.

    Another advantage of the “methods are fields of delegate type” strategy is that you can implement code sharing techniques that are different than the ones we’ve “baked in” to the language. A protected field of delegate type is essentially a virtual method, but more flexible. Derived classes can replace it with whatever they want, and you have emulated a regular virtual method. But you could build custom inheritence mechanisms; if you really like prototype inheritance, for example, you could have a convention that if the field is null, then a method on some prototypical instance is called instead, and so on.

    A major disadvantage of the methods-are-fields-of-delegate-type approach is that of course, overloading no longer works. Fields must be unique in name; methods merely must be unique in signature. Also, you don’t get generic fields the way that we get generic methods, so method type inference stops working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this in my application: class A { public: int
Say I have this class: class myclass { public int Field1{ get; set; }
Given this definition: [UniqueId(Ident)] public class MyClass : MyBase { public int Ident{ get;
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
these are my entities (simplified): public class IdentificationRequest { private int id; private ICollection<IdentificationRequestStateHistoryItem>
public class MyClass { public int Age; public int ID; } public void MyMethod()
Assuming public class MyClass { public int ID {get; set; } public string Name
class MyClass { public: void method2() { static int i; ... } }; Will
I have the following pattern: template <int a, int b> class MyClass { public:
I have my class public function convert( $title ) { $nameout = strtolower( $title

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.