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

C# Global variables

Читайте также:
  1. Academia in the process of globalization. Its intercultural nature
  2. And that’s how we imagine the world leaders's debates at the global election of the president of the Earth.
  3. Characteristics of globalization
  4. Data in a program (variables and literals)
  5. Definition, waves and history of globalization process
  6. ENABLING U.S. GLOBAL INFORMATION SUPERIORITY, DURING PEACE AND THROUGH WAR. 1 страница

Global variables are accessible through out the whole program. To declare a global variable you must use the static or const keyword. The const keyword prevents the variable from being modified.

Example:

1. using System;2. class Program3. {4. static int a = 2; //Accessible in the whole program 5. static void test()6. {7. Console.WriteLine(a);8. }9. static void Main()10. {11. test();12. Console.Read();13. }14. }

Output:

2

Line 4: Do you see the static keyword. This makes the variable accessible in the whole program.

C# Main() Function

Functions are able to accept and return parameters. Main() is no different than any other function. The C# Main function is slightly different in that it is able to accept and return values to external applications. Such as MS word or any other external application. The are four versions of the Main() function:

' C# Main() Versions:

Lets accept parameters from an external program like DOS. Here are the instructions to follow:

using System;class Program{ static void Main(string[] args) { foreach(string x in args) Console.WriteLine(x); Console.Read(); }}

Compile and make an executable from the code above. When you finish compiling the code move the executable file to the C:\ directory and rename the file to test.exe. Then run run a DOS window and move to the c:\ directory.

C:\>test.exe it's working 1 2 3 123

Each space separates each parameter. The output should look like this:

The C# program accepted the arguments and simply displayed the arguments located in an array.


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


<== предыдущая страница | следующая страница ==>
The C# out Parameters| C# Recursion Tips

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