Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence multiplication in c# [duplicate]

Tags:

python

c#

Possible Duplicate:
Is there an easy way to return a string repeated X number of times?

In Python you can multiply sequences like this

fivespaces= ' ' * 5

Is there any built-in equivalent for this in C#? (without operator overloads or class extensions)


1 Answers

If it's just a string then you can return multiples by passing in a count to string()

var fivespaces = new string(" ", 5);

In the case where you want a collection of something else like a custom type, you can use Enumerable.Repeat to get a collection:

var items = Enumerable.Repeat(new SomeModel(), 5);

like image 116
Jamie Dixon Avatar answered Mar 10 '26 01:03

Jamie Dixon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!