Home |  JournalSeek |  SoftwareSeek |  GenomeSeek |  Expression |  Developer |  Research
Return to Genamics Home

15. Attributes

Both C# and Java contain information such as the access level of a field in the compiled code. C# generalizes this ability, so you can compile custom information about just about any element of code such as a class, method, field and even individual parameters. This information can be retrieved at run-time. Here is a very simple example of a class which makes use of attributes:

[AuthorAttribute ("Ben Albahari")]
class A
{
    [Localizable(true)]
    public String Text {
        get {return text;
        }
        ...
    }
}

Java uses a combination of /** */ and comments to include additional information about classes and methods, but this information (apart from the ) is not built into the byte code. C# uses the pre-defined attributes ObsoleteAttribute so the compiler can warn you against obsolete code (like ), and the ConditionalAttribute to enable conditional compilation. Microsoft's new XML libraries utilize attributes to express how fields should be serialized into XML, which means you can easily turn a class into XML and then reconstruct it again. Another apt use for attributes is for the creation of a really powerful class browsing tool. The C# Language Reference explains exactly how to create and use attributes.

16. Selection Statements

C# lets you govern a switch statement with an integral type, char, enum or (unlike C++ or Java) a string. In Java and C++ if you leave out a break after each case statement you risk having another case statement being executed. I have no idea why this rarely needed error-prone behavior was made the default behavior in Java and C++, and I'm glad to see C# doesn't do this.

17. Predefined Types

The C# primitive types are basically the same as the ones in Java except they add unsigned types to. We've got sbyte, byte, short, ushort, int, uint, long, ulong, char, float & double. The only surprise here is the 12-byte "decimal" floating point number which can take advantage of the latest processors.

18. Field Modifiers

Again, the field modifiers are basically the same as the ones in Java. To express fields which can't be modified, C# uses the const and readonly modifiers. A const field modifier is like the Java final field modifier, and compiles so that the actually value is part of the IL code. A readonly modifier compiles so that at run-time the value is evaluated. This allows an upgrade, say to one one of the standard C# libraries, without breaking your deployed code.

19. Jump Statements

Not many surprises here, apart from perhaps the infamous goto. However, it's really a distant relative of the evil goto statement we remember from basic 20 years ago. A goto statement must point to a label or one of the options in a switch statement. The first usage of pointing to a label is similar to the usage of a continue : label statement in Java, except with a little more freedom. Such a goto statement may point anywhere within its scope, which restricts it to the same method, or finally block if it is declared within one. It may not jump into a loop statement which it is not within, and it cannot leave a try block before the enclosing finally block(s) are executed. The continue statement in C# is equivalent to the continue statement in Java, except it can't point to a label.


Previous     Next


C# 3.0 in a Nutshell (April 2007) - by Joseph Albahari and Ben Albahari.









Add To Favorites
Email This Page

C# Comparative Contents
  1. C# and Java
  2. Properties
  3. Indexers
  4. Delegates
  5. Events
  6. Enums
  7. Collections and the Foreach Statement
  8. Structs
  9. Type Unification
  10. Operator Overloading
  11. Polymorphism
  12. Interfaces
  13. Versioning
  14. Parameter Modifiers
  15. Attributes
  16. Selection Statements
  17. Predefined Types
  18. Field Modifiers
  19. Jump Statements
  20. Assemblies, Namespaces & Access Levels
  21. Pointer Arithmetic
  22. Rectangular Arrays
  23. Constructors and Destructors
  24. Managed Execution Environments
  25. Libraries
  26. Interoperability
  27. Conclusion


Side Panel
Privacy Policy About Us Contact Us