What are Jagged Arrays?
A Jagged Array is an array of an array in which the length of each array index can differ.
Example: A Jagged Array can be used to create a table in which the lengths of the row are not same. This array is declared using square brackets ([ ]) to indicate each dimension.
The following code demonstrates the creation of a two-dimensional jagged array.Class Jagged{public static void Main(){int [][] jagged=new int [3][];jagged[0]=mew int[4]jagged[1]=mew int[3]jagged[2]=mew int[5]int I;‘Storing values in first arrayfor (I=0;I<4;I++)jagged[0][I]=I;‘Storing values in second array
for( I=0;I<3;I++)
jagged[1][I]=I;
‘Storing values in third array
for(I=0;I<5;I++)
jagged[2][I]=I;
‘Displaying values from first array
for (I=0;I<4;I++)
Console.WriteLine(jagged[0][I])
‘Displaying values from second array
for (I=0;I<3;I++)
Console.WriteLine(jagged[1][I])
‘Displaying values from third array
for(I=0;I<5;I++)
Console.WriteLine(jagged[2][I])
}
}
Monday, May 21, 2007
Debugging and Error Handling
Different Types are Errors are:
· Fatal Errors à Errors that cause applications to fail completely
· Syntax Errors à Simple Errors in code that prevent compilation
· Semantic Errors (also known as Logic Errors) à Application logic is in some way flawed.
Debugging Methods available in VS
Debug builds maintain symbolic information about your application, so that VS is capable of knowing exactly what is happening as each line of code is executed. The symbolic information is contained in .pdb files, which you may have seen appearing in Debug directories on your computer. This enables you to perform many useful operations, which include:
· Outputting debugging information to VS
· Looking at the values of variables in scope during application execution
· Pausing and restarting program execution
· Automatically halting execution at certain points in the code
· Stepping through program execution a line at a time
· Monitoring changes in variable content during application execution
· Modifying variable content at runtime
· Performing test calls of functions
In the Output Window, you can see information relating to the compilation and execution of code, including errors encountered during compilation and so forth. This window has two modes: Build mode and Debug mode. These modes show you compilation and runtime information, respectively.
Debugging in Nonbreak (Normal) Mode
How do you output information to the Output Window at runtime?
Writing text to the Output window at runtime is very simple. You simply need to replace calls to Console.WriteLine() with the required call to write text where you want it. There are two commands you can use to do this:
· Debug.WriteLine() à this command works only for debug build. This won’t even be compiled into a release build. It will just disappear.
· Trace.WriteLine() à this command works for both the debug and release builds
Two versions of your application can be created from a single source file. The debug version displays all kinds of extra diagnostic information, whereas the release version won’t have this overhead.
Syntax for the general output of these functions is as follows:
:
This allows you to see at a glance what output messages are displayed in the Output window, which is useful for times when similar messages are output from different places in the application.
For Example:
Debug.WriteLine(“Added 1 to I”, “MyFunc”);
Result is:
MyFunc: Added 1 to i
Where does the Debug.WriteLine() and Trace.WriteLine() commands are present?
In the System.Diagnostics namespace.
What are the other commands that are equivalent to Console.Write() command?
· Debug.Write()
· Trace.Write()
And the other different Commands are as follows and they take the Boolean value parameters:
· Debug.WriteLineIf()
· Trace.WriteLineIf()
· Debug.WriteIf()
· Trace.WriteIf()
What command is used as an alternative to writing information to the Output window?
An alternative to writing information to the Output window is to use trace points. This is a feature of VS rather than C#, but serve the same function as using Debug.WriteLine(). They output debugging information without modifying your code.
What are the steps to add a trace point to the code?
Error-Handling techniques available in C#
· Fatal Errors à Errors that cause applications to fail completely
· Syntax Errors à Simple Errors in code that prevent compilation
· Semantic Errors (also known as Logic Errors) à Application logic is in some way flawed.
Debugging Methods available in VS
Debug builds maintain symbolic information about your application, so that VS is capable of knowing exactly what is happening as each line of code is executed. The symbolic information is contained in .pdb files, which you may have seen appearing in Debug directories on your computer. This enables you to perform many useful operations, which include:
· Outputting debugging information to VS
· Looking at the values of variables in scope during application execution
· Pausing and restarting program execution
· Automatically halting execution at certain points in the code
· Stepping through program execution a line at a time
· Monitoring changes in variable content during application execution
· Modifying variable content at runtime
· Performing test calls of functions
In the Output Window, you can see information relating to the compilation and execution of code, including errors encountered during compilation and so forth. This window has two modes: Build mode and Debug mode. These modes show you compilation and runtime information, respectively.
Debugging in Nonbreak (Normal) Mode
How do you output information to the Output Window at runtime?
Writing text to the Output window at runtime is very simple. You simply need to replace calls to Console.WriteLine() with the required call to write text where you want it. There are two commands you can use to do this:
· Debug.WriteLine() à this command works only for debug build. This won’t even be compiled into a release build. It will just disappear.
· Trace.WriteLine() à this command works for both the debug and release builds
Two versions of your application can be created from a single source file. The debug version displays all kinds of extra diagnostic information, whereas the release version won’t have this overhead.
Syntax for the general output of these functions is as follows:
This allows you to see at a glance what output messages are displayed in the Output window, which is useful for times when similar messages are output from different places in the application.
For Example:
Debug.WriteLine(“Added 1 to I”, “MyFunc”);
Result is:
MyFunc: Added 1 to i
Where does the Debug.WriteLine() and Trace.WriteLine() commands are present?
In the System.Diagnostics namespace.
What are the other commands that are equivalent to Console.Write() command?
· Debug.Write()
· Trace.Write()
And the other different Commands are as follows and they take the Boolean value parameters:
· Debug.WriteLineIf()
· Trace.WriteLineIf()
· Debug.WriteIf()
· Trace.WriteIf()
What command is used as an alternative to writing information to the Output window?
An alternative to writing information to the Output window is to use trace points. This is a feature of VS rather than C#, but serve the same function as using Debug.WriteLine(). They output debugging information without modifying your code.
What are the steps to add a trace point to the code?
Error-Handling techniques available in C#
Delegates
Define Delegates?
A delegate is a type that enables you to store references to functions. Delegates are declared much like functions, but with no function body and using the delegate keyword.
How is the delegate signature defined?
The delegate declaration specifies a function signature consisting of a return type and the parameter list. After defining a delegate, you can declare a variable with the type of that delegate. You can then initialize this variable as a reference to any function that has the same signature as that delegate.
Example:
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1, double param2)
{
return param1 / param2;
}
static void Main (string[ ] args)
{
ProcessDelegate process;
………………
if (input == “M”)
process = new ProcessDelegate(Multiply)
else
process = new ProcessDelegate(Divide
Console.WriteLine(“Result: {0}”, process(param1, param2));
Console.ReadKey();
)
A delegate is a type that enables you to store references to functions. Delegates are declared much like functions, but with no function body and using the delegate keyword.
How is the delegate signature defined?
The delegate declaration specifies a function signature consisting of a return type and the parameter list. After defining a delegate, you can declare a variable with the type of that delegate. You can then initialize this variable as a reference to any function that has the same signature as that delegate.
Example:
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1, double param2)
{
return param1 / param2;
}
static void Main (string[ ] args)
{
ProcessDelegate process;
………………
if (input == “M”)
process = new ProcessDelegate(Multiply)
else
process = new ProcessDelegate(Divide
Console.WriteLine(“Result: {0}”, process(param1, param2));
Console.ReadKey();
)
Overloading Functions
Function overloading provides you with the ability to create multiple functions with the same name, but each working with different parameter types.
For Example:
Static int MaxValue (int[ ] intArray)
{
………………
return maxVal;
}
Static double MaxValue (double[ ] doubleArray)
{
………………
return maxVal;
}
Here both the function names MaxValue() is same but its signature is different. It would be error to define two functions with the same name and signature, but since these two functions have different signatures, this is fine.
The beauty of this type of code is that you don’t have to explicitly specify which of these two functions you wish to use. You simply provide an array parameter, and the correct function will be executed depending on the type of the parameter used.
All aspects of the function signature are included when overloading functions. For Example, you have two different functions that take parameters by value and by reference, respectively:
Static void ShowDouble (ref int val)
{
………………
}
Static void ShowDouble (int val)
{
………………
}
The choice as to which of these versions to use is based purely on whether the function call contains the ref keyword. The following would call the reference version:
ShowDouble (ref val);
And the following would call the value version:
ShowDouble (val);
For Example:
Static int MaxValue (int[ ] intArray)
{
………………
return maxVal;
}
Static double MaxValue (double[ ] doubleArray)
{
………………
return maxVal;
}
Here both the function names MaxValue() is same but its signature is different. It would be error to define two functions with the same name and signature, but since these two functions have different signatures, this is fine.
The beauty of this type of code is that you don’t have to explicitly specify which of these two functions you wish to use. You simply provide an array parameter, and the correct function will be executed depending on the type of the parameter used.
All aspects of the function signature are included when overloading functions. For Example, you have two different functions that take parameters by value and by reference, respectively:
Static void ShowDouble (ref int val)
{
………………
}
Static void ShowDouble (int val)
{
………………
}
The choice as to which of these versions to use is based purely on whether the function call contains the ref keyword. The following would call the reference version:
ShowDouble (ref val);
And the following would call the value version:
ShowDouble (val);
Struct Functions
Structs are used for storing multiple data elements in one place. Also it has the ability to contain functions as well as data.
Struct Example:
Struct CustomerName
{
public string firstName, lastName;
}
You can use the struct in your code as follows:
CustomerName myCustomer;
myCustomer.firstName = “John”;
myCustomer.lastName = “Franklin”;
Console.WriteLine(“{0} {1}”, myCustomer.firstName, myCustomer.lastName);
Adding Functions to Structs:
By adding functions to structs, you can simplify this by centralizing the processing of common tasks such as follows:
Struct CustomerName
{
public string firstName, lastName;
public string Name()
{
return firstName + “” + lastName;
}
}
Now you can use the struct in your code as follows:
CustomerName myCustomer;
myCustomer.firstName = “John”;
myCustomer.lastName = “Franklin”;
Console.WriteLine( myCustomer.Name());
What is the difference between a struct and a class in C#?
The list of similarities between classes and structs is as follows:
· Longstructs can implement interfaces and can have the same kinds of members as classes.
Structs differ from classes in several important ways; however,
· structs are value types rather than reference types, and inheritance is not supported for structs.
· Struct values are stored on the stack or in-line.
· Careful programmers can sometimes enhance performance through judicious use of structs.
For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.
Struct Example:
Struct CustomerName
{
public string firstName, lastName;
}
You can use the struct in your code as follows:
CustomerName myCustomer;
myCustomer.firstName = “John”;
myCustomer.lastName = “Franklin”;
Console.WriteLine(“{0} {1}”, myCustomer.firstName, myCustomer.lastName);
Adding Functions to Structs:
By adding functions to structs, you can simplify this by centralizing the processing of common tasks such as follows:
Struct CustomerName
{
public string firstName, lastName;
public string Name()
{
return firstName + “” + lastName;
}
}
Now you can use the struct in your code as follows:
CustomerName myCustomer;
myCustomer.firstName = “John”;
myCustomer.lastName = “Franklin”;
Console.WriteLine( myCustomer.Name());
What is the difference between a struct and a class in C#?
The list of similarities between classes and structs is as follows:
· Longstructs can implement interfaces and can have the same kinds of members as classes.
Structs differ from classes in several important ways; however,
· structs are value types rather than reference types, and inheritance is not supported for structs.
· Struct values are stored on the stack or in-line.
· Careful programmers can sometimes enhance performance through judicious use of structs.
For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.
The Main() Function
The Main() is the entry point for a C# application. When the execution is initiated, the Main() function executes and when the Main() function finishes the execution ends. There are four possible signatures you can use for the Main() function:
· Static void Main ()
· Static void Main (string[] args)
· Static int Main ()
· Static int Main (string[] args)
The args parameter of Main() is a method for accepting information from outside the application, specified at runtime. This information takes the form of command-line parameters. i.e. any command-line parameters that are specified are placed in this args array.
How do you add the command-line arguments?
The below method is the easiest way to add the arguments, rather than typing them at a command-line prompt every time.
· Right-click the project name in the Solution Explorer window and select Properties.
· Select the Debug page and add whatever command-line arguments you want to the Command Line Arguments setting
· Run the Application
· Static void Main ()
· Static void Main (string[] args)
· Static int Main ()
· Static int Main (string[] args)
The args parameter of Main() is a method for accepting information from outside the application, specified at runtime. This information takes the form of command-line parameters. i.e. any command-line parameters that are specified are placed in this args array.
How do you add the command-line arguments?
The below method is the easiest way to add the arguments, rather than typing them at a command-line prompt every time.
· Right-click the project name in the Solution Explorer window and select Properties.
· Select the Debug page and add whatever command-line arguments you want to the Command Line Arguments setting
· Run the Application
Parameters and Return Values versus Global Data
Limitations on using the global variables are:
· The global data might be modified by code elsewhere in your application, which could cause unpredicted results
· There are times when you only ever want to use a function for one purpose, and using a global data store reduces the possibility that you will make an error in a function call, perhaps passing it with the wrong variable.
· The global data might be modified by code elsewhere in your application, which could cause unpredicted results
· There are times when you only ever want to use a function for one purpose, and using a global data store reduces the possibility that you will make an error in a function call, perhaps passing it with the wrong variable.
Subscribe to:
Posts (Atom)