C# Function Or Method with Examples
In C#, a function is a block of code that performs a specific task and can be called from other parts of your program. Functions are a fundamental concept in C# and are often referred to as methods.
Example
returnType methodName(parameters)
{
// Method body (code)
// ...
return returnValue; // Optional return statement
}
Basic Structure Of C# Function
Return Type (returnType):
It is the return value that will be passed after completing the execution of function.It can be any valid C# data type, including primitive types (e.g., int, double, string), custom types (e.g., classes, structs), or void if the function doesn’t return a value.
Method Name (methodName):
It is the name of function that should be meaningful and It follows C# naming conventions (usually in PascalCase) and is used to call the function elsewhere in your code.
Parameters (parameters):
It is an option variable that you passed in function between parentheses () and and may include one or more parameter declarations, separated by commas.Each parameter consists of a data type and a parameter name.