In C#, square bracket syntax in a function's parameter represents an optional parameter.
An optional parameter allows a function to be called with or without providing a value for that parameter. If a value is not provided, the parameter will be set to its default value.
Here's an example of a function with an optional parameter:
public void MyFunction(string requiredParam, int optionalParam = 42) { Console.WriteLine("Required parameter: " + requiredParam); Console.WriteLine("Optional parameter: " + optionalParam); } In this example, the MyFunction method has a required parameter called requiredParam, which must be provided when calling the method. It also has an optional parameter called optionalParam, which is assigned a default value of 42.
The optional parameter is defined using the square bracket syntax, with the default value specified after the equals sign. This means that if the caller does not provide a value for optionalParam, it will be set to the default value of 42.
Here's an example of calling the MyFunction method with both parameters provided:
MyFunction("Hello", 123); Output:
Required parameter: Hello Optional parameter: 123
And here's an example of calling the MyFunction method with only the required parameter provided, allowing the optional parameter to take its default value:
MyFunction("Hello"); Output:
Required parameter: Hello Optional parameter: 42
"C# square bracket syntax in function parameters example"
public void ProcessData(int[] data) { // Function logic to process an integer array } "C# function parameter square brackets for generic types"
public void ProcessList<T>(List<T> dataList) { // Function logic to process a generic list } "C# square bracket syntax for params keyword in functions"
params keyword in C# function parameters.public void ProcessParams(params string[] values) { // Function logic to process variable number of string parameters } "C# square bracket syntax in function parameters for indexers"
public int this[int index] { get { /* Getter logic */ } set { /* Setter logic */ } } "C# square brackets in function parameters for multidimensional arrays"
public void ProcessMatrix(int[,] matrix) { // Function logic to process a 2D array } "C# function parameters with square brackets for nullable types"
public void ProcessNullable(int? value) { // Function logic to handle nullable integer } "C# function parameters square bracket syntax for named arguments"
public void DisplayPersonDetails(string name, int age = 25) { // Function logic to display person details } "C# square bracket syntax for array parameters with ref keyword"
ref keyword in C# function parameters for modifying array elements.public void ModifyArrayElements(ref int[] values) { // Function logic to modify array elements } "C# function parameters square brackets for optional parameters"
public void DisplayMessage(string text, bool isUppercase = false) { // Function logic to display a message with optional formatting } "C# square bracket syntax in function parameters for events"
public event EventHandler<EventArgs> MyEvent;
chrome-remote-debugging linear-equation nco video-player azure-eventgrid tableau-api nimbus maven webbrowser-control android-query