|
Beginning Visual C# 2005 Authors: Watson K., Nagel C., Pedersen J. Published year: 2005 Pages: 49-51/278 |
In this chapter, you've seen a fairly complete overview of the use of functions in C# code. Much of the additional features that functions offer (delegates in particular) are more abstract, and you need to understand them only in the light of object-oriented programming, which is a subject that you will encounter in Chapter 8.
This chapter covered:
Defining and using functions in console applications
Exchanging data with functions via return values and parameters
Passing parameter arrays to functions
Passing values by reference or by value
Specifying parameters for additional return values
The concept of variable scope, where variables can be hidden from sections of code where they aren't required
Details of the Main() function, including command-line parameter usage
Using functions in struct types
Function overloading, where you can supply different parameters to the same function to get additional functionality
Delegates and how to dynamically select functions for execution at runtime
A knowledge of how to use functions is central to all of the programming you are likely to be doing in the future. In later chapters, particularly when you learn about OOP (from Chapter 8 onwards), you will learn a more formal structure for functions and how they apply to classes. From then on, you will find that the ability to abstract code into reusable blocks is possibly the most useful aspect of C# programming.
The following two functions have errors. What are they?
static bool Write() { Console.WriteLine("Text output from function."); } static void myFunction(string label, params int[] args, bool showLabel) { if (showLabel) Console.WriteLine(label); foreach (int i in args) Console.WriteLine("", i); }
Write an application that uses two command-line arguments to place values into a string and an integer variable, respectively. Then display these values.
Create a delegate and use it to impersonate the Console.ReadLine() function when asking for user input.
Modify the following struct to include a function that returns the total price of an order:
struct order { public string itemName; public int unitCount; public double unitCost; }
Add another function to the order struct that returns a formatted string as follows , where italic entries enclosed in angle brackets are replaced by appropriate values:
Order Information: < unit count > < item name > items at $< unit cost > each, total cost $< total cost >
So far in this book, you have covered all the basics of simple programming in C#. Before you move on to look at object-oriented programming in the next section of the book, it's time to look at debugging and error handling in C# code.
Errors in code are something that will always be with you. No matter how good a programmer is, there will always be problems that slip through, and part of being a good programmer is realizing that this is the case and being prepared to deal with it. Of course, these may be minor problems that don't affect the execution of an application, perhaps a spelling mistake on a button or the like. They may also be glaring errors that cause applications to fail completely (usually known as fatal errors). Fatal errors include simple errors in code that will prevent compilation ( syntax errors), but may be more involved and only occur at runtime. Alternatively, errors may be subtler. Perhaps your application will fail to add a record to a database if a requested field is missing or adds a record with the wrong data in other restricted circumstances. Errors such as these, where application logic is in some way flawed, are known as semantic errors (also known as logic errors).
Often, the first that you might hear about the more subtle errors will be when a user of your application complains that something isn't working properly. This leaves you with the task of tracing through your code to try to find out what is happening and how you can change your code so that it does what it was intended to do.
In situations like this, you will find that the debugging capabilities of VS are a fantastic help. In the first part of this chapter, you look at some of the techniques available and apply them to some common problems.
In addition to this, you will also look at the error-handling techniques available in C#. These enable you to take precautions in cases where errors are likely, and write code that is resilient enough to cope with errors that might otherwise be fatal. These techniques are part of the C# language rather than a debugging feature of VS, but VS does provide some tools to help you here too.
In this chapter you look at:
The debugging methods available in Visual Studio
The error-handling techniques available in C#
|
Beginning Visual C# 2005 Authors: Watson K., Nagel C., Pedersen J. Published year: 2005 Pages: 49-51/278 |