This document provides a summary of C# programming concepts including string literals, delegates, events, nullable value types, and generics. It explains the different types of string literals in C# and how escape sequences work. It describes how to declare delegate types and create delegate instances. It covers how to declare and raise events. It discusses nullable value types and how they are represented in C#. Finally, it provides an overview of generics in C# including how to declare generic types and methods.
This document provides a summary of key C# programming concepts in a concise reference card format. It includes definitions and examples of string literals, delegates, events, nullable value types, and generics. The document is intended as a handy reference for C# developers to have key syntax and concepts at their fingertips. It covers topics from basic string escaping to more advanced features introduced in recent versions of C#.
Wild & Weird Ideas: An Overview of Ruby 1.9Murray Steele
A presentation I gave at the London Ruby User Group (LRUG) in December 2007 about the changes in the ruby programming language in the soon to be released 1.9 version.
Note that Ruby 1.9 was still in development at the time I wrote this talk, so it's possible the stuff I say in it is completely inaccurate with respect to any currently released version of Ruby 1.9.
This document provides an overview and reference manual for the C programming language as implemented on PDP-11 systems. It describes the basic lexical conventions and tokens of C like comments, identifiers, keywords, and constants. It also covers fundamental data types like characters, integers, floats, and derived types like arrays, functions, pointers, and structures. The document discusses storage classes and object types, type conversions, and provides syntax notation to describe C language elements.
The workshop discussed approaches to error-tolerant audio coding for networked audio. The panelists presented on classification of audio error control strategies including error correction, error limiting, and error concealment. Error correction can be done with independent or dependent source coding. Dependent source coding prioritizes error protection based on perceptual significance. Error limiting aims to limit error propagation through frequent resynchronization points. Error concealment techniques try to reduce the perceptual impact of errors.
This document is a presentation on D Programming by Jonathan Mercier. It includes an introduction to D Programming and covers topics like objects, functions, parallelism, and basics of the language. The presentation is divided into sections on introduction, basics, GTK D, and thanks. It also provides context on why a new language was needed by listing the dates of other major languages like C++, Java, Python, and Ruby.
This document provides an overview of CSS (Cascading Style Sheets) including:
- A brief history noting the first proposal in 1994 and recommendation in 1996.
- Explanations of CSS versions from 1.0 to the modular 3.0 currently in development.
- The benefits of separating presentation from content using CSS including design flexibility, print/mobile support, and easier maintenance.
This document provides an overview and summary of the GlassFish application server. It discusses what GlassFish is, where to get it, and includes the following key points:
- GlassFish is an open source Java EE application server sponsored by Sun Microsystems (now Oracle) that supports the latest Java EE specifications.
- Distribution packages are available for several operating systems from the GlassFish download page or the Sun distribution page.
- The document provides information on GlassFish versions, documentation, and tips for administration, security, and performance.
This document provides a summary of key C# programming concepts in a concise reference card format. It includes definitions and examples of string literals, delegates, events, nullable value types, and generics. The document is intended as a handy reference for C# developers to have key syntax and concepts at their fingertips. It covers topics from basic string escaping to more advanced features introduced in recent versions of C#.
Wild & Weird Ideas: An Overview of Ruby 1.9Murray Steele
A presentation I gave at the London Ruby User Group (LRUG) in December 2007 about the changes in the ruby programming language in the soon to be released 1.9 version.
Note that Ruby 1.9 was still in development at the time I wrote this talk, so it's possible the stuff I say in it is completely inaccurate with respect to any currently released version of Ruby 1.9.
This document provides an overview and reference manual for the C programming language as implemented on PDP-11 systems. It describes the basic lexical conventions and tokens of C like comments, identifiers, keywords, and constants. It also covers fundamental data types like characters, integers, floats, and derived types like arrays, functions, pointers, and structures. The document discusses storage classes and object types, type conversions, and provides syntax notation to describe C language elements.
The workshop discussed approaches to error-tolerant audio coding for networked audio. The panelists presented on classification of audio error control strategies including error correction, error limiting, and error concealment. Error correction can be done with independent or dependent source coding. Dependent source coding prioritizes error protection based on perceptual significance. Error limiting aims to limit error propagation through frequent resynchronization points. Error concealment techniques try to reduce the perceptual impact of errors.
This document is a presentation on D Programming by Jonathan Mercier. It includes an introduction to D Programming and covers topics like objects, functions, parallelism, and basics of the language. The presentation is divided into sections on introduction, basics, GTK D, and thanks. It also provides context on why a new language was needed by listing the dates of other major languages like C++, Java, Python, and Ruby.
This document provides an overview of CSS (Cascading Style Sheets) including:
- A brief history noting the first proposal in 1994 and recommendation in 1996.
- Explanations of CSS versions from 1.0 to the modular 3.0 currently in development.
- The benefits of separating presentation from content using CSS including design flexibility, print/mobile support, and easier maintenance.
This document provides an overview and summary of the GlassFish application server. It discusses what GlassFish is, where to get it, and includes the following key points:
- GlassFish is an open source Java EE application server sponsored by Sun Microsystems (now Oracle) that supports the latest Java EE specifications.
- Distribution packages are available for several operating systems from the GlassFish download page or the Sun distribution page.
- The document provides information on GlassFish versions, documentation, and tips for administration, security, and performance.
The document provides information about formatting strings and dates/times in .NET. It discusses:
1) Common .NET types like string, bool, numeric types and their sizes.
2) How to format numbers, dates and times as strings using standard and custom format strings.
3) Examples of formatting numbers, dates and times with explanations of format specifiers.
OpenSSO is a single sign-on solution that can centralize authentication and authorization for web applications, web services, and federated access. It provides standards-based authentication, authorization, federation, web access management and web services security. OpenSSO includes an embedded directory server and supports pluggable authentication mechanisms. It allows for federated single sign-on across domains using protocols like SAML and WS-Federation. OpenSSO also includes a security token service and policy-based authorization to secure web services. Finally, OpenSSO Identity Services provides platform-independent access to OpenSSO functionality through web services.
This document summarizes Andreas Schneble's presentation on using email to drive high-volume lead generation. Some key points:
- Schneble led a global email marketing program at Sun Microsystems that increased leads generated by 100x in one year through systematic profiling campaigns, product workshops to develop compelling offers, and optimizing landing pages and registration incentives.
- Careful tracking and analysis of offer downloads, registrations and conversions helped optimize the program. Testing different subject lines and messages was important.
- Aligning marketing functions and taking a global, systematic approach drove major efficiencies over single, one-off emails. The program engaged marketing teams across products, countries and functions to fuel user passion and move leads toward revenue.
The OpenSSO roadmap outlines plans to improve ease-of-use through new task flows, introduce mobile one-time passwords and a reverse proxy, enhance monitoring capabilities, and strengthen entitlement enforcement. It also details upcoming federation capabilities for SaaS providers, a .NET Fedlet to simplify deploying service providers, and continued focus on performance and usability improvements.
Sun welcome middleware_overview 0324101_bosnia(2)Oracle BH
Oracle has acquired Sun Microsystems and is integrating Sun's middleware products into its strategy and product portfolio. Oracle plans to continue supporting key Sun products like GlassFish, Directory Server, and Identity Manager while transitioning customers to Oracle equivalents like WebLogic Server, Internet Directory, and Identity Manager. The acquisition allows Oracle to deliver a more complete stack of integrated hardware, software, and middleware to customers.
Initial Sketch on Capstone Fall 2016. Uses RFID, Beacons, Phones, NativeScript and AMTech to provide an App for attendees and presenters to the Capstone Festival
This document discusses activity management using Internet of Things (IOT) sensors and devices. It provides examples of how activity management can be used in retail stores, conferences, and transportation to track inventory, employee movement, attendee movement, bus arrivals, and more. It also describes the components of an activity management platform, including sensors, gateways, processing rules, data storage, and applications. The platform can operate on-premise, at the edge, or in the cloud and support various protocols and devices.
Chapter5: Usage of Build-In Functions or MethodsNgeam Soly
This document provides an overview of character and string handling in C#. It discusses character literals and data types, as well as common string operations like comparison, concatenation, indexing, and formatting. Methods for searching, manipulating, and building strings using classes like StringBuilder are also demonstrated through code examples.
Microsoft C# is a programming language designed for building enterprise applications that run on the .NET Framework. C# code is compiled as managed code, which benefits from services like garbage collection, language interoperability, security, and versioning support provided by the common language runtime. C# is introduced in Visual Studio .NET with project templates, designers, and other development tools. The .NET Framework library is used for C# programming. C# and Java share features like garbage collection, reflection, and interfaces to improve on C++.
This document contains a list of questions related to various topics in C programming such as linked lists, trees, bit manipulation, sorting, pointers, functions, statements, arrays, variables, and structures. For each topic, there are multiple questions listed along with a note indicating if the question has been updated recently or is new. Users can click on a question to find more details or see example code.
The document contains a list of questions related to data structures and algorithms in C language. There are questions about linked lists, trees, bit manipulation, sorting techniques and writing custom functions. Users can click on a question to see explanations, code samples and updates. The forum also allows users to ask their own questions and see responses.
This document contains a list of questions related to various topics in C programming such as linked lists, trees, bit manipulation, sorting, pointers, functions, statements, arrays, variables, and structures. For each topic, there are multiple questions listed along with a note indicating if the question has been updated recently or is new. Users can click on a question to find more details or see example code.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by treating each DFA state as a set of NFA states. Transitions are determined by taking the epsilon closure of the NFA states reachable on the input symbol from the current set of states. An example applies the algorithm to construct the DFA for the regular expression (a/b)n*abb from its NFA.
q Strings come in all shapes, sizes and programmers' preferences. They are value types that hold sequences of characters for appropriate definitions of value, character and sequence.
q Value object types are different from many other object types because they focus on fine-grained information rather than application entities. Therefore they are copyable, normally live on the heap or in other objects, and are not generally accessed by pointers.
q The core language offers C-style, null-terminated strings. The standard library offers a single string type. Unfortunately, it is by no means the best encapsulation. However, it is should still be used in preference to proprietary or lower-level solutions.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by taking each state of the NFA as a set and constructing the transition table of the DFA. Each state of the DFA is a set of NFA states. An example is provided to demonstrate converting the NFA for (a/b)n*abb to an equivalent DFA.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by treating each DFA state as a set of NFA states. Transitions are determined by taking the epsilon closure of the NFA states reachable on the input symbol from the current set of states. An example applies the algorithm to construct the DFA for the regular expression (a/b)n*abb from its NFA.
The document discusses constructing a non-deterministic finite automaton (NFA) from a regular expression and converting an NFA to a deterministic finite automaton (DFA). It provides an algorithm for constructing an NFA from a regular expression using Thompson's construction. It also provides an algorithm called the subset construction for converting an NFA to an equivalent DFA.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by keeping track of sets of NFA states. Each state of the DFA is a set of NFA states. The start state is the epsilon-closure of the NFA start state. New states are added by computing the epsilon-closure of the move function. The construction continues until all states are marked.
The document contains a list of questions related to C programming. It covers topics like linked lists, trees, sorting, pointers, functions, statements, arrays, variables, structures, macros, headers, file operations, and declarations/definitions. There are over 100 questions listed, ranging from basic concepts to more advanced topics. The questions provide examples of problems to solve or explanations to write in C code.
My slides from "Inside PHP", a talk about how to change the syntax of the PHP programming language.
Modified PHP 5.4.4 source code (with the "until" keyword added during this presentation) is available here:
http://github.com/thomaslee/oscon2012-inside-php
This document provides an overview of variables and constants in C#. It discusses the definition and initialization of variables, including value types like integers, floats, characters and reference types. Constants refer to fixed values that cannot be altered, and include integer, floating point, character and string literals. Various examples are provided of defining and initializing different types of variables and constants in C#.
The document provides an overview of the basics of C programming, including:
- An introduction to C as a general purpose, block structured, procedural programming language that has features of both high-level and low-level languages.
- Descriptions of C's character set, identifiers, keywords, data types, variables, constants, and operators.
- Explanations of common data types like integer, float, character, and pointers.
- An overview of program structure in C including declaration, initialization of variables, and use of operators, decision making statements, and loops.
The document provides information about formatting strings and dates/times in .NET. It discusses:
1) Common .NET types like string, bool, numeric types and their sizes.
2) How to format numbers, dates and times as strings using standard and custom format strings.
3) Examples of formatting numbers, dates and times with explanations of format specifiers.
OpenSSO is a single sign-on solution that can centralize authentication and authorization for web applications, web services, and federated access. It provides standards-based authentication, authorization, federation, web access management and web services security. OpenSSO includes an embedded directory server and supports pluggable authentication mechanisms. It allows for federated single sign-on across domains using protocols like SAML and WS-Federation. OpenSSO also includes a security token service and policy-based authorization to secure web services. Finally, OpenSSO Identity Services provides platform-independent access to OpenSSO functionality through web services.
This document summarizes Andreas Schneble's presentation on using email to drive high-volume lead generation. Some key points:
- Schneble led a global email marketing program at Sun Microsystems that increased leads generated by 100x in one year through systematic profiling campaigns, product workshops to develop compelling offers, and optimizing landing pages and registration incentives.
- Careful tracking and analysis of offer downloads, registrations and conversions helped optimize the program. Testing different subject lines and messages was important.
- Aligning marketing functions and taking a global, systematic approach drove major efficiencies over single, one-off emails. The program engaged marketing teams across products, countries and functions to fuel user passion and move leads toward revenue.
The OpenSSO roadmap outlines plans to improve ease-of-use through new task flows, introduce mobile one-time passwords and a reverse proxy, enhance monitoring capabilities, and strengthen entitlement enforcement. It also details upcoming federation capabilities for SaaS providers, a .NET Fedlet to simplify deploying service providers, and continued focus on performance and usability improvements.
Sun welcome middleware_overview 0324101_bosnia(2)Oracle BH
Oracle has acquired Sun Microsystems and is integrating Sun's middleware products into its strategy and product portfolio. Oracle plans to continue supporting key Sun products like GlassFish, Directory Server, and Identity Manager while transitioning customers to Oracle equivalents like WebLogic Server, Internet Directory, and Identity Manager. The acquisition allows Oracle to deliver a more complete stack of integrated hardware, software, and middleware to customers.
Initial Sketch on Capstone Fall 2016. Uses RFID, Beacons, Phones, NativeScript and AMTech to provide an App for attendees and presenters to the Capstone Festival
This document discusses activity management using Internet of Things (IOT) sensors and devices. It provides examples of how activity management can be used in retail stores, conferences, and transportation to track inventory, employee movement, attendee movement, bus arrivals, and more. It also describes the components of an activity management platform, including sensors, gateways, processing rules, data storage, and applications. The platform can operate on-premise, at the edge, or in the cloud and support various protocols and devices.
Chapter5: Usage of Build-In Functions or MethodsNgeam Soly
This document provides an overview of character and string handling in C#. It discusses character literals and data types, as well as common string operations like comparison, concatenation, indexing, and formatting. Methods for searching, manipulating, and building strings using classes like StringBuilder are also demonstrated through code examples.
Microsoft C# is a programming language designed for building enterprise applications that run on the .NET Framework. C# code is compiled as managed code, which benefits from services like garbage collection, language interoperability, security, and versioning support provided by the common language runtime. C# is introduced in Visual Studio .NET with project templates, designers, and other development tools. The .NET Framework library is used for C# programming. C# and Java share features like garbage collection, reflection, and interfaces to improve on C++.
This document contains a list of questions related to various topics in C programming such as linked lists, trees, bit manipulation, sorting, pointers, functions, statements, arrays, variables, and structures. For each topic, there are multiple questions listed along with a note indicating if the question has been updated recently or is new. Users can click on a question to find more details or see example code.
The document contains a list of questions related to data structures and algorithms in C language. There are questions about linked lists, trees, bit manipulation, sorting techniques and writing custom functions. Users can click on a question to see explanations, code samples and updates. The forum also allows users to ask their own questions and see responses.
This document contains a list of questions related to various topics in C programming such as linked lists, trees, bit manipulation, sorting, pointers, functions, statements, arrays, variables, and structures. For each topic, there are multiple questions listed along with a note indicating if the question has been updated recently or is new. Users can click on a question to find more details or see example code.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by treating each DFA state as a set of NFA states. Transitions are determined by taking the epsilon closure of the NFA states reachable on the input symbol from the current set of states. An example applies the algorithm to construct the DFA for the regular expression (a/b)n*abb from its NFA.
q Strings come in all shapes, sizes and programmers' preferences. They are value types that hold sequences of characters for appropriate definitions of value, character and sequence.
q Value object types are different from many other object types because they focus on fine-grained information rather than application entities. Therefore they are copyable, normally live on the heap or in other objects, and are not generally accessed by pointers.
q The core language offers C-style, null-terminated strings. The standard library offers a single string type. Unfortunately, it is by no means the best encapsulation. However, it is should still be used in preference to proprietary or lower-level solutions.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by taking each state of the NFA as a set and constructing the transition table of the DFA. Each state of the DFA is a set of NFA states. An example is provided to demonstrate converting the NFA for (a/b)n*abb to an equivalent DFA.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by treating each DFA state as a set of NFA states. Transitions are determined by taking the epsilon closure of the NFA states reachable on the input symbol from the current set of states. An example applies the algorithm to construct the DFA for the regular expression (a/b)n*abb from its NFA.
The document discusses constructing a non-deterministic finite automaton (NFA) from a regular expression and converting an NFA to a deterministic finite automaton (DFA). It provides an algorithm for constructing an NFA from a regular expression using Thompson's construction. It also provides an algorithm called the subset construction for converting an NFA to an equivalent DFA.
The document discusses constructing a DFA from a regular expression and NFA. It provides an algorithm for the subset construction which works by keeping track of sets of NFA states. Each state of the DFA is a set of NFA states. The start state is the epsilon-closure of the NFA start state. New states are added by computing the epsilon-closure of the move function. The construction continues until all states are marked.
The document contains a list of questions related to C programming. It covers topics like linked lists, trees, sorting, pointers, functions, statements, arrays, variables, structures, macros, headers, file operations, and declarations/definitions. There are over 100 questions listed, ranging from basic concepts to more advanced topics. The questions provide examples of problems to solve or explanations to write in C code.
My slides from "Inside PHP", a talk about how to change the syntax of the PHP programming language.
Modified PHP 5.4.4 source code (with the "until" keyword added during this presentation) is available here:
http://github.com/thomaslee/oscon2012-inside-php
This document provides an overview of variables and constants in C#. It discusses the definition and initialization of variables, including value types like integers, floats, characters and reference types. Constants refer to fixed values that cannot be altered, and include integer, floating point, character and string literals. Various examples are provided of defining and initializing different types of variables and constants in C#.
The document provides an overview of the basics of C programming, including:
- An introduction to C as a general purpose, block structured, procedural programming language that has features of both high-level and low-level languages.
- Descriptions of C's character set, identifiers, keywords, data types, variables, constants, and operators.
- Explanations of common data types like integer, float, character, and pointers.
- An overview of program structure in C including declaration, initialization of variables, and use of operators, decision making statements, and loops.
The document provides an overview of the basics of C programming, including:
- C is a general-purpose, block-structured, procedural programming language. It has features of both high-level and low-level languages.
- The main data types in C are integer, floating point, character, and void. Variables and constants are also introduced.
- Operators in C include arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators.
- The document discusses various C programming concepts like identifiers, keywords, tokens, data types, variables, constants, and operators in detail with examples.
This document provides an overview of C# 3.0 and 4.0 features. It discusses the evolution of C# and key features introduced in each version, including LINQ in C# 3.0 and dynamic programming in C# 4.0. The document also covers the .NET Framework and Common Language Runtime fundamentals that enable C# programming.
Regular expressions are a powerful tool for string manipulation that are implemented in Python using the "re" module. They are useful for identifying patterns in strings and performing substitutions. Some common regular expression functions in Python include re.match(), re.search(), re.findall(), and re.finditer(), which can be used to search for patterns at the beginning, anywhere in, or all occurrences in a string respectively. Regular expressions use special characters to match patterns flexibly, such as ^ and $ to match start/end, * for 0 or more matches, and | for alternatives. They enable tasks like extracting digits from strings, validating inputs, and performing search and replace.
Vibrant Technologies is headquarted in Mumbai,India.We are the best Perl Scripting training provider in Navi Mumbai who provides Live Projects to students.We provide Corporate Training also.We are Best Perl Scripting classes in Mumbai according to our students and corporators
Perl is an interpreted programming language optimized for string manipulation and file input/output. It supports regular expressions and variables do not require declaration. Perl scripts can be run by executing the script file from the command line on Windows and UNIX systems. The basic syntax includes using semicolons to end statements and hash symbols for comments. Perl supports scalars, lists, arrays, and hashes to store and manipulate data. Pattern matching in Perl uses regular expressions to test strings.
1. #16
tech facts at your fingertips
Get More Refcardz! Visit refcardz.com
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
@"Here is a backslash " "Here is a backslash " Here is a backslash
@"String on "String onrntwo lines" String on
As C# has evolved over the past few years, there’s more and two lines" two lines
more to remember. While it’s still a compact language, it’s @"Say ""Hello,"" and wave." "Say "Hello," and wave." Say "Hello," and wave.
helpful to have an aide mémoire available when you just can’t @"ABC" "u0041x42U00000043" 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 @"text". 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
" 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
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
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 "null". 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
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
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 ("dlrow olleH".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 + ": "
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