# Writing C# .net code in PowerShell

**URL:** <https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909>\
**Category:** PowerShell Help\
**Created:** [March 3, 2016, 2:47am UTC](https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909 "2016-03-03T02:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul-smith](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@paul-smith](https://forums.powershell.org/u/paul-smith)\
**Post date:** [March 3, 2016, 2:47am UTC](https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909/1 "2016-03-03T02:47:04Z")

</div>

Hi

I am currently working through some webservice calls wrote in C# .net and converting these to PowerShell. This has been going ok until I got to the following:

test.systemFields = new systemFieldsType[2]

```
            int j = 0;
            metadata.systemFields[j].name = "name";
            metadata.systemFields[j++].value = "test name";
            metadata.systemFields[j].name = "type";
            metadata.systemFields[j++].value = "test";

```

In PowerShell I am trying:

$test.systemFields = New-Object systemFieldsType[2]

I can see systemFieldsType as an option but it doesn’t like me adding [2].

Any ideas how I deal with this in PowerShell?

Many thanks  
Paul

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 3, 2016, 2:49am UTC](https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909/2 "2016-03-03T02:49:49Z")

</div>

You’re creating an array with two elements. That’s shorthand for calling the array constructor with an argument of two, which looks like this in PowerShell:

```
$test.systemFields = New-Object -TypeName systemFieldsType[] -ArgumentList 2

# Or as I usually write it, out of habit:

$test.systemFields = New-Object systemFieldsType[](2)
```

I know that second syntax is sort of me wishing that New-Object was just like the new statement in C#, but I can’t break the habit. That’s what New should look like. 😛

---

<div class="post-metadata">

**Author:** ![paul-smith](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@paul-smith](https://forums.powershell.org/u/paul-smith)\
**Post date:** [March 3, 2016, 7:32am UTC](https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909/3 "2016-03-03T07:32:10Z")

</div>

Hi Dave

Thanks for the reply.

When I run :  
$test.systemFields = New-Object -TypeName systemFieldsType

and then:  
$test.systemFields

I get the ‘name’ and ‘value’ headers. However, when I run:  
$test.systemFields = New-Object -TypeName systemFieldsType -ArgumentList 2

and then:  
$test.systemFields

I don’t get the headers and cannot set the following:

int j = 0;  
metadata.systemFields[j].name = “name”;  
metadata.systemFields[j++].value = “test name”;  
metadata.systemFields[j].name = “type”;  
metadata.systemFields[j++].value = “test”;

Any ideas?

Many thanks  
Paul

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/writing-c-net-code-in-powershell/5909/4 "2024-05-16T20:42:43Z")

</div>

Closed on May 16, 2024, 8:42pm UTC


