Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

The C# out Parameters

Читайте также:
  1. Calculation of population genetics parameters in cattle data (15 microsatellites genotyped in 12 Eurasian cattle breeds)
  2. Corresponding parameters
  3. Implicit conversions involving type parameters
  4. Investigation of Air Parameters in the Working Area #2
  5. Method parameters
  6. Type parameters and constraints

The out parameter is very similar to the ref keyword. You are allowed only to return one variable from a function. The out keyword gives the ability to return multiple values. You must use out in the same way as you would the ref keyword. Except in one case:

· An out variable has to be an unassigned a variable when passed to a function

Example:

using System;

class Program

{

static int AddOne(out int x)

{

int y = 90;

x = 19; //out variable must be initialized

x = x + 1;

return y + 1;

}

static void Main()

{

int z; //out variable must be unassigned be declared

Console.WriteLine(AddOne(out z));

Console.WriteLine(z);

Console.Read();

}

}

Output:

· Tip: If you assign an out variable before it is passed to a function that value will be ignored because it must be reassigned in the function.

C# Scope

Variable scope is an crucial subject in any programming language. One of the most difficult errors to debug are runtime bugs. Compilation errors are easy to pin point because the compiler tells you were the error is. A runtime bug is a bug that produces incorrect results. The program runs fine but it gives unexpected results or behavior. Trying to track variable values is a daunting task if you have hundreds or thousands of variables. If you keep track of your variable scopes you will have less headaches in the future.

When you declare a variable inside main() it is only visible in the main() block. For example, you cannot declare a variable x in main() and then try and access it in a function.

Example 1:

using System;class Program{ static void printX() { Console.WriteLine(x); //Error--Cannot access variable x } static void Main() { int x = 78; //Declared in main() printX(); Console.Read(); }}

Important: This program will not compile because the function printX() is trying to access a variable that is not accessible.

If you want x to accessible in the function you must pass it by value or pass by reference. Variables only have scope in the block that they are declared in.

Example 2:

using System;class Program{ static void Main() { int x = 3; if (true) //start -- if block { int y = 6; //declared in if block } //ERROR--y is only accessible in the if block Console.WriteLine(y); //OK -- x is accessible Console.WriteLine(x); Console.Read(); }}

Note: This program will not compile because y can ONLY be accessed in the if block

Example 3:

using System;class Program{ static void Main() { int x; if (true) //start -- if block { int y = 6; //declared in if block x = y; //x is still in Main() } //OK -- x is accessible Console.WriteLine(x); Console.Read(); }}

Output:

6

x is still accessible because it is still in the Main block. y can only be accessed only in that particular if block.

 


Дата добавления: 2015-11-14; просмотров: 60 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
C# Function Basics| C# Global variables

mybiblioteka.su - 2015-2024 год. (0.006 сек.)