Programming C#: Building .NET Applications with C# [NOOK Book]

Overview

The programming language C# was built with the future of application development in mind. Pursuing that vision, C#'s designers succeeded in creating a safe, simple, component-based, high-performance language that works effectively with Microsoft's .NET Framework. Now the favored language among those programming for the Microsoft platform, C# continues to grow in popularity as more developers discover its strength and flexibility. And, from the start, C# developers have relied on Programming C# both as an ...

See more details below
Programming C#: Building .NET Applications with C#

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study
  • NOOK for Web

Want a NOOK? Explore Now

NOOK Book (eBook)
$17.49
BN.com price
(Save 43%)$30.99 List Price

Overview

The programming language C# was built with the future of application development in mind. Pursuing that vision, C#'s designers succeeded in creating a safe, simple, component-based, high-performance language that works effectively with Microsoft's .NET Framework. Now the favored language among those programming for the Microsoft platform, C# continues to grow in popularity as more developers discover its strength and flexibility. And, from the start, C# developers have relied on Programming C# both as an introduction to the language and a means of further building their skills.


The fourth edition of Programming C#--the top-selling C# book on the market--has been updated to the C# ISO standard as well as changes to Microsoft's implementation of the language. It also provides notes and warnings on C# 1.1 and C# 2.0.


Aimed at experienced programmers and web developers, Programming C#, 4th Edition, doesn't waste too much time on the basics. Rather, it focuses on the features and programming patterns unique to the C# language. New C# 2005 features covered in-depth include:

  • Visual Studio 2005
  • Generics
  • Collection interfaces and iterators
  • Anonymous methods
  • New ADO.NET data controls
  • Fundamentals of Object-Oriented Programming

Author Jesse Liberty, an acclaimed web programming expert and entrepreneur, teaches C# in a way that experienced programmers will appreciate by grounding its applications firmly in the context of Microsoft's .NET platform and the development of desktop and Internet applications.


Liberty also incorporates reader suggestions from previous editions to help create the most consumer-friendly guide possible.
Read More Show Less

Editorial Reviews

From Barnes & Noble
The Barnes & Noble Review
If you're ready to start learning C#, your timing is perfect. Jesse Liberty -- who, we're convinced, could teach programming to chimpanzees -- has just completed his new introduction to the language. You can't beat Liberty for clarity and simplicity, and you can't beat his Programming C# for thoroughness, either.

Liberty starts with a quick overview of .NET and a quick Hello, World. Then, it's on to a tour of the fundamentals, from the language's strongly typed nature to its use of namespaces, with a focus on its object-oriented features.

Once you're comfortable building console applications, Liberty moves on to Windows applications, web applications, and (briefly) web services. There's a full chapter on accessing databases with ADO.NET, which updates ADO with a new disconnected data architecture that works much like web servers do, avoiding constant connections that waste resources.

C# introduces several powerful new techniques and appropriates many of the best techniques introduced by other contemporary languages. Liberty explains each innovation in detail, including delegates, assemblies, versioning, attributes, reflection, marshaling, and remoting. Whether C# is your first language or your tenth, Programming C# is your express route to real mastery. (Bill Camarda)

Bill Camarda is a consultant, writer, and web/multimedia content developer with nearly 20 years' experience in helping technology companies deploy and market advanced software, computing, and networking products and services. His 15 books include Special Edition Using Word 2000 and Upgrading & Fixing Networks For Dummies®, Second Edition.

Read More Show Less

Product Details

  • ISBN-13: 9780596552725
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 2/22/2005
  • Sold by: Barnes & Noble
  • Format: eBook
  • Edition number: 4
  • Pages: 672
  • Sales rank: 666,777
  • File size: 6 MB

Meet the Author

Jesse Liberty is the best selling author of Programming ASP.NET, Programming C#, and a dozen other books on web and object oriented programming. He is president of Liberty Associates, Inc., where he provides contract programming, consulting and on-site training in ASP.NET, C#, C++ and related topics. Jesse has been a Distinguished Software Engineer at AT&T and Vice President for technology development at CitiBank.

Read More Show Less

Read an Excerpt

Chapter 18: Attributes and Reflection

Throughout this book, I have emphasized that a .NET application contains code, data, and metadata. Metadata is information about the data--that is, information about the types, code, assembly, and so forth--that is stored along with your program. This chapter will explore how some of that metadata is created and used.

Attributes are a mechanism for adding metadata, such as compiler instructions and other data about your data, methods, and classes, to the program itself. Attributes are inserted into the metadata and are visible through ILDasm and other metadata-reading tools.

Reflection is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior.

Attributes

An attribute is an object that represents data you want to associate with an element in your program. The element to which you attach an attribute is referred to as the target of that attribute. For example, the attribute:


[NoIDispatch]

is associated with a class or an interface to indicate that the target class should derive from IUnknown rather than IDispatch, when exporting to COM. COM interface programming is discussed in detail in Chapter 22.

In Chapter 17, you saw this attribute:


[assembly: AssemblyKeyFile("c:\myStrongName.key")]

This inserts metadata into the assembly to designate the program's StrongName.

Intrinsic Attributes

Attributes come in two flavors: intrinsic and custom. Intrinsic attributes are supplied as part of the Common Language Runtime (CLR), and they are integrated into .NET. Custom attributes are attributes you create for your own purposes.

Most programmers will use only intrinsic attributes, though custom attributes can be a powerful tool when combined with reflection, described later in this chapter.

Attribute Targets

If you search through the CLR, you'll find a great many attributes. Some attributes are applied to an assembly, others to a class or interface, and some, such as [WebMethod], to class members. These are called the attribute targets. Possible attribute targets are detailed in Table 18-1.

Table 18-1: Possible attribute targets

Member Name

Usage

All

Applied to any of the following elements: assembly, class, class member, delegate, enum, event, field, interface, method, module, parameter, property, return value, or struct

Assembly

Applied to the assembly itself

Class

Applied to instances of the class

ClassMembers

Applied to classes, structs, enums, constructors, methods, properties, fields, events, delegates, and interfaces

Constructor

Applied to a given constructor

Delegate

Applied to the delegated method

Enum

Applied to an enumeration

Event

Applied to an event

Field

Applied to a field

Interface

Applied to an interface

Method

Applied to a method

Module

Applied to a single module

Parameter

Applied to a parameter of a method

Property

Applied to a property (both get and set, if implemented)

ReturnValue

Applied to a return value

Struct

Applied to a struct

Applying Attributes

You apply attributes to their targets by placing them in square brackets immediately before the target item. You can combine attributes, either by stacking one on top of another:


[assembly: AssemblyDelaySign(false)]

[assembly: AssemblyKeyFile(".\\keyFile.snk")]

or by separating the attributes with commas:


[assembly: AssemblyDelaySign(false),
 
  assembly: AssemblyKeyFile(".\\keyFile.snk")]

TIP:   You must place assembly attributes after all using statements and before any code.

Many intrinsic attributes are used for interoperating with COM, as discussed in detail in Chapter 22. You've already seen use of one attribute ([WebMethod]) in Chapter 16. You'll see other attributes, such as the [Serializable] attribute, used in the discussion of serialization in Chapter 19.

The System.Runtime namespace offers a number of intrinsic attributes, including attributes for assemblies (such as the keyname attribute), for configuration (such as debug to indicate the debug build), and for version attributes.

You can organize the intrinsic attributes by how they are used. The principal intrinsic attributes are those used for COM, those used to modify the Interface Definition Language (IDL) file from within a source-code file, attributes used by the ATL Server classes, and attributes used by the Visual C++ compiler.

Perhaps the attribute you are most likely to use in your everyday C# programming (if you are not interacting with COM) is [Serializable]. As you'll see in Chapter 19, all you need to do to ensure that your class can be serialized to disk or to the Internet is add the [Serializable] attribute to the class:


[serializable]

class MySerializableClass

The attribute tag is put in square brackets immediately before its target--in this case, the class declaration.

The key fact about intrinsic attributes is that you know when you need them; the task will dictate their use.

Custom Attributes

You are free to create your own custom attributes and use them at runtime as you see fit. Suppose, for example, that your development organization wants to keep track of bug fixes. You already keep a database of all your bugs, but you'd like to tie your bug reports to specific fixes in the code.

You might add comments to your code along the lines of:


// Bug 323 fixed by Jesse Liberty 1/1/2005.

This would make it easy to see in your source code, but there is no enforced connection to Bug 323 in the database. A custom attribute might be just what you need. You would replace your comment with something like this:


[BugFixAttribute(323,"Jesse Liberty","1/1/2005") 

Comment="Off by one error"]

You could then write a program to read through the metadata to find these bug-fix notations and update the database. The attribute would serve the purposes of a comment, but would also allow you to retrieve the information programmatically through tools you'd create.

Declaring an Attribute

Attributes, like most things in C#, are embodied in classes. To create a custom attribute, you derive your new custom attribute class from System.Attribute:


public class BugFixAttribute : System.Attribute

You need to tell the compiler with which kinds of elements this attribute can be used (the attribute target). You specify this with (what else?) an attribute:


[AttributeUsage(AttributeTargets.Class |
 
   AttributeTargets.Constructor |
 
   AttributeTargets.Field |
 
   AttributeTargets.Method |
 
   AttributeTargets.Property,
 
   AllowMultiple = true)]

AttributeUsage is an attribute applied to attributes: a meta-attribute. It provides, if you will, meta-metadata--that is, data about the metadata. For the AttributeUsage attribute constructor, you pass two arguments. The first argument is a set of flags that indicate the target--in this case, the class and its constructor, fields, methods, and properties. The second argument is a flag that indicates whether a given element might receive more than one such attribute. In this example, AllowMultiple is set to true, indicating that class members can have more than one BugFixAttribute assigned.

Naming an Attribute

The new custom attribute in this example is named BugFixAttribute. The convention is to append the word Attribute to your attribute name. The compiler supports this by allowing you to call the attribute with the shorter version of the name. Thus, you can write:


[BugFix(123, "Jesse Liberty", "01/01/05", Comment="Off by one")]

The compiler will first look for an attribute named BugFix and, if it does not find that, will then look for BugFixAttribute.

Constructing an Attribute

Every attribute must have at least one constructor. Attributes take two types of parameters, positional and named. In the BugFix example, the programmer's name and the date are positional parameters, and comment is a named parameter. Positional parameters are passed in through the constructor and must be passed in the order declared in the constructor:


public BugFixAttribute(int bugID, string programmer, 

string date)

{
 
   this.bugID = bugID;
 
   this.programmer = programmer;
 
   this.date = date;

}

Named parameters are implemented as properties:


public string Comment

{
 
    get
 
    {
 
        return comment;
 
    }
 
    set
 
    {
 
        comment = value;
 
    }

}

It is common to create read-only properties for the positional parameters:


public int BugID

{
 
    get
 
    {
 
        return bugID;
 
    }

}

Using an Attribute

Once you have defined an attribute, you can put it to work by placing it immediately before its target. To test the BugFixAttribute of the preceding example, the following program creates a simple class named MyMath and gives it two functions. You'll assign BugFixAttributes to the class to record its code-maintenance history...

Read More Show Less

Table of Contents

Preface; About This Book; How the Book Is Organized; Who This Book Is For; C# Versus Visual Basic .NET; C# Versus Java; C# versus C++; Conventions Used in This Book; Support; We’d Like to Hear from You; Acknowledgements; The C# Language; Chapter 1: C# and the .NET Framework; 1.1 The .NET Platform; 1.2 The .NET Framework; 1.3 Compilation and the MSIL; 1.4 The C# Language; Chapter 2: Getting Started:"Hello World”; 2.1 Classes, Objects, and Types; 2.2 Developing "Hello World”; 2.3 Using the Visual Studio .NET Debugger; Chapter 3: C# Language Fundamentals; 3.1 Types; 3.2 Variables and Constants; 3.3 Expressions; 3.4 Whitespace; 3.5 Statements; 3.6 Operators; 3.7 Namespaces; 3.8 Preprocessor Directives; Chapter 4: Classes and Objects; 4.1 Defining Classes; 4.2 Creating Objects; 4.3 Using Static Members; 4.4 Destroying Objects; 4.5 Passing Parameters; 4.6 Overloading Methods and Constructors; 4.7 Encapsulating Data with Properties; 4.8 Readonly Fields; Chapter 5: Inheritance and Polymorphism; 5.1 Specialization and Generalization; 5.2 Inheritance; 5.3 Polymorphism; 5.4 Abstract Classes; 5.5 The Root of all Classes: Object; 5.6 Boxing and Unboxing Types; 5.7 Nesting Classes; Chapter 6: Operator Overloading; 6.1 Using the operator Keyword; 6.2 Supporting Other .NET Languages; 6.3 Creating Useful Operators; 6.4 Logical Pairs; 6.5 The Equals Operator; 6.6 Conversion Operators; Chapter 7: Structs; 7.1 Defining Structs; 7.2 Creating Structs; Chapter 8: Interfaces; 8.1 Implementing an Interface; 8.2 Accessing Interface Methods; 8.3 Overriding Interface Implementations; 8.4 Explicit Interface Implementation; Chapter 9: Arrays, Indexers, and Collections; 9.1 Arrays; 9.2 The foreach Statement; 9.3 Indexers; 9.4 Collection Interfaces; 9.5 Array Lists; 9.6 Queues; 9.7 Stacks; 9.8 Dictionaries; Chapter 10: Strings and Regular Expressions; 10.1 Strings; 10.2 Regular Expressions; Chapter 11: Handling Exceptions; 11.1 Throwing and Catching Exceptions; 11.2 Exception Objects; 11.3 Custom Exceptions; 11.4 Rethrowing Exceptions; Chapter 12: Delegates and Events; 12.1 Delegates; 12.2 Events; Programming with C#; Chapter 13: Building Windows Applications; 13.1 Creating a Simple Windows Form; 13.2 Creating a Windows Form Application; 13.3 XML Documentation Comments; 13.4 Deploying an Application; Chapter 14: Accessing Data with ADO.NET; 14.1 Relational Databases and SQL; 14.2 The ADO.Net Object Model; 14.3 Getting Started with ADO.NET; 14.4 Using ADO Managed Providers; 14.5 Working with Data-Bound Controls; 14.6 Changing Database Records; 14.7 ADO.NET and XML; Chapter 15: ProgrammingWeb Applications with Web Forms; 15.1 Understanding Web Forms; 15.2 Creating a Web Form; 15.3 Adding Controls; 15.4 Data Binding; 15.5 Responding to Postback Events; 15.6 ASP.NET and C#; Chapter 16: Programming Web Services; 16.1 SOAP, WSDL, and Discovery; 16.2 Building a Web Service; 16.3 Creating the Proxy; C# and the .NET CLR; Chapter 17: Assemblies and Versioning; 17.1 PE Files; 17.2 Metadata; 17.3 Security Boundary; 17.4 Versioning; 17.5 Manifests; 17.6 Multi-Module Assemblies; 17.7 Private Assemblies; 17.8 Shared Assemblies; Chapter 18: Attributes and Reflection; 18.1 Attributes; 18.2 Intrinsic Attributes; 18.3 Custom Attributes; 18.4 Reflection; 18.5 Reflection Emit; Chapter 19: Marshaling and Remoting; 19.1 Application Domains; 19.2 Context; 19.3 Remoting; Chapter 20: Threads and Synchronization; 20.1 Threads; 20.2 Synchronization; 20.3 Race Conditions and Deadlocks; Chapter 21: Streams; 21.1 Files and Directories; 21.2 Reading and Writing Data; 21.3 Asynchronous I/O; 21.4 Network I/O; 21.5 Web Streams; 21.6 Serialization; 21.7 Isolated Storage; Chapter 22: Programming .NET and COM; 22.1 Importing ActiveX Controls; 22.2 Importing COM Components; 22.3 Exporting .NET Components; 22.4 P/Invoke; 22.5 Pointers; C# Keywords; Colophon;

Jesse Liberty is the best selling author of Programming ASP.NET, Programming C#, and a dozen other books on web and object oriented programming. He is president of Liberty Associates, Inc., where he provides contract programming, consulting and on-site training in ASP.NET, C#, C++ and related topics. Jesse has been a Distinguished Software Engineer at AT&T and Vice President for technology development at CitiBank.

Read More Show Less

Customer Reviews

Average Rating 4.5
( 4 )
Rating Distribution

5 Star

(2)

4 Star

(2)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 4 Customer Reviews
  • Anonymous

    Posted January 19, 2007

    Great book

    Your Programming C# is very clear and informative. Every time I use this book, I think I should congratulate the author. Thank you for writing such a great book. I will certainly look for other ones from you.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted January 4, 2006

    An excellent book for developers new to C#

    Programming C#: Fourth Edition is a good beginner¿s guide to the C# programming language. Having purchased the previous Version 3 edition, I was interested in the new 2.0 features. Unfortunately, the introduction states that a proficient C # 1.1 programmer should look at another book. The book structure covers the same topics the previous version does. New figures illustrate the Visual Studio 2005 environment rather than the older VS 2003 environment. Overall, I enjoy the writing style that Jesse Liberty gives in his books. I highly recommend purchasing this book and the Programming ASP.Net book.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted November 21, 2005

    Easy to read and easy to implement

    This book was one of the few technical books I enjoyed reading regardless of need. I go this book to help with developing projects for work, but now I like to just browse through it to pick up better programming habits. I have also read Programming ASP.NET without realizing the author was the same. I could not help but chuckle when I realized this because I had suggested these two books as great examples of good technical books.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted March 27, 2005

    easy transition from Java, if you want to

    [A review of the 4th edition 2005] As a java programmer, reading a C# book is like wandering into a parallel universe. Most things are different, but everything is recognisable. Liberty walks us through the syntax of C# and then how to program in it. All the nice things in Java can be found in C#. Strong typing. Automatic garbage collection. Interfaces. Introspection... As the author mentions, C# came out in 2000, while Java did so in 96. Those 4 years let C#'s designers effectively make it a superset of Java. Some of you who might be tempted to read this book will be Java programmers. Well, as a Java programmer, I'd have to say that if you like Java, then intrinsically, you should also like C#. If you have to shift, for career reasons perhaps, then this book might be reassuring. You can re-express your expertise in C# with relatively little effort. The syntax is not too dis-similar. Likewise, the code snippets, necessarily short though they are, will probably follow the same logical ordering as in Java.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing all of 4 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)