Skip to Main Content
C# in a Nutshell, Second Edition
book

C# in a Nutshell, Second Edition

by Peter Drayton, Ben Albahari, Ted Neward
August 2003
Intermediate to advanced content levelIntermediate to advanced
928 pages
32h 1m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell, Second Edition

Chapter 20. C# Language Reference

The following table describes the syntax of various C# language elements. The left column shows the syntax for each element, and the right column includes one or more examples.

Arrays

 
type [*]
+ array-name = 
[
  new type [ dimension+ ][*]*; |
  { value1, value2, ... };
]
byte[ ] arr1 = new byte[10];
int[ ] arr2 = {0, 1, 2};

([*] is the set: [ ] [,] [,,] etc.)

Attributes

[[target:]? attribute-name (
positional-param+ |
[named-param = expr]+ |
positional-param+, [named-param = 
                   expr]+)?]
[assembly:CLSCompliant(false)]
[WebMethod(true, 
  Description="My web method")]

Break statement

break;
break;

Checked/unchecked

checked (expr)


unchecked (expr)
// throws exception
short x = 32767;
int i = checked( (short) ++x );
// silently overflows to -32768
short y = 32767;
int j = unchecked( (short) ++y );
checked [statement | statement-block]






unchecked [statement | statement-block]
// throws exception
public short foo( ) {
  short y = 32767;
  checked {
    return ++y;
  }
}
// silently overflows
public short bar( ) {
  short y = 32767;
  unchecked {
    return ++y;
  }
}

Class declaration

                           attributes? unsafe? access-modifier?
new? 
[ abstract | sealed ]?
class class-name 
[: base-class | 
 : interface+ | 
 : base-class, interface+ ]?
{ class-members }
public class MyClass : Base, IFoo {
  // ...
}

Constant declaration

const type [variable = constant-expr]+;
const int xyzzy = 42;

Constant fields

                           attributes? access-modifier?
new?
const type [constant-name ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# in a Nutshell

C# in a Nutshell

Ben Albahari, Ted Neward, Peter Drayton
C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard

Publisher Resources

ISBN: 0596005261Catalog PageErrata