F# equivalent of C#’s Object Initialization syntax

Yan Cui

I help clients go faster for less using serverless technologies.

In C#, you can use the object/collection initialization syntax like this:

image

The F# equivalent of object initialization is done like this:

image

As for collection initialization, you have a far more diverse range of tools available to you, for example:

image

You can also create slices of an existing array:

image

You can even add your own multi-dimensional indexers to support similar behaviours in your type too!

Update 2012/01/11:

As Robert Pickering mentioned in the comments, slicing is not limited to arrays, it works with other collection types too (as long as a GetSlice extension method is defined on the type, for more information, see here). For instance, you can use slicing on a string like this:

image

With 2D arrays, you can also use a multi-dimensional slicer too:

image


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


3 thoughts on “F# equivalent of C#’s Object Initialization syntax”

  1. Sebastian Ullrich

    Collection initializer != array initializer. There’s no F# equivalent of C#’s collection initializers.

  2. Sebastian Ullrich: you’re not limited to using array initialization in F#, it works with arrays, lists and sequences (which is a F# alias for IEnumerable).

Leave a Comment

Your email address will not be published. Required fields are marked *