Advertisement
C_Volume2 Data Structures #84762

Your First C# Programs

This tutorial will get you started with C# by introducing a few very simple programs. Please vote if it is useful You will Understand the basic structure of a C# program.Obtain a basic familiarization of what a "Namespace" is.Obtain a basic understanding of what a "Class" is.Learn what a "Main" method does. Learn how to obtain command-line input. Learn about console input/output (I/O).

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<p><b><font size="4" color="#FF00FF">Your first C# Programs</font><font size="4" color="#00FF00"><br>
<br>
</font></b>This tutorial will get you started with C# by introducing a few very 
simple programs. You will<br>
<br>
Understand the basic structure of a C# program.<br>
Obtain a basic familiarization of what a &quot;Namespace&quot; is.<br>
Obtain a basic understanding of what a &quot;Class&quot; is.<br>
Learn what a &quot;Main&quot; method does.<br>
Learn how to obtain command-line input.<br>
Learn about console input/output (I/O).<br>
<br>
<b><font color="#FF0000">Listing 1. A Simple Welcome Program: Welcome.cs</font></b><br>
<br>
// Namespace Declaration<br>
using System;<br>
// Program start class<br>
class Welcome {<br>
// Main begins program execution.<br>
public static void Main() {<br>
// Write to console<br>
Console.WriteLine(&quot;Welcome to the PSC!&quot;); <br>
}<br>
} <br>
<br>
The program in Listing 1 has 4 primary elements, a namespace declaration, a 
class, a &quot;Main&quot; method, and a program statement.<br>
The namespace declaration indicates that you are referencing the &quot;System&quot; 
namespace. Namespaces contain groups of code that can be called upon by C# 
programs. With the &quot;using System;&quot; declaration, you are telling your program 
that it can reference the code in the &quot;System&quot; namespace without pre-pending the 
word &quot;System&quot; to every reference. <br>
The class declaration, &quot;class Welcome&quot;, contains the data and method definitions 
that your program uses to execute. It is one of a few different types of 
elements your program can use to describe objects, such as interfaces and 
structures. This particular class has no data, but it does have one method. This 
method defines the behavior of this class (or what it is capable of doing).<br>
The one method within the Welcome class tells what this class will do when 
executed. The method name, &quot;Main&quot;, is reserved for the starting point of a 
program. Before the word &quot;Main&quot; is a &quot;static&quot; modifier. The &quot;static&quot; modifier 
explains that this method works in this specific class only, rather than an 
instance of the class. This is necessary, because when a program begins, no 
object instances exist. Every method must have a return type. In this case it is 
&quot;void&quot;, which means that &quot;Main&quot; does not return a value. Every method also has a 
parameter list following it's name with zero or more parameters between 
parenthesis. <br>
The &quot;Main&quot; method specifies it's behavior with the &quot;Console.WriteLine(...)&quot; 
statement. &quot;Console&quot; is a class in the &quot;System&quot; namespace. &quot;WriteLine(...)&quot; is a 
method in the &quot;Console&quot; class. We use the &quot;.&quot;, dot, operator to separate 
subordinate program elements. It should be interesting to note that we could 
also write this statement as &quot;System.Console.WriteLine(...)&quot;. This follows the 
pattern &quot;namespace.class.method&quot; as a fully qualified statement. Had we left out 
the &quot;using System&quot; declaration at the top of the program, it would have been 
mandatory for us to use the fully qualified form &quot;System.Console.WriteLine(...)&quot;. 
This statement is what causes the string, &quot;Welcome to the PSC!&quot; to print on the 
console screen.<br>
Observe that comments are marked with &quot;//&quot;. These are single line comments, 
meaning that they are valid until the end-of-line. If you wish to span multiple 
lines with a comment, begin with &quot;/*&quot; and end with &quot;*/&quot;. Everything in between 
is part of the comment. You may place a single line comment within a multi-line 
comment. However, you can't put multi-line comments within a multi-line comment. 
Comments are not considered when your program is compiled. <br>
All statements end with a &quot;;&quot;, semi-colon. Classes and methods begin with &quot;{&quot;, 
left curly brace, and end with a &quot;}&quot;, right curly brace. Any statements within 
and including &quot;{&quot; and &quot;}&quot; define a block. Blocks define scope (or lifetime and 
visibility) of program elements.<br>
Many programs are written to accept command-line input. Collection of 
command-line input occurs in the &quot;Main&quot; method. Listing 2 shows a program which 
accepts a name from the command line and writes it to the console.<br>
<br>
<b><font color="#FF0000">Listing 2. Getting Command-Line Input: Greet.cs</font></b><br>
<br>
// Namespace Declaration<br>
using System;<br>
// Program start class<br>
class Greet {<br>
// Main begins program execution.<br>
public static void Main(string[] args) {<br>
// Write to console<br>
Console.WriteLine(&quot;Hello, {0}!&quot;, args[0]);<br>
Console.WriteLine(&quot;Welcome to the PSC!&quot;); <br>
}<br>
}<br>
<br>
Remember to add your name to the command-line, i.e. &quot;Greet Pankaj&quot;. If you 
don't, your program will crash unless you detect and avoid such error 
conditions.<br>
In Listing 2, you'll notice an entry in the &quot;Main&quot; method's parameter list. The 
parameter name is &quot;args&quot;. It's what you use to refer to the parameter later in 
your program. The &quot;string[]&quot; expression defines the Type of parameter that &quot;args&quot; 
is. The &quot;string&quot; Type holds characters. These characters could form a single 
word, or multiple words. The &quot;[]&quot;, square brackets denote an Array, which is 
like a list. Therefore, the Type of the &quot;args&quot; parameter, is a list of words 
from the command-line.<br>
You'll also notice an additional &quot;Console.WriteLine(...)&quot; statement within the 
&quot;Main&quot; method. The argument list within this statement is different than before. 
It has a formatted string with a &quot;{0}&quot; parameter embedded in it. The first 
parameter in a formatted string begins at number 0, the second is 1, and so on. 
The &quot;{0}&quot; parameter means that the next argument following the end quote will 
determine what goes in that position. <br>
This is the &quot;args[0]&quot; argument, which refers to the first string in the &quot;args&quot; 
array. The first element of an Array is number 0, the second is number 1, and so 
on. For example, if I wrote &quot;Greet Pankaj&quot; on the command-line, the value of 
&quot;args[0]&quot; would be &quot;Pankaj&quot;.<br>
Now about the embedded &quot;{0}&quot; parameter in the formatted string. Since &quot;args[0]&quot; 
is the first argument, after the formatted string, of the &quot;Console.WriteLine()&quot; 
statement, it's value will be placed into the first embedded parameter of the 
formatted string. When this command is executed, the value of &quot;args[0]&quot;, which 
is &quot;Pankaj&quot; will replace &quot;{0}&quot; in the formatted string. Upon execution of the 
command-line with &quot;Greet Pankaj&quot;, the output will be as follows:<br>
<br>
&gt;Hello, Pankaj! <br>
&gt;Welcome to the PSC! <br>
<br>
Another way to provide input to a program is via the console. Listing 3 shows 
how to obtain interactive input from the user.<br>
<br>
<font color="#FF0000"><b>Listing 3. Getting Interactive Input: GreetMe.cs</b></font><br>
<br>
// Namespace Declaration<br>
using System;<br>
// Program start class<br>
class GreetMe {<br>
// Main begins program execution.<br>
public static void Main() {<br>
// Write to console/get input<br>
Console.Write(&quot;What is your name?: &quot;);<br>
Console.Write(&quot;Hello, {0}! &quot;, Console.ReadLine());<br>
Console.WriteLine(&quot;Welcome to the PSC!&quot;); <br>
}<br>
}<br>
<br>
This time, the &quot;Main&quot; method doesn't have any parameters. However, there are now 
three statements and the first two are different from the third. They are &quot;Console.Write(...)&quot; 
instead of &quot;Console.WriteLine(...)&quot;. The difference is that the &quot;Console.Write(...)&quot; 
statement writes to the console and stops on the same line, but the &quot;Console.WriteLine(...)&quot; 
goes to the next line after writing to the console. <br>
The first statement simply writes &quot;What is your name?: &quot; to the console. <br>
The second statement doesn't write anything until it's arguments are properly 
evaluated. The first argument after the formatted string is &quot;Console.ReadLine()&quot;. 
This causes the program to wait for user input at the console, followed by a 
Return or Enter. The return value from this method replaces the &quot;{0}&quot; parameter 
of the formatted string and is written to the console. <br>
The last statement writes to the console as described earlier. Upon execution of 
the command-line with &quot;GreetMe&quot;, the output will be as follows:<br>
<br>
&gt;What is your Name? &lt;type your name here&gt;<br>
&gt;Hello, &lt;your name here&gt;! Welcome to the PSC! <br>
<br>
So now you know the basic structure of a C# program. You have become familiar 
with namespaces and classes. You know the &quot;Main&quot; method is your entry point to 
start a C# program and how to capture command-line input and perform interactive 
I/O.<br>
&nbsp;</p>
</body>
</html>
Původní komentáře (3)
Obnoveno z Wayback Machine