Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin "apply" equivalent in C#

Tags:

c#

kotlin

When using Kotlin, one could use apply to set multiple properties of an existing object and keeping the code cleaner, for example instead of:

person.firstName = "John"
person.lastName = "Doe"
person.phone = "123 456 789"

We can use:

person.apply {
   firstName = "John"
   lastName = "Doe"
   phone = "123 456 789"
}

Is there an equivalent to the apply in C#?

The closest to this is the using but it can't be used this way as far as I know.

Edit: I know of object initializer in C#, but I'm actually looking for something that can be done for existing objects (for example an object fetched from the database).

like image 225
Ziad Akiki Avatar asked Mar 14 '20 19:03

Ziad Akiki


People also ask

What is kotlin apply function?

Android Online Course for Professionals by MindOrks apply accepts an instance as the receiver while with requires an instance to be passed as an argument. In both cases, the instance will become this within a block. apply returns the receiver and with returns a result of the last expression within its block.

What is difference between apply and run in Kotlin?

Kotlin apply vs with with runs without an object(receiver) whereas apply needs one. apply runs on the object reference, whereas with just passes it as an argument. The last expression of with function returns a result.

Why would you use apply in Kotlin?

In Kotlin, apply is an extension function on a particular type and sets its scope to object on which apply is invoked. Apply runs on the object reference into the expression and also returns the object reference on completion.

What is with in Kotlin?

Calls the specified function block with the given receiver as its receiver and returns its result.


Video Answer


2 Answers

Try this.... https://dev.to/amay077/kotlins-scope-functions-in-c-pbn

Code pasted below for convenience, but the above link is the source...

static class ObjectExtensions 
{
  // Kotlin: fun <T, R> T.let(block: (T) -> R): R
  public static R Let<T, R>(this T self, Func<T, R> block) 
  {
    return block(self);
  }

  // Kotlin: fun <T> T.also(block: (T) -> Unit): T
  public static T Also<T>(this T self, Action<T> block)
  {
    block(self);
    return self;
  }   
}

Can be used like this....

var model = new MyModel().Also(m => {
  m.Initialize();
  m.Load(path);
});
like image 109
user2430020 Avatar answered Sep 20 '22 11:09

user2430020


You can use it in this way with object initializers:

var person = new Person
{
   FirstName = "John",
   LastName = "Doe",
   Phone = "123 456 789"
};

Or with a constructor:

var person = new Person("John", "Doe", "123 456 789");

Your class would have to look like this for the constructor option:

class Person
{
    public Person(string firstName, string lastName, string phone)
    {
        FirstName = firstName;
        LastName = lastName;
        Phone = phone;
    }

    public string FirstName { get;set; }
    public string LastName { get;set; }
    public string Phone { get;set; }
}
like image 38
Guapo Avatar answered Sep 24 '22 11:09

Guapo