
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Passing arrays to methods in C#?
To pass arrays to methods, you need to pass an array as a method argument.
int displaySum(int[] arr, int size) { }
Now call it −
sum = d.displaySum(myArr, 5 ) ;
Example
using System; namespace Test { class Demo { int displaySum(int[] arr, int size) { int i; int sum = 0; for (i = 0; i < size; ++i) { sum += arr[i]; } return sum; } static void Main(string[] args) { Demo d = new Demo(); int [] myArr = new int[] {59, 34, 76, 65, 12, 99}; int sum; sum = d.displaySum(myArr, 5 ) ; /* output the returned value */ Console.WriteLine( "Sum: {0} ", sum ); Console.ReadKey(); } } }
Output
Sum: 246
- Related Articles
- Passing Arrays to Function in C++
- What are different methods of passing parameters in C#?
- Passing static methods as arguments in PHP
- How to pass Arrays to Methods in Java?
- Passing by pointer Vs Passing by Reference in C++
- Passing a vector to constructor in C++
- Parameter Passing Techniques in C/C++
- What are new methods have added to the Arrays class in Java 9?
- Passing the Assignment in C++
- Arrays in C/C++?
- How to define methods in C#?
- Count passing car pairs in C++
- Passing an array to a C++ function
- Passing Lua script from C++ to Lua
- Are there benefits of passing by pointer over passing by reference in C++?

Advertisements