A Closer Look C# Help

First, a few general comments about C# syntax. In C#, as in other C-style languages, most statements end in a semicolon (; ) and can continue over multiple lines without needing a continuation character (such as the underscore in Visual Basic). Statements can be joined into blocks using curly braces ({)). Singleline comments begin with two forward slash characters (II), and multiline comments begin with a slash and an asterisk (f·) and end with the same combination reversed (. f). In these aspects, C# is identical to C++ and Java but different from Visual Basic. It is the semicolons and curly braces that give C# code such a different visual appearance from Visual Basic code. If your background is predominantly Visual Basic, take extra care to remember the semicolon at the end of every statement. Omitting this is usually the biggest single cause of compilation errors among developers new to C-style languages.  Another thing to remember is that C# is case sensitive. That means that variables named myVar and MyVarare two different variables.

The first few lines in the previous code example have to do with namespaces (mentioned later in this chapter), which are away to group together associated classes. This concept will be familiar to Java and C++ developers but may be new to Visua! Basic6 developers. C# namespaces are basically the same as C++ namespaces or, equivalently, Java packages, but there is no comparable concept in VisualBasic 6. TIle namespace keyword.•declares the namespace your class should be associated with. All code within braces that follow it is regarded as being within that namespace. The using statement Specifies a espace that the compiler should look at to find any classes that are referenced in your code but that aren’t defined in’the current namespace. This serves the same purpose as the import statement in Java the using namespace statement in C++.

using System;
namespace Wrox.ProCSharp.Basics

The reason for the presence of the using statement in the First. cs me is that you are going to use library class, System. Console. The using System statement allows you to refer to this class (and similarly for any other classes in the System namespace). The.standard System namespace where the most commonly used .NET types reside. it.is important to realize that everything you do in C# depends on the .NET base classes. In this case, you are using the Console class within the System namespace in order to write to the console window. C# has no built-in keywords of its own for input or output; it is completely reliant on the .NET classes

Because almost every ell program uses classes in the System namespece, we will assume tlult a using System; statement is present in the file for all code snippets in this chapter.

Next, you declare a class called MyFirstClass. However, because it has been placed in a namespace called Wrox. ProCSharp. Basics, the fully qualified name of this class is Wrox. ProCSharp. Basics

class MyFirstCSharpClass

As in Java, all C# code must be contained within a class. Classes in C# are similar to classes in Java and C++, and very roughly comparable to class modules in VisualBasic 6. The class declaration consists of the class keyword, followed by the class name and a pair of curly braces. All code associated with the class should be placed between these braces.

Next, you declare a method called Main ( ) . Every C# executable (such as console applications, Windows applications, and Windows services) must have an entry point – the Main () method (note the capital M):

static void Main()

The method is called when the program is started, like the main () function in <;:++or Java, or Sub Main () in a Visual Basic6 module. This method must return either nothing (void) or an integer (int) .. A C# method corresponds to a method in C++ and Java (sometimes referred to in C++ as a member function). It also corresponds to either a Visual Basic Function or a VISualBasic Sub, depending on whether the method returns anything (unlike VISUalBasic,CII makes no conceptual distinction between .tunctions and subroutines).

Note the format of method definitions in C#:

[modifiers) return_type ~thodNarne([pararneters))
II Method bo?y. NB. This code block is pseudo-code,


Here, the first square brackets represent certain optional keywords. Modifiers are used to specify certain features of the method you are defining, such as where the method can be called from. In this case, you have two modifiers: public and static. The public modifier means that the method can be accessed from anywhere, so it can be called from outside your class. This is the same meaning as public in C++ and Java, and Publ ic in Visual Basic. The s ta tic modifier indicates that the method does not operate on a .specificinstance of your class and therefore is called without first instantiating the class. This is, important because you are creating an executable rather than a class library. Once again, this has the same meaning as the static keyword in C++ and Java, though in this case there is no VisualBasic equivalent (the Static keyword in Visual Basic has a different meaning). You set the return type to void, and in the example, you don’t include any parameters.

 Finally, we come to the code statements themselves

Console.WriteLine(OThis isn’t at all like Javal·);

In this case, you simply call the WriteLine () method of the System. Console class to write a line of , text to the console window. Wri teLine () is a static method, so you don’t need to instantiate a Console object before calling it.

Console. ReadLine () reads user input. Adding this line forces the application to wait for the carriage return key to be pressed before the application exits, and, in the case of VIsual Studio 2008, the console window disappears.

You then call return to exit from the method (also, because this is the Main () method, you exit the program as well.). You specified void in your method header, so you don’t return any values. The return statement is equivalent to return in C++ and java, and Exit Sub or Exit Function in VIsual Basic.

Now that you have had a taste of basic C# syntax, you are ready for more detail. Because it is virtually impossible to write any nontrivial program without variables, we will start by looking at variables in C#.

Posted on October 27, 2015 in C# Basics

Share the Story

Back to Top
Share This