Csharp Online 100


Published on

Published in: Technology
1 Comment
  • For brief detail of nullable type in c# please refer
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Csharp Online 100

  1. 1. Subscribe Now for FREE! refcardz.com tech facts at your fingertips CONTENTS INCLUDE: C# n String Literals n Delegates n Declaring Events n Generics Query Expressions (C# 3) By Jon Skeet n n Hot Tips and more... Verbatim string literal Regular string literal Result AbOUT ThIS REfCARD @quot;Here is a backslash quot; quot;Here is a backslash quot; Here is a backslash @quot;String on quot;String onrntwo linesquot; String on As C# has evolved over the past few years, there’s more and two linesquot; two lines more to remember. While it’s still a compact language, it’s @quot;Say quot;quot;Hello,quot;quot; and wave.quot; quot;Say quot;Hello,quot; and wave.quot; Say quot;Hello,quot; and wave. helpful to have an aide mémoire available when you just can’t @quot;ABCquot; quot;u0041x42U00000043quot; ABC remember that little bit of syntax which would be so handy right Table 2. Sample verbatim and regular string literals now. You’ll find this reference card useful whatever type of C# project you’re working on, and whichever version of C# you’re using. It covers many topics, from the basics of string escape DELEgATES sequences to the brave new world of query expressions and Delegates show up in various different contexts in .NET— LINQ in C# 3. for event handlers, marshalling calls to the UI thread in Windows Forms, starting new threads, and throughout LINQ. A delegate STRINg LITERALS type is known as a function type in other languages—it represents C# has two kinds of string literals—the regular ones, and some code which can be called with a specific sequence of verbatim string literals which are of the form @quot;textquot;. Regular parameters and will return a specific type of value. string literals have to start and end on the same line of source Delegate type declarations www.dzone.com code. A backslash within a string literal is interpreted as an Declaring a delegate type is like declaring a method, but with escape sequence as per table 1. the keyword delegate before it. For example: Escape sequence Result in string delegate bool StringPredicate(string x) ' Single quote (This is usually used in character literals. Character literals use the same escape sequences as string literals.) Any instance of the StringPredicate type declared above can be quot; Double quote invoked with a string parameter, and will return a Boolean value. Backslash Creating delegate instances 0 Unicode character 0 (the “null” character used to terminate C-style strings) Over the course of its evolution, C# has gained more and more a Alert (Unicode character 7) ways of creating delegate instances. b Backspace (Unicode character 8) C# 1 t Horizontal tab (Unicode character 9) In C# 1 only a single syntax was available to create a delegate n New line (Unicode character 10 = 0xa) instance from scratch . v Vertical quote (Unicode character 11 = 0xb) f Form feed (Unicode character 12 = 0xc) new delegate-type-name (method-name) r Carriage return (Unicode character 13 = 0xd) → uxxxx Unicode escape sequence for character with hex value xxxx. For example, u20AC is Unicode U+20AC, the Euro symbol. xnnnn Like u but with variable length—escape sequence stops at first Get More Refcardz (variable length) non-hexadecimal character. Very hard to read and easy to create bugs—avoid! (They’re free!) Uxxxxxxxx Unicode escape sequence for character with hex value xxxxxxxx— generates two characters in the result, used for characters not in n Authoritative content the basic multilingual plane. n Designed for developers Table 1. String/character escape sequences n Written by top experts n Latest tools & technologies Verbatim string literals can span multiple lines (the whitespace n Hot tips & examples is preserved in the string itself), and backslashes are not n Bonus content online interpreted as escape sequences. The only pseudo-escape n New issue every 1-2 weeks sequence is for double quotes—you need to include the double quotes twice. Table 2 shows some examples of verbatim string Subscribe Now for FREE! literals, regular string literal equivalents, and the actual resulting Refcardz.com C# string value. DZone, Inc. | www.dzone.com
  2. 2. 2 C# tech facts at your fingertips Delegates, continued However, lambda expressions have many special cases to The method name (known as a method group in the specification) make them shorter: can be prefixed by a type name to use a static method from a n If the compiler can infer the types of the parameters different type, or an expression to give the target of the delegate. (based on the context) then the types can be omitted. For instance, to create an instance of StringPredicate which will (C# 3 has far more powerful type inference than C# 2.) return true when passed strings which are five characters long or n If there is only a single parameter and its type is inferred, shorter, and false otherwise, you might use code like this: the parentheses around the parameter list aren’t needed. class LengthFilter n If the body of the delegate is just a single statement, the { braces around it aren’t needed—and for single-statement int maxLength; delegates returning a value, the return keyword isn’t needed. public LengthFilter(int maxLength) { Applying all of these shortcuts to our example, we end up with: this.maxLength = maxLength; StringPredicate predicate = text => text.Length <=5; } public bool Filter(string text) DECLARINg EvENTS { return text.Length <= maxLength; Events are closely related to delegates, but they are not the } same thing. An event allows code to subscribe and unsubscribe } using delegate instances as event handlers. The idea is that // In another class when an event is raised (for instance when a button is clicked) LengthFilter fiveCharacters = new LengthFilter(5); all the event handlers which have subscribed to the event are StringPredicate predicate = called. Just as a property is logically just the two operations new StringPredicate(fiveCharacters.Filter); get and set, an event is also logically just two operations: subscribe and unsubscribe. To declare an event and explicitly C# 2 write these operations, you use syntax which looks like a property C# 2 introduced two important improvements in the ways we declaration but with add and remove instead of get and set: can create delegate instances. public event EventHandler CustomEvent 1. You no longer need the new delegate-type part: { add StringPredicate predicate = fiveCharacters.Filter; { 2. Anonymous methods allow you to specify the logic of // Implementation goes here: “value” variable is the // handler being subscribed to the event the delegate in the same statement in which you create the } delegate instance. The syntax of an anonymous method is remove simply the keyword delegate followed by the parameter list, { // Implementation goes here: “value” variable is the then a block of code for the logic of the delegate. // handler being unsubscribed from the event } All of the earlier code to create a StringPredicate can be } expressed in a single statement: StringPredicate predicate = delegate (string text) Don’t forget that a single delegate instance can { return text.Length <= 5; } ; Hot refer to many actions, so you only need one Note that you don’t declare the return type of the anonymous Tip variable even if there are multiple subscribers. method—the compiler checks that every return value within the anonymous method can be implicitly converted to the return type of the delegate. If you don’t need to use any of the Many events are implemented using a simple variable to store delegate’s parameters, you can simply omit them. For instance, the subscribed handlers. a StringPredicate which returns a result based purely on the C# allows these events to be created simply, as field-like events: time of day might look like this: public event EventHandler SimpleEvent; StringPredicate predicate = delegate { return DateTime.Now.Hour >= 12; } ; This declares both the event and a variable at the same time. It’s roughly equivalent to this: One important feature of anonymous methods is that they have access to the local variables of the method in which private EventHander __hiddenField; public event EventHandler SimpleEvent they’re created. This implements the notion of closures in other { languages. There are important subtleties involved in closures, add so try to keep things simple. See http://csharpindepth.com/ { Articles/Chapter5/Closures.aspx or chapter 5 of C# in Depth lock(this) { (Manning, 2008) for more details. __hiddenField += value; } C# 3 } C# 3 adds lambda expressions which are like anonymous remove { methods but even more concise. In their longest form, lambda lock(this) expressions look very much like anonymous methods, but with { => after the parameter list instead of delegate before it: __hiddenField -= value; } StringPredicate predicate = } (string text) => { return text.Length <=5; }; } DZone, Inc. | www.dzone.com
  3. 3. 3 C# tech facts at your fingertips Declaring Events, continued Everywhere you refer to SimpleEvent within the declaring type, The C# compiler also lifts operators and Hot the compiler actually references __hiddenField, which is why Tip conversions—for instance, because int has an you’re able to raise the event by calling SimpleEvent(). Outside addition operator, so does Nullable<int>. the type declaration, however, SimpleEvent only refers to the Beware of one conversion you might not expect or want event. This duality has caused confusion for many developers— to happen—a comparison between a normal non-nullable value type, and the null literal. Here’s some code you you just need to remember that fields and events really are might not expect to compile: very different things, and field-like events are just the compiler doing some work for you. int i = 5; if (i == null) { NULLAbLE vALUE TypES ... } Nullable value types were introduced in .NET 2.0 and C# 2, with How can it possibly be null? It can’t, of course, but the support being provided by the framework, the language and compiler is using the lifted == operator for Nullable<int> the runtime. The principle is quite straightforward: a new struct which makes it legal code. Fortunately the compiler issues System.Nullable<T> has been introduced which contains a a warning in this situation. value of type T (which must be another value type) and a flag to say whether or not this value is quot;nullquot;. The HasValue property returns whether or not it’s a null value, and the Value property gENERICS returns the embedded value or throws an exception if you try to call it on a null value. This is useful when you want to represent The biggest feature introduced in C# 2 was generics—the the idea of an unknown value. ability to parameterise methods and types by type parameters. It’s an ability which is primarily used for collections by most The runtime treats nullable value types in a special manner when people, but which has a number of other uses too. I can’t cover it comes to boxing and unboxing. The boxed version of a generics in their entirety in this reference card — please read Nullable<int> is just a boxed int or a null reference if the online documentation or a good book about C# for a thorough original value was a null value. When you unbox, you can either grounding on the topic— but there are some areas which are unbox to int or to Nullable<int> (this follows for all nullable useful to have at your fingertips. Following are some references: value types—I’m just using int as a concrete example). Reference Resource C# support for nullable value types MSDN http://msdn.microsoft.com/en-us/library/512aeb7t.aspx C# in Depth http://books.dzone.com/books/csharp C# adds a sprinkling of syntactic sugar. Firstly, writing (Manning Publications) Nullable<int> etc. can get quite tedious—so C# lets you just add a question mark to the normal type name to mean “the Syntax: declaring generic types and methods nullable version”. Thus Nullable<int> and int? are exactly the Only types and methods can be generic. You can have other same thing, and can be used entirely interchangeably. members which use the type parameter of declaring type (just as the Current property of IEnumerable<T> is of type T, for The null-coalescing operator has been introduced to make example) but only types and methods can introduce new type working with null values (both of nullable value types and parameters. normal reference types) easier. Consider this expression: For both methods and types, you introduce new type left ?? right parameters by putting them after the name, in angle brackets. At execution time, first left is evaluated. If the result is non-null, If you need more than one type parameter, use commas to separate them. Here are examples, both from List<T> (one that’s the result of the whole expression and right is never of the most commonly used generic types). (In MSDN a lot evaluated. Otherwise, right is evaluated and that’s the result of of other interfaces are also listed, but they’re all covered by the expression. The null-coalescing operator is right associative, ICollection<T> anyway.) which means you can string several expressions together like this: Generic type declaration: first ?? second ?? third ?? fourth ?? fifth public class List<T> : ICollection<T> The simple way of understanding this is that each expression Generic method declaration: is evaluated, in order, until a non-null result is found, at which public List<TOutput> ConvertAll<TOutput> point the evaluation stops. (Converter<T, TOutput> converter) A few things to note here: One thing to be aware of is that there is also You can use the newly introduced type parameters for the rest Hot n Tip a nongeneric class System.Nullable, which of the declaration—the interfaces a type implements, or its just provides some static support methods for base type it derives from, and in the parameters of a method. nullable types. Don’t get confused between Nullable n Even though ConvertAll uses both T and TOutput, it only and Nullable<T>. introduces TOutput as a type parameter—the T in the declaration is the same T as for the List<T> as a whole. → DZone, Inc. | www.dzone.com
  4. 4. 4 C# tech facts at your fingertips Generics, continued You can specify different sets of constraints for different type parameters; each type parameter’s constraints are introduced n The parameter to ConvertAll is another generic type— with an extra where. All of the examples below would be in this case, a generic delegate type, representing a valid type declarations (and the equivalent would be valid for delegate which can convert a value from one type ( T in method declarations too): this case) to another (TOutput). class Simple<T> where T : Stream, new() n Method signatures can get pretty hairy when they use class Simple<T> where T : struct, IComparable<T> a lot of type parameters, but if you look at where each class Simple<T, U> where T : class where U : struct class Odd<T, U> where T : class where U : struct, T one has come from and what it’s used for, you can tame class Bizarre<T, U, V> where T : Stream, IEnumerable, even the wildest declaration. IComparable, U, V Type constraints The Odd class may appear to have constraints which are impossible to satisfy—how can a value type inherit from a You can add type constraints to generic type or method reference type? Remember that the “class” constraint also declarations to restrict the set of types which can be used, with includes interfaces, so Odd<IComparable,int> would be valid, the where contextual keyword. For instance, Nullable<T> has for example. It’s a pretty strange set of constraints to use though. a constraint so that T can only be a non-nullable value type— you can’t do Nullable<string> or Nullable<Nullable<int>>, Using type parameters for example. Table 3 shows the constraints available. We’ve seen that you can use type parameters in type and method declarations, but you can do much more with them, Syntax Notes treating them much like any “normal” type name: T : class T must be a reference type—a class or delegate, an array, or an interface n Declare variables using them, such as: T : struct T must be a non-nullable value type (e.g. int, Guid, DateTime) T currentItem; T : Stream T must inherit from the given type, which can be a class, interface, or T[] buffer; T : IDisposable another type parameter. (T can also be the specified type itself – for IEnumerable<T> sequence; T:U instance, Stream satisfies T : Stream.) n Use typeof to find out at execution time which type is T : new() T must have a parameterless constructor. This includes all value types. actually being used: Table 3. Type constraints for generic type parameters Type t = typeof(T); n Use the default operator to get the default value for that In both methods and type declarations, the constraints come type. This will be null for reference types, or the same result just before the opening brace of the body of the type/method. returned by new T() for value types. For example: For example: T defaultValue = default(T); // Generic type public class ResourceManager<T> where T : IDisposable n Create instances of other generic classes: { sequence = new LinkedList<T>(); // Implementation } // Generic method Note that the typeof operator can be used to get public void Use<T>(Func<T> source, Action<T> action) where T : IDisposable FYI generic types in their “open” or “closed” forms, { e.g. typeof(List<>) and typeof(List<int>). Re- using (T item = source()) flection with generic types and methods is tricky, { but MSDN (http://msdn.microsoft.com/library/System.Type. action(item); IsGenericType.aspx) has quite good documentation on it. } } Type constraints can be combined (comma-separated) so Lack of variance: long as you follow certain rules. For each type parameter: why a List<Banana> isn’t a List<Fruit> n Only one of “class” or “struct” can be specified, and it Probably the most frequently asked question around .NET has to be the first constraint. generics is why it doesn’t allow variance. This comes in two forms: covariance and contravariance—but the actual terms n You can’t force a type parameter to inherit from two aren’t as important as the principle. Many people initially different classes, and if you specify a class it must be the expect the following code to compile: first inheritance constraint. (However, you can specify a class, multiple interfaces, and multiple type parameters— List<Banana> bananas = new List<Banana>(); List<Fruit> fruit = bananas; unlikely as that may be!) It would make a certain amount of sense for that to work – after n You can’t force a type parameter to inherit from a sealed all, if you think of it in human language, a collection of bananas class, System.Object, System.Enum, System.ValueType or is a collection of fruit. However, it won’t compile for a very good System.Delegate. reason. Suppose the next line of code had been: n You can’t specify a “class” constraint and specify a class fruit.Add(new Apple()); to derive from as it would be redundant. That would have to compile—after all, you can add an Apple to n You can’t specify a “struct” constraint and a “new( )” a List<Fruit> with no problems... but in this case our list of fruit constraint—again, it would be redundant. is actually meant to be a list of bananas, and you certainly can’t n A “new()” constraint always comes last. add an apple to a list of bananas! → DZone, Inc. | www.dzone.com
  5. 5. 5 C# tech facts at your fingertips Generics, continued n The first parameter of an extension method can’t have any other modifiers such as out or ref. Unfortunately, when this becomes a problem n Unlike normal instance methods, extension methods can FYI you just have to work around it. That may mean be called “on” a null reference. In other words, don’t copying data into the right kind of collection, or assume that the first parameter will be non-null. it may mean introducing another type parameter somewhere n Extension methods are fabulous for allowing the result (i.e. making a method or type more generic). It varies on a of one method to be the input for the next. Again, this is case-by-case basis, but you’re in a better position to imple- how LINQ to Objects works—many of the extension ment the workaround when you understand the limitation. methods return an IEnumerable<T> (or another interface inheriting from it) which allows another method call to appear immediately afterwards. For example: ExTENSION METhODS people.Where(p => p.Age >=18 .OrderBy(p => p.LastName) Extension methods were introduced in C# 3. They’re static .Select(p => p.FullName) methods declared in static classes—but they are usually used as if they were instance methods of a completely different type! QUERy ExpRESSIONS (C# 3) This sounds bizarre and takes a few moments to get your head round, but it’s quite simple really. Here’s an example: If you've seen any articles at all on C# 3, you'll almost certainly using System; have seen a query expression, such as this: public static class Extensions from person in people { where person.Age >= 18 public static string Reverse(this string text) orderby person.LastName { char[] chars = text.ToCharArray(); select person.FullName Array.Reverse(chars); Query expressions are the “LIN” part of LINQ—they provide return new string(chars); } the language integration. The query expression above looks } very different from normal C#, but it is extremely readable. Even if you've never seen one before, I expect you can understand Note the use of the keyword this in the declaration of the the basics of what it's trying to achieve. text parameter in the Reverse method. That’s what makes it an extension method—and the type of the parameter is what Query expressions are translated into “normal” C# 3 as a makes it an extension method on string. It’s useful to be able to sort of pre-processor step. This is done in a manner which talk about this as the extended type of the extension method, knows nothing about LINQ itself— there are no ties between although that’s not official terminology. query expressions and the System.Linq namespace, or IEnumerable<T> and IQueryable<T>. The translation rules are all The body of the method is perfectly ordinary. Let’s see it in use: documented in the specification—there’s no magic going on, and class Test { you can do everything in query expressions in “normal” code too. static void Main() { The details can get quite tricky, but table 4 gives an example Console.WriteLine (quot;dlrow olleHquot;.Reverse()); of each type of clause available, as well as which methods are } called in the translated code. } Clause Full example Methods called for clause There’s no explicit mention of the Extensions class, and we’re First “from” from p in people n/a using the Reverse method as if it were an instance method on (implicit type) select p string. To let the compiler know about the Extensions class, we First “from” from Person p in people Cast<T> just have to include a normal using directive for the namespace (explicit type) select p (where T is the specified type) containing the class. That’s how IEnumerable<T> seems to Subsequent from p in people SelectMany “from” from j in jobs gain a load of extra methods in .NET 3.5—the System.Linq select new { Person=p, Job=j } namespace contains the Enumerable static class, which has lots where from p in people Where of extension methods. A few things to note: where p.Age >= 18 select p n Extension methods can only be declared in static select from p in people Select non-nested classes. select p.FullName n If an extension method and a regular instance method let from p in people Select let income = p.Salary + are both applicable, the compiler will always use the p.OtherIncome instance method. select new { Person=p, Income=income} n Extension methods work under the hood by the orderby from p in people OrderBy compiler adding the System.Runtime.CompilerServices. orderby p.LastName, OrderByDescending p.FirstName, ThenBy ExtensionAttribute attribute to the declaration. If you want p.Age descending ThenByDescending to target .NET 2.0 but still use extension methods, you just (depending on clauses) need to write your own version of the attribute. (It doesn’t join from p in people Join join job in jobs have any behaviour to implement.) on p.PrimarySkill equals job.RequiredSkill n The extended type can be almost anything, including value select p.FullName + quot;: quot; types, interfaces, enums and delegates. The only restriction + job.Description is that it can’t be a pointer type. Table 4. Clauses used in query expressions DZone, Inc. | www.dzone.com
  6. 6. 6 C# tech facts at your fingertips Query Expressions (C#3), continued var result = from p in people group p by p.LastName into family let size = family.Count() Clause Full example Methods called for clause where size > 4 join ... into from p in people GroupJoin select family.Key + quot;: quot; + size join job in jobs on p.PrimarySkill We can split the above into two separate query expressions: equals job.RequiredSkill into jobOptions var tmp = from p in people select p.FullName + quot;: quot; + jobOptions.Count() group p by p.LastName; group ... by from p in people GroupBy var result = from family in tmp group p by p.LastName let size = family.Count() where size > 4 Table 4. Clauses used in query expressions select family.Key + quot;: quot; + size; If a “select” or “group ... by” clause is followed by “into”, Splitting things up this way can help to turn a huge query into several it effectively splits the query into two. For instance, take the more manageable ones, and the results will be exactly the same. following query, which shows the size of all families containing This is only scratching the surface of LINQ. For further details, more than 4 people: I recommend reading LINQ in Action (Manning, 2008). AbOUT ThE AUThOR RECOMMENDED bOOK Jon Skeet C# in Depth is designed to bring Jon Skeet is a software engineer with experience in both C# and Java, you to a new level of programming currently working for Google in the UK. Jon has been a C# MVP since 2003, skill. It dives deeply into key C# helping the community through his newsgroup posts, popular web articles, topics—in particular the new ones and a blog covering C# and Java. Jon’s recent book C# in Depth looks in C# 2 and 3. Make your code at the details of C# 2 and 3, providing an ideal guide and reference for those who know C# 1 but want to gain expertise in the newer features. more expressive, readable and powerful than ever with LINQ and Publications a host of supporting features. Author of C# in Depth (Manning, 2008), co-author of Groovy in Action (Manning, 2007) Blog http://msmvps.com/jon.skeet bUy NOW Web Site books.dzone.com/books/csharp http://pobox.com/~skeet/csharp Want More? Download Now. Subscribe at refcardz.com Upcoming Refcardz: Available: n Core .NET Published August 2008 Published June 2008 fREE n Adobe Flex n Groovy n jQuerySelectors Core CSS I n Design Patterns n Published July 2008 n JSF n NetBeans IDE 6.1 Java Editor n Flexible Rails: Flex 3 on Rails 2 n Apache Struts 2 n RSS and Atom Published May 2008 n Core CSS II n GlassFish Application Server n Windows PowerShell Dependency Injection in EJB 3 Ruby n n n Silverlight 2 n JPA n IntelliJ IDEA Published April 2008 n Spring Configuration n Core CSS III GWT Style, Configuration n Getting Started with Eclipse n SOA Patterns and JSNI Reference n Getting Started with Ajax Published April 2008 DZone, Inc. 1251 NW Maynard ISBN-13: 978-1-934238-15-8 Cary, NC 27513 ISBN-10: 1-934238-15-5 50795 888.678.0399 DZone communities deliver over 3.5 million pages per month to 919.678.0300 more than 1.5 million software developers, architects and designers. Refcardz Feedback Welcome DZone offers something for every developer, including news, refcardz@dzone.com $7.95 tutorials, blogs, cheatsheets, feature articles, source code and more. Sponsorship Opportunities 9 781934 238158 “DZone is a developer’s dream,” says PC Magazine. sales@dzone.com Copyright © 2008 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, Version 1.0 photocopying, or otherwise, without prior written permission of the publisher. Reference: C# in Depth, Jon Skeet, Manning Publications, April 2008.