[A-Z] Uppercase letters
[ ] Space and tab
[x00-x1Fx7F] Control characters
[x21-x7E] Visible characters
[x20-x7E] Visible characters and spaces
[!'#$%&'()*+,-./:;<=>?@[\]_`{|}~] Punctuation characters
[ vf] Whitespace characters
w+([-+.']w+)*@w+([-.]w+)*.w+([-,]w+)* Email address
http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)? Internet URL
(((d{3}) ?)|(d{3}-))?d{3}-d{4} U.S. phone number
d{3}-d{2}-d{4} U.S. Social Security number
d{5}(-d{4})? U.S. ZIP code

To verify that an email address is correctly formatted, you can use the following statements with the specified regular expression:

string email = '[email protected]';

Regex r = new Regex(@'^[w-.]+@([w-]+.)+[w-] {2,4}$');

if (r.IsMatch(email))

 Console.WriteLine('Email address is correct.');

else

 Console.WriteLine('Email address is incorrect.');

There are many different regular expressions that you can use to validate an email address. However, there is no perfect regular expression to validate all email addresses. For more information on validating email addresses using regular expressions, check out the following web sites: http://regular-expressions.info/email.html and http://fightingforalostcause.net/misc/2006/compare-email-regex.php.

Summary

String manipulations are common operations, so it's important that you have a good understanding of how they work and the various methods and classes that deal with them. This chapter provided a lot of information about how strings are represented in C# and about using regular expressions to perform matching on strings.

Chapter 9

Generics

One of the new features in the .NET Framework (beginning with version 2.0) is the support of generics in Microsoft Intermediate Language (MSIL). Generics use type parameters, which allow you to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Generics enable developers to define type- safe data structures, without binding to specific fixed data types at design time.

Generics are a feature of the IL and not specific to C# alone, so languages such as C# and VB.NET can take advantage of them.

This chapter discusses the basics of generics and how you can use them to enhance efficiency and type safety in your applications. Specifically, you will learn:

□ Advantages of using generics

□ How to specify constraints in a generic type

□ Generic interfaces, structs, methods, operators, and delegates

□ The various classes in the .NET Framework class library that support generics

Understanding Generics

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату