Can pass a non initialized variable to methods

This preview shows page 41 - 45 out of 45 pages.

can pass a non-initialized variable to method’s argument by using out keyword. Also, it is used to get more than one return parameter from a method. Out signifies a reference parameter , which mean out keyword passes an argument by reference. Note the value of an out variable must be initialized in method’s body. Syntax The syntax of out keyword is the same as ref keyword. We write “out” keyword before a parameter before defining a parameter in method’s signature and before passing an argument while calling a method. MethodName(out variableName); //use out keyword with paremeter when method is called. void MethodName(out v) { //TODO: } //use out keyword with parameter when method is defined
Image of page 41

Subscribe to view the full document.

Methods in C# Pass by Reference with out Keyword Example In below code, we’re initializing a parameter value in method. The same changes would affect the variable, which was passed as an argument of the method. Pass empty variable in method argument via out keyword and initialize it in method’s body class Program { static void outMethod(out int i) { i = 1; } static void Main(string[] args) { int j; outMethod(out j); Console.WriteLine(j); // j = 1 } } In main method, see that the variable “j” is not initialized and passed to outMethod. When passing it to outMethod we must write the “out” keyword. Inside the outMethod body, the value of “I” is initialized with 1. The same changes will affect the original variable in main method “j”, because the value of j is not 1.
Image of page 42
Methods in C# Use ParamsArray to Pass Unlimited Method Argument The number of arguments of a method depends upon the length of a parameter list in method’s signature. For example, in our previous example of “Sum” method, we cannot pass more than two values in its arguments. But with param array we can pass an unlimited number of arguments. Syntax The syntax of using params array is simple; just write “ params before an array type in the method’s parameter list. void myMethod ( params int[] args ) { //TODO: }
Image of page 43

Subscribe to view the full document.

Methods in C# Use ParamsArray to Pass Unlimited Method Argument Example Suppose we are required to make a method which takes unlimited arguments and returns a sum of all the arguments’ values. Add unlimited arguments and return its result int Sum(params int[] args) { int add = 0; foreach (int item in args) { add = add + item; } return add; } i n the above code snippet, see that “ params ” keyword is used before int array. The code written in method body is simple and straightforward. It is iterating over all arguments and adding each one of them with the next value. In the end it returns the sum of all arguments. Sum (1, 2, 3, 4, 5); // return 15
Image of page 44
Summary var is an implicit type; it can store data of any type at compile time. • Operators are special symbols that manipulate data to produce a required result.
Image of page 45
  • Spring '16
  • Antin
  • Control flow

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern