15

Is there an equivalent of this interface in C#?

Example:

Consumer<Byte> consumer = new Consumer<>();
consumer.accept(data[11]);

I have searched around Func<> and Action<> but I have no idea.

The original Java code of Consumer.accept() interface is pretty simple. But not for me:

void accept(T t);

/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation.  If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
}
user11909
  • 1,235
  • 11
  • 27
vil.coyote.ch
  • 193
  • 1
  • 8
  • 1
    It is java code and we are c# programer and this interface is not the simple to us : ) Waht this cosumer do? – blogprogramisty.net Apr 06 '16 at 11:10
  • 2
    Any delegate type that takes one parameter and does not return a value would be a candidate. `Action` is one of them. – Dennis_E Apr 06 '16 at 11:17
  • Thanks Dennis, but How can I substitue accept methode ? – vil.coyote.ch Apr 06 '16 at 11:19
  • 1
    What are you looking for? The question doesn't make any sense as it is. There are lambdas, callbacks, Observables, LINQ all of which could do whatever you use `Consumer.accept` for – Panagiotis Kanavos Apr 06 '16 at 11:22
  • @vil.coyote.ch In Java, the methods have different names. In case of `Consumer`, it's `accept`. In C#, the method name is always the same: `Invoke`. But you can omit it: `action(parameter)` is shorthand for `action.Invoke(parameter)`. – Dennis_E Apr 06 '16 at 11:24
  • BTW from the *code* it looks like LINQ - `andThen` is the important part, not the implementation mechanism (Consume). Is this applying a filter or chaining steps? LINQ offers `Where` for filters. Almost all LINQ methods can be chained – Panagiotis Kanavos Apr 06 '16 at 11:25
  • Also, you could use an iterator to consume one IEnumerable and return another. – Panagiotis Kanavos Apr 06 '16 at 11:26
  • @Dennis_E Invoke does nothing like what `Consume` shows. It just calls a delegate. Consume though, consumes something – Panagiotis Kanavos Apr 06 '16 at 11:27
  • I think it would be like the following extension method `public static Action AndThen(this Action before, Action after) { return t => { before(t); after(t); }; }` C# has extension methods instead of default methods. – juharr Apr 06 '16 at 11:29
  • 2
    @PanagiotisKanavos `action.Invoke(parameter)` is the same thing as `consumer.accept(parameter)`. They just gave them different names. But I realize that's not the op's question. – Dennis_E Apr 06 '16 at 11:35
  • @Dennis_E the semantics are completely different. `Consume` *accepts* data from another consumer before doing something on it. Invoke simply executes a delegate. Chaining like `Consume` though can be found in a *lot* of C# features - iterators accept and return IEnumerables. Observables accept data from and expose it to other observables and subscribers. Tasks do the same as well. At its simplest form, even nesting Func<> calls can do the same. – Panagiotis Kanavos Apr 06 '16 at 11:43
  • @PanagiotisKanavos Are we talking about the same thing? You're talking about something called `Consume`, which doesn't appear in the question anywhere. I'm talking about a type called `Consumer` and a method called `accept`. – Dennis_E Apr 06 '16 at 11:52

2 Answers2

19

"Consumer interface represents an operation that accepts a single input argument and returns no result"

Well, provided that the quote above taken from here is accurate it’s roughly an equivalent of Action<T> delegate in C#;

For example this java code:

import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    Consumer<String> c = (x) -> System.out.println(x.toLowerCase());
    c.accept("Java2s.com");
  }
}

Converted to C# would be:

using System;

public class Main
{
  static void Main(string[] args)
  {
     Action<string> c = (x) => Console.WriteLine(x.ToLower());
     c.Invoke("Java2s.com"); // or simply c("Java2s.com");
  }
}
Fabjan
  • 13,506
  • 4
  • 25
  • 52
3

Consumer<T> corresponds to Action<T> and the andThen method is a sequencing operator. You can define andThen as an extension method e.g.

public static Action<T> AndThen<T>(this Action<T> first, Action<T> next)
{
    return e => { first(e); next(e); };
}
Lee
  • 142,018
  • 20
  • 234
  • 287