Home

C# and Code Organization: C# Language Accessories

 

Command Line Options

In Lesson 1, we saw that, to see the result of an application, you must execute it. To make this possible,, Microsoft Visual C# ships with an internal program called a compiler. A compiler is a computer program made of internal other sub-programs. One of the sub-programs, in fact probably the first, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks for syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the programs or troubleshoot these problems. In our lessons, we will address as many issues as possible.

If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its result(s) to the compiler. The compiler calls another program called a linker. If the program contains just one file, the linker considers it. If the program contains more than one file, the linker considers them. The linker gathers some of the files that the C# compiler shipped with (those files that your program needs in order to work, since your program doesn't need all possible files that C# ships with), puts them together ("links" them) with your file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant problem, the compiler creates the program. This doesn't mean that everything is alright, it only means that the compiler thinks that everything is alright: it is still possible that the result may not be what you would expect. To make your life easier, all of the sub-programs (parser, linker, debugger, etc) that ship with C# are grouped in one large program: the compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to "translate" your English-based instructions into a computer-based language.

The compiler that ships with the C# version we will use, that is, the compiler of the Microsoft .NET Framework is a program called csc. Like most other programs, it has the extension .exe. This csc name is not standard. This means that another C# compiler may have another name; csc.exe is just the name of the compiler we will use.

The csc compiler is freely available if you download the .NET Framework from the Microsoft web site.

In this book, we will create our program using Microsoft Visual C# 2005 Express Edition or Professional but if you didn't have it, you would still be able to create and execute applications. To do this, at the Command Prompt, you would type csc, followed by the name of the file that contains the code with its extension. An example would be:

csc Filename.cs

When you do this, an executable with the same name as the file is created. If you want, you can ask the compiler to produce an executable using the name of your choice. To do this, you would compile the project using the following formula:

csc /out:NameOfExecutate.exe Filename.cs

The NameOfExecutate factor in our formula represents the name you want the executable to have. If the name you want is in one word, you can just type it. If you want a name made of various words, you can include those words in double-quotes

The FileName factor is one we are familiar with.

Unsafe Code

When C# was invented, one of its biggest goals was to avoid some of the difficulties of C/C++. Among them was the use of pointers. C/C++ uses pointers to refer to the area in memory where a value is located. C# highly avoids pointers and takes over memory management as opposed to letting the programmer take care of that aspect of an application. You can still use pointers in C# in extreme cases when you judge them necessary.

Because the C# compiler is in charge of managing the memory used by the values of an application, pointers are said to be unsafe. If you want to use a pointer in your application, you must precede the name of every method that uses unsafe code with the unsafe keyword. Here is an example:

using System;

class Exercise
{
	unsafe static void Main()
	{
		int Length = 224;
		int *Len = &Length;
		
		Console.Write("Length ");
		Console.WriteLine(Length);
		Console.Write("Length ");
		Console.WriteLine(*Len);
		Console.WriteLine();

		Length = 804;
		Console.Write("Length ");
		Console.WriteLine(Length);
		Console.Write("Length ");
		Console.WriteLine(*Len);
	}
}

To compile the application, you must indicate that you are using unsafe code. To do that, use the /unsafe modifier. Here is an example:

csc /unsafe Exercise.cs

To apply this option in Microsoft Visual C# 2005 Express Edition or Professional, on the main menu, you can click Project -> Project Properties... In the Build section, click the Allow Unsafe Code check box:

Properties

 

Code Editor Region Delimiter

Microsoft Visual C# provides various to assist you with code writing and management. The characteristics include color-coded words, intuitive indentation, delimitation of sections of code, etc. Consider the following contents of the Code Editor based on what we have reviewed so far:

Notice that there are - buttons on the left side of some lines of code. These allow you to collapse a section of code if you think you don't need to see it. To do this, you can click the - button. If you click that - button, it changes into a + button. Here is an example:

The + button allows you to expand a hidden code section. This behavior of creating + and - buttons is part of the Code Editor of Microsoft Visual Studio (yes, many other programming environments use that behavior also). To create these sections, the Code Editor follows some rules. For example, it looks for the start and end of such items as directives, namespaces, classes, methods, etc.

Besides, or instead of, the sections of code created by the Code Editor, if you want, you can create your own sections. To do this, start the section with

#region Whatever

and end it with

#endregion Whatever

When and where you start, the #region expression is required. On the right side of this expression, you can type anything you want on the line. To end the section, type #endregion, followed by anything you want. Consider the following example:

using System;

class House
{
    void Create()
    {
    }
}

#region These are classes used for Student Registration
class Car
{
    void Build()
    {
    }
}

class Student
{
    void Register()
    {
    }
}
#endregion We can just stop it here

class Program
{
    static void Main()
    {
    }
}

You don't have to type anything on the right side of #endregion. After creating the region, the Code Editor would display a - button to the left side of #region with a line from there to the left of #endregion:

Regions

This then allows you to expand and collapse that section at will:

We mentioned that you didn't have to type anything on the right side of #endregion and you could leave it empty. In our example, notice that there is a rectangle with gray lines around the string that follows #region. This rectangle doesn't cover the string that follows #endregion. This means that if you don't type anything on the right side of #endregion, that section on the right side the #region line would not show.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next