Repository: alloy-framework/alloy
Pull request #213 (2025-06-23 19:30:40)

Add support for optional params

pkg-pr-new[bot]: 
[Open in StackBlitz](https://pkg.pr.new/template/4a68c892-2a42-451f-9e29-7557f0ba0bda)


<details><summary><b>@alloy-js/babel-plugin</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-plugin-jsx-dom-expressions</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-jsx-dom-expressions@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-preset</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-preset@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/cli</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/cli@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/core</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/core@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/create</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/create@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/csharp</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/csharp@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/java</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/java@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/json</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/json@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/markdown</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/markdown@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/prettier-plugin-alloy</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/prettier-plugin-alloy@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/rollup-plugin</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/rollup-plugin@213
```

</p></details>
      
    

<details><summary><b>@alloy-js/typescript</b></summary><p>

```
npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/typescript@213
```

</p></details>
      
    

_commit: <a href="https://github.com/alloy-framework/alloy/runs/44631191278"><code>ee31751</code></a>_


Patch:
diff --git a/.chronus/changes/feature-optional-params-2025-5-23-19-31-1.md b/.chronus/changes/feature-optional-params-2025-5-23-19-31-1.md
new file mode 100644
index 0000000..6de4bd6
--- /dev/null
+++ b/.chronus/changes/feature-optional-params-2025-5-23-19-31-1.md
@@ -0,0 +1,8 @@
+---
+# Change versionKind to one of: breaking, feature, fix, internal
+changeKind: feature
+packages:
+  - "@alloy-js/csharp"
+---
+
+Add support for optional parameters
diff --git a/packages/csharp/src/components/Parameters.tsx b/packages/csharp/src/components/Parameters.tsx
index d83b46f..4a903a9 100644
--- a/packages/csharp/src/components/Parameters.tsx
+++ b/packages/csharp/src/components/Parameters.tsx
@@ -1,4 +1,5 @@
 import * as core from "@alloy-js/core";
+import { code } from "@alloy-js/core";
 import { useCSharpNamePolicy } from "../name-policy.js";
 import { CSharpOutputSymbol } from "../symbols/csharp-output-symbol.js";
 import { useCSharpScope } from "../symbols/scopes.js";
@@ -7,11 +8,15 @@ import { Name } from "./Name.js";
 export interface ParameterProps {
   name: string;
   type: core.Children;
+  /** If the parmaeter is optional(without default value) */
+  optional?: boolean;
+  /** Default value for the parameter */
+  default?: core.Children;
   refkey?: core.Refkey;
   symbol?: core.OutputSymbol;
 }
 
-// a constructor/method parameter
+/** Define a parameter to be used in class or interface method. */
 export function Parameter(props: ParameterProps) {
   const name = useCSharpNamePolicy().getName(props.name, "parameter");
   const scope = useCSharpScope();
@@ -31,14 +36,16 @@ export function Parameter(props: ParameterProps) {
 
   return (
     <core.Declaration symbol={memberSymbol}>
-      {props.type} <Name />
+      {props.type}
+      {props.optional ? "?" : ""} <Name />
+      {props.default ? code` = ${props.default}` : ""}
     </core.Declaration>
   );
 }
 
 export interface ParametersProps {
   // param name and type
-  parameters: Array<ParameterProps>;
+  parameters: ParameterProps[];
 }
 
 // a collection of parameters


Test patch:
diff --git a/packages/csharp/src/components/interface/method.test.tsx b/packages/csharp/src/components/interface/method.test.tsx
index c5ec44d..e09262c 100644
--- a/packages/csharp/src/components/interface/method.test.tsx
+++ b/packages/csharp/src/components/interface/method.test.tsx
@@ -103,6 +103,58 @@ it("defines params and return type", () => {
   `);
 });
 
+it("defines optional param", () => {
+  const res = (
+    <Wrapper>
+      <InterfaceMethod
+        public
+        name="MethodOne"
+        parameters={[
+          {
+            name: "intParam",
+            type: "int",
+            optional: true,
+          },
+        ]}
+        returns="string"
+      />
+    </Wrapper>
+  );
+
+  expect(res).toRenderTo(`
+    public interface TestInterface
+    {
+      public string MethodOne(int? intParam);
+    }
+  `);
+});
+
+it("defines optional param with default", () => {
+  const res = (
+    <Wrapper>
+      <InterfaceMethod
+        public
+        name="MethodOne"
+        parameters={[
+          {
+            name: "intParam",
+            type: "int",
+            default: 12,
+          },
+        ]}
+        returns="string"
+      />
+    </Wrapper>
+  );
+
+  expect(res).toRenderTo(`
+    public interface TestInterface
+    {
+      public string MethodOne(int intParam = 12);
+    }
+  `);
+});
+
 it("specify doc comment", () => {
   expect(
     <TestNamespace>