tfrascaroli
tfrascaroli

Reputation: 1219

C# to VB.net code conversion. Issue with anonymous New()

I'm currently translating the class that comes with Bundler, part of the framework ServiceStack. The bit I'm currently stuck with is the following:

new { media }

or

new[] { typeof(object) }

I'm a VB.net programmer, and I don't really get what is being done here. Creating some sort of a anonymous parameter?

PS: It might be worth noting the context in which this is happening: Both of those constructions are being passed as parameters to functions.

Thank you for your time.

EDIT:

Ok, now (I think) I understand what the code does, but I'm still helpless regarding the vb.net equivalent code for those snippets, could you lend me a hand?

Upvotes: 2

Views: 105

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Two code snippets you posted show separate language features:

new { media }

This one shows how you instantiate anonymous type instance. You can read more about anonymous types here: Anonymous Types (C# Programming Guide). You can read about anonymous types in VB.NET here: Anonymous Types (Visual Basic).

For your type equivalent VB.NET code would be:

New With { Key .media = media }

Key makes property in anonymous type set-only and makes equality comparison on that type check that property to determine if entire object is equal. In C# all anonymous type property are key properties by default, and you can't make them mutable.

Second one:

new [] { typeof(object) }

This one creates implicitly typed Type[] array.

You can create similar array in VB.NET with following syntax:

{ GetType(object) }

Read How to: Initialize an Array Variable in Visual Basic for more details.

Upvotes: 3

Dai
Dai

Reputation: 155035

The new { foo = bar } syntax in C# instantiates an anonymous type.

new[] creates an implicitly-typed array, unlike anonymous types it is a language-shorthand and has no semantic implications (unless it's an implicitly-typed array of anonymous types).

The VB equivalent is described here: http://msdn.microsoft.com/en-us/library/bb384767.aspx

New With { Key media }

VB.NET has the concept of "Key properties" - which are readonly and participate in equality comparisons. in C# all properties of anonymous types are considered key properties.

Upvotes: 1

Related Questions