Читайте также:
|
|
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:
2Line 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 |