DEV Community

Cover image for SpreadArray CSharp
Alexandre Freire
Alexandre Freire

Posted on

4 1

SpreadArray CSharp

I decided to publish an extension that simulates a javascript spread function.

When I started using c # I missed some features that I used in javascript, I know they are different things and that c # is a strongly typed language.

But I'll leave the link to the package that I created and use in some projects, and that solves some problems.

Spread Array CSharp

The spread operator is denoted by three dots (…)(popular in javascript). · The spread operator unpacks elements of iterable objects such as arrays, sets, and maps into a list.

Example

var user = new User("Alexandre");
var userAddress = new UserAddress("Porto Velho", "Brasil");
var contact = new Contact("my@mail.com");

dynamic dynamicUserProfile = new ExpandoObject();
dynamicUserProfile = user.Spread(userAddress).Spread(contact);

/// Example 1: preparing to json response
JObject json = JObject.FromObject(new { dynamicUserProfile });

/// Example 2: setting value on richTextBox from windows application
richTextBox1.Text = json.SelectToken("dynamicUserProfile").ToString();
Enter fullscreen mode Exit fullscreen mode
    public class UserAddress
    {
        public UserAddress(string city, string country)
        {
            City = city;
            Country = country;
        }

        public string City { get; set; }
        public string Country { get; set; }
    }

    public class User
    {
        public User(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

    public class Contact
    {
        public Contact(string email)
        {
            Email = email;
        }

        public string Email { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay