C# Programs on Functions

C# Programming Examples - Functions

In C#, a function is a technique of wrapping code to perform a certain task and return a value. Functions allow programmers to insert code and call it from another part of the program. This is useful when we need to run the same piece of code from different places. Functions are members of the class and are very similar to a method, and sometimes class and method are used interchangeably. A function consists of components such as function name, return type, body, access specifier, and parameters.

C# Function Syntax:

Functions in C# have the following syntax:

<Access Specifiers> <return type> <function name>(<function parameters>)
{
<function body>
return;
}

Access specifiers, return types and parameters are optional.

advertisement
advertisement

Local Function in C#:

A local function is a private function whose scope is limited to the function from which it is created. Local functions can only be called from container members. Local functions can be declared in and called from methods, constructors, event accessors, property accessors, finalizers, anonymous methods, lambda expressions, and other local functions.

C# Local Function Syntax:

<modifiers> <return-type> <method-name> <parameter-list>

With a local function, we can use the following modifiers:

  • async
  • unsafe
  • static
  • extern

The following section contains C# programs on functions, predefined functions, classes, abstract properties, method types, method hiding, namespaces, and preprocessor attributes. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.

Here is the listing of C# programming examples on Functions:

advertisement
  1. C# Programs on Predefined Functions
  2. C# Programs on Classes and Abstract Properties
  3. C# Programs on Method Types and Method Hiding
  4. C# Programs on Namespaces and Preprocessor Attributes

1. C# Programs on Predefined Functions

Program Description
Reverse a String with Predefined Function in C# C# Program to Reverse a String with Predefined Function
Sort String using Predefined Function in C# C# Program to Sort a String using Predefined Function
Searching using Predefined Functions in C# C# Program to Perform Searching using Predefined Functions
Integer Binary Equivalent using Recursion in C# C# Program to Print Binary Equivalent of an Integer using Recursion
Boxing Operation in C# C# Program to Demonstrate Boxing Operations
Unboxing Operation in C# C# Program to Perform Unboxing Operation

2. C# Programs on Classes and Abstract Properties

Program Description
Class Properties in C# C# Program to Demonstrate Properties of the Class
Abstract Properties in C# C# Program to Demonstrate Abstract Properties
Sealed Class in C# C# Program to Create a Sealed Class
Environment Type in C# C# Program to Demonstrate Environment Type
icollection Count in C# C# Program to Demonstrate icollection Count
iscollection.synchronised in C# C# Program to Demonstrate iscollection.synchronised
C# Path Class Operations C# Program to Demonstrate the Operations of C# Path Class
Create Filtered Sequence using Select Clause in C# C# Program to Create Filtered Sequence using Select Clause

3. C# Programs on Method Types and Method Hiding

Program Description
Method Hiding in C# C# Program to Demonstrate Method Hiding
Static Method in C# C# Program to Implement Static Method
Anonymous Method in C# C# Program to Create Anonymous Method
Culture Names in C# C# Program to Demonstrate Culture Names
Obsolete Class in C# C# Program to Create Obsolete Class
Goto Statement in C# C# Program to Demonstrate the Goto Statement

advertisement

4. C# Programs on Namespaces and Preprocessor Attributes

Program Description
Nullable Data Type in C# C# Program to Ilustrate Nullable Data Types
New modifier in C# C# Program to Demonstrate New Modifier
Print Hello World without using WriteLine in C# C# Program to Print Hello World Without using WriteLine
#define Preprocessor in C# C# Program to Demonstrate the use of #define Preprocessor
Namespaces in C# C# Program to Implement Namespaces
Pass by Value Program in C# C# Program to Demonstrate Pass by Value
Pass by Reference in C# C# Program to Demonstrate Pass by Reference
Value Type Vs Reference Type in C# C# Program to Demonstrate the Difference between Value Type Parameter and Reference Type Parameter

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.