Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is this madness? The pursuit of single-statement methods

Is this madness? The pursuit of single-statement methods

Scheduled Pinned Locked Moved C#
performancequestion
22 Posts 12 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rob Philpott

    Yeah, it's tricky isn't it. Documentation doesn't seem to state that order is guaranteed not to change, but implementation indicates that's the case. I can't really imagine how you can improve much on that implementation either (which is strikingly similar to the initial posted implementation), so it's probably fair to assume no reordering will occur. But without that cast-iron guarantee, a future version of .NET could scupper things. Well hey, that's what consultancy rates are for. The bit which has me intrigued now is the Set class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.

    Regards, Rob Philpott.

    Richard DeemingR Online
    Richard DeemingR Online
    Richard Deeming
    wrote on last edited by
    #21

    Rob Philpott wrote:

    The bit which has me intrigued now is the Set<t> class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.

    Yes, it's an internal class within the Enumerable class: Enumerable.Set<TElement>[^] I'd have used a HashSet<T> as well, but I guess MS were probably writing the Distinct method before the HashSet class was finished. Either that, or there were some very specific performance issues they were trying to work around. But if that was the case, I'd have expected to see a comment explaining the problem they were trying to solve.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    1 Reply Last reply
    0
    • K Kevin Li Li Ken un

      My code started out as something fairly easy to follow like this:

      void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
      {
      // Create a set to hold a collection of lines that have already been processed.
      SortedSet seen = new SortedSet();
      // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
      StringBuilder stringBuilder = new StringBuilder();
      // Split the text from the text box by lines and examine each one.
      foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
      {
      // If the string is successfully added to the set, then it has not been processed before.
      if (seen.Add(line))
      {
      // Add the line to the output.
      stringBuilder.AppendLine(line);
      }
      }
      // Build the string and assign it to the text box.
      this.InputTextBox.Text = stringBuilder.ToString();
      }

      Then I factored out some of the code into extension methods, removed the unnecessary curly braces, and got this:

      void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
      {
      // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
      StringBuilder stringBuilder = new StringBuilder();
      // Split the text from the text box into a collection of unique lines.
      foreach (string line in this.InputTextBox.Text.SplitLines().Unique())
      // Add the line to the output.
      stringBuilder.AppendLine(line);
      // Build the string and assign it to the text box.
      this.InputTextBox.Text = stringBuilder.ToString();
      }

      And then I took it a step further.

      void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) =>
      // Take the text from the text box, split it by lines, remove the duplicates, and assign the results to the text box.
      this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(
      new StringBuilder(),
      (StringBuilder stringBuilder, string line) => stringBuilder.AppendLine(line),
      (StringBuilder stringBuilder) => stringBuilder.ToString());

      And the one-liner:

      void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());

      I get great joy a

      B Offline
      B Offline
      BenScharbach
      wrote on last edited by
      #22

      Interesting!! I have never seen a method, like your RemoveDuplicatesButton_Click() use the => syntax at this point?! That actually works?

      Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups