Keywords

C# has more keywords than Java. When both languages define the same keyword, they typically have the same function, although some equivalent features are available through different keywords. Each keyword will be discussed in detail in the appropriate section of this chapter, but we’ll begin by presenting a high-level comparison of the keywords available in both languages.

Package and Namespace Keywords

Although Java packages and C# namespaces are significantly different in their implementation and capabilities, both languages provide keywords for their use and declaration. Table 4-1 contrasts these keywords.

Table 4-1. Comparison of Package and Namespace Keywords

Java

C#

Comments

import

using

Makes the elements of a package/namespace accessible without the need to use fully qualified names

package

namespace

Declares program elements to be members of a named package/namespace

More Info

For details of packages and namespaces, refer to the Namespaces section later in this chapter.

Simple Type and Constant Keywords

Table 4-2 contrasts the Java and C# keywords associated with

  • Built-in constant values required by the languages: null, void, true, and false.

  • Built-in simple data types: char, int, byte, and so forth.

In this category, C# provides a superset of the keywords provided in Java. Because the Java Virtual Machine supports a restricted range of data types that doesn’t include unsigned integer types, Java does not have keywords to represent them. Java also doesn’t have the decimal type, although the BigDecimal class provides similar functionality.

More Info

For a comprehensive coverage of each keyword in Table 4-2, refer to the Types section in Chapter 5.

Table 4-2. Simple Type Comparison

Java

C#

Comments

boolean

bool

True or false values.

char

char

16-bit Unicode character.

byte

sbyte

8-bit signed integer (-128 to 127).

N/A

byte

8-bit unsigned integer (0 to 255).

short

short

16-bit signed integer (-32768 to 32767).

N/A

ushort

16-bit unsigned integer (0 to 65535).

int

int

32-bit signed integer (-2147483648 to 2147483647).

N/A

uint

32-bit unsigned integer (0 to 4294967295).

long

long

64-bit signed integer (-9223372036854775808 to 9223372036854775807).

N/A

ulong

64-bit unsigned integer (0 to 18446744073709551615).

float

float

32-bit double-precision floating point.

double

double

64-bit double-precision floating point.

N/A

decimal

128-bit high-precision decimal number with 28 significant digits. Intended for precise financial calculations.

true

true

Technically, true is not a Java keyword but a Boolean literal.

false

false

Technically, false is not a Java keyword but a Boolean literal.

null

null

Technically, null is not a Java keyword but a literal.

void

void

Identify a functional member as not returning a value.

Type Declaration, Access, and Manipulation Keywords

Table 4-3 contrasts the Java and C# keywords associated with declaring, accessing, and manipulating complex types and their members.

More Info

These keywords are covered in detail in the Modifiers section later in this chapter and in the Types and Members sections in Chapter 5.

Table 4-3. Complex Type Keyword Comparison

Java

C#

Comments

class

class

Declares a new class type.

const

const

Java reserves but does not implement the keyword const.

N/A

delegate

Declares a reference type that defines a method signature.

N/A

enum

Declares a value type defining a set of named constants.

N/A

event

Declares an event member in a class or struct.

N/A

this

Declares an indexer member in a class or struct. Duplicate use of the this keyword here is a syntax decision by the C# designers bound to cause confusion.

interface

interface

Declares a new interface type.

Object

object

Object is not a Java keyword but a class name. C# defines the object keyword, which is an alias for the System.Object class.

N/A

operator

Declares an operator member in a class or struct.

String

string

Unicode String values. String is not a Java keyword but a class name. C# defines the string keyword, which is an alias for the System.String class.

N/A

struct

Declares a new struct type, a complex C# type that has no Java equivalent.

extends

:

No keyword used in C#. Syntax is

class : super_class {...}

implements

:

No keyword used in C#. Syntax is

class : interface {...}

N/A

params

Used to identify a parameter array in a method call.

N/A

out

Identifies a variable as not needing initialization prior to being passed to a method where its value will be set. Note that unlike the case with Java, the use of out in C# has nothing to do with I/O.

N/A

ref

Identifies a variable that is to be passed by reference instead of by value.

N/A

as

Performs a cast but returns null if the cast fails instead of throwing an exception.

instanceof

is

Determines whether an object is an instance of a specified type.

new

new

Creates an instance of a type.

N/A

typeof

Returns an instance of System.Type for the specified type.

super

base

Used to reference superclass members.

this

this

Self-reference for use within object instances.

N/A

explicit

Declares an explicit type conversion operator.

N/A

implicit

Declares an implicit type conversion operator.

Modifier Keywords

Table 4-4, Table 4-5, and Table 4-6 contrast the keywords used as modifiers for program elements. We have broken this category down into three topic areas: access modifiers, inheritance-related modifiers, and others.

More Info

For comprehensive coverage of each keyword, refer to the Modifiers section later in this chapter.

Table 4-4. Access Modifier Keyword Comparison

Java

C#

Comments

public

public

Identifies a public program element.

private

private

Identifies a private program element.

protected

protected

Identifies a protected program element. The protected keyword provides different accessibility in C# than Java.

N/A

internal

Identifies an internal program element providing access to all members of the containing assembly.

Table 4-5. Inheritance Modifier Keyword Comparison

Java

C#

Comments

abstract

abstract

Identifies an incomplete implementation of a program element.

final

sealed

Identifies an element that cannot be derived from.

N/A

new

Identifies an element that hides an inherited member.

N/A

override

Identifies an element that overrides an inherited virtual member.

N/A

virtual

Identifies a member that can be overridden by a derived class.

Table 4-6. Other Modifier Keyword Comparison

Java

C#

Comments

final

readonly

Identifies a field that can be assigned to only once.

native

extern

Identifies an externally implemented member.

static

static

Identifies a static program element.

strictfp

N/A

C# has no equivalent for the strictfp keyword.

synchronized

lock

Synchronizes access to a statement block.

transient

N/A

C# does not provide a language equivalent of transient; however, the NonSerialized attribute provides comparable functionality. See Chapter 10 for full details.

volatile

volatile

Ensures synchronized and ordered access to a field.

Flow Control Keywords

Table 4-7 contrasts the Java and C# keywords used to provide conditional and flow control capabilities.

More Info

See the Statements section later in this chapter for a full description of the keywords in Table 4-7.

Table 4-7. Conditional and Flow Control Keyword Comparison

Java

C#

Comments

assert

N/A

C# alternatives are implemented through static members of the Trace and Debug classes in the .NET class libraries.

if

if

Conditional statement.

else

else

Optional component of the if statement.

switch

switch

Conditional statement.

case

case

Component of the switch statement.

default

default

Optional component of the switch statement.

for

for

Determinant loop.

do

do

Postloop conditional.

while

while

Preloop conditional.

N/A

foreach

Collection-specific enumerator.

N/A

in

Part of the foreach statement.

goto

goto

Java reserved keyword (unused). Provides unconditional branching support in C#.

continue

continue

Start a new iteration of a loop.

break

break

Terminate a loop.

return

return

Return from a functional member.

Exception Handling Keywords

Exception handling is predominantly the same in C# and Java, although declaring exceptions is not required in C#. C# adds keywords used to control how integral arithmetic overflow is handled at compile time and run time. Table 4-8 describes the C# keywords related to exception handling.

More Info

For comprehensive coverage of the keywords in Table 4-8, see the Statements section later in this chapter and the Exceptions and Exception Handling section in Chapter 6.

Table 4-8. Exception Handling Keyword Comparison

Java

C#

Comments

try

try

Encloses a block of code statements. If an exception is thrown by one of these statements, program execution will be transferred to the statements enclosed by a suitable catch block or to the statement immediately following the enclosed statement block.

catch

catch

C# also supports general and anonymous catch clauses.

finally

finally

Code to execute regardless of whether a try...catch statement block exits normally or via an exception.

throw

throw

Throw or rethrow an exception.

throws

N/A

Not supported in C#.

N/A

checked

Turns on integral arithmetic overflow checking.

N/A

unchecked

Turns off integral arithmetic overflow checking.

Unmanaged Code Keywords

C# introduces four keywords specifically related to the use of unmanaged code, a feature not available in Java. Table 4-9 summarizes these keywords.

More Info

Refer to the Unsafe Code section in Chapter 6 for comprehensive coverage of the keywords in Table 4-9.

Table 4-9. Unmanaged Code Keywords

Java

C#

Comments

N/A

fixed

Allows a pointer to reference a managed variable, ensuring that the garbage collector does not relocate or deallocate the variable while it’s still referenced.

N/A

sizeof

Gets the size in bytes of the memory occupied by a value type instance. See the section Unsafe Code in Chapter 6 for details.

N/A

stackalloc

Allocates memory blocks on the stack for the storage of local variables.

N/A

unsafe

Identifies unsafe types, members, statements, or code blocks.

Keywords as Identifiers

It’s possible to use keywords as identifiers in C#. The compiler will not interpret an identifier as a keyword if it’s prefixed with the @ symbol. For example:

public class @class {
    public string @public(string @string) {
        string @return = @string + @string;
        return @return;
    }
    public static void Main() {
        System.Console.WriteLine(
            new @class().@public("A silly test"));
    }
}

Because the common language runtime (CLR) is designed to run components developed in different languages, the @ prefix is required to allow methods to be called in components that have used identifiers that are also C# keywords.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.3.208