- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
C#
INSERT ATTRIBUTES
C# provides attributes so you can specify additional information about the functionality of a string of code. For example, you can add an attribute to a
string of code that points to another file for the program to open or to a type in another program.
Attributes retrieve information by using the reflection process. When your C# program runs, the attributes obtain information about program assemblies — the collection of types used in your program — and the types within those assemblies including classes, interfaces, and value types.
When your C# program encounters an attribute in a string of code, the program invokes the attribute. For example, if
your C# program encounters an attribute for accessing an external file, such as a text file, the file will open when you and your users access that string of code.
C# contains three reserved attributes: AttributeUsage, which decribes how a custom attribute class is used; Conditional, which marks a conditional method; and Obsolete, which marks a piece of unusable code. You can also create your own custom attributes by definining an attribute class. C# gives you the building blocks of attribute creation by deriving all custom attributes from the built-in
System.Attribute class.
INSERT ATTRIBUTES
Console
Applicatio
¤ Click New Project in the
Start page.
■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for your file.
ˇ Click OK.
WORKING WITH VISUAL C# BASICS 3
The Visual C# reflection system comes in the form of a built-in method called GetCustomAttributes. The GetCustomAttributes class returns those custom attributes as an array of objects that you can view in your program output.
TYPE THIS:
using System; [Obsolete]
class GetAttribute
{
static void Main(string[] args)
{
Type x = typeof(GetAttribute);
object[] xget = x.GetCustomAttributes(); Console.WriteLine(“The custom attribute is:”); foreach (object y in xget)
Console.WriteLine (y);
}
}
RESULT:
The custom attribute is
System.ObsoleteAtt ribute.
■ The Class1.cs code appears in the parent window.
Á Delete the summary description code for Class1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
‡ Type the preprocessor |
° Remove the comments |
· Type code that outputs a |
||||||
and additional System |
within the Main method. |
string from the Main method |
||||||
namespace at top, the class |
|
|
and then gives permission to |
|||||
to output data, and the output |
|
|
access the One method. |
|||||
statement for the first method. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CONTINUED |
|
47
C#
INSERT ATTRIBUTES
C# provides three built-in attributes. One provides the necessary building block for creating custom attributes. The other two attributes provide common
attribute functions in C# programs: making a method conditional and marking a string of code that is obsolete. For example, you can set an attribute that will run a method only if the user-entered password matches the password in your program.
The AttributeUsage attribute lets you define where you can apply your attributes. Visual C# contains built-in attribute values called AttributeTargets that let you determine what elements should include the attribute, such as a class.
The Conditional attribute lets you determine whether a string in your program will let a method execute. The Conditional attribute looks for a preprocessing identifier, such as input from the user, that tells the program whether it should execute a particular method or skip the entire method.
The Obsolete attribute lets you mark a section of code as that not to be used. You can set the Obsolete attribute to display a message when you encounter the code in your program (which is the preferred method so you can inform users about other options), or as a Boolean false attribute that will generate a compiler warning when you access that part of the program.
INSERT ATTRIBUTES (CONTINUED)
Main method |
± Run the program by |
calls NewMethod. |
pressing the F5 key. |
■ The Main method string appears first followed by the One method string and the
NewMethod string.
WORKING WITH VISUAL C# BASICS 3
The AttributeTargets class not only lets you specify the attribute target for an assembly or module but also for classes.
TYPE THIS:
using System; [AttributeUsage(AttributeTargets.Class)] class information Info : SystemAttribute
{
public Info(string name);
}
The AttributeTargets.Class argument within the AttributeUsage attribute tells your program that the program attributes apply to a class and not to any other target.
¡ Change the NewMethod
code in the Main method to
OldMethod.
™ Run the program by |
£ Save the program as the |
pressing the F5 key. |
filename. |
■ The obsolete method |
|
prompts the Task List to report |
|
that you are going the wrong |
|
way. |
|
49