using System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] class RemarkAttribute : Attribute { string remarkValue; public RemarkAttribute(string comment) { remarkValue = comment; } public string remark { get { return remarkValue; } } } [RemarkAttribute("This class uses an attribute.")] class UseAttrib { // ... } public class AttribDemo { public static void Main() { Type t = typeof(UseAttrib); Console.Write("Attributes in " + t.Name + ": "); object[] attribs = t.GetCustomAttributes(false); foreach(object o in attribs) { Console.WriteLine(o); } Console.Write("Remark: "); // Retrieve the RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.WriteLine(ra.remark); } }
Language Basics
Subclass System.Attribute
using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class AuthorAttribute : System.Attribute { private string company; private string name; public AuthorAttribute(string name) { this.name = name; company = ""; } public string Company { get { return company; } set { company = value; } } public string Name { get { return name; } } } [assembly: Author("Tom", Company = "Ltd.")] [Author("Tom", Company = "Abc Ltd.")] class SomeClass { } [Author("Lena")] public class SomeOtherClass { } [Author("FirstName")] [Author("Jack", Company = "Ltd.")] class MainClass { public static void Main() { Type type = typeof(MainClass); object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true); foreach (AuthorAttribute a in attrs) { Console.WriteLine(a.Name + ", " + a.Company); } } }
The @ prefix turns off the processing of escape characters.
using System; using System.Text; class StringApp { static void Main(string[] args) { string finalString = @" String file: 'C:MyAppStrings'"; Console.WriteLine(finalString); } }
White space is preserved with verbatim strings.
using System; using System.Text; class StringApp { static void Main(string[] args) { string myLongString = @"This is a very very very long string"; Console.WriteLine(myLongString); } }
Demonstrate an @ identier
// Demonstrate an @ identier.
using System;
public class IdTest {
static void Main() {
int @if; // use if as an identifier
for(@if = 0; @if < 10; @if++) Console.WriteLine("@if is " + @if); } } [/csharp]
Using the as Keyword to Work with an Interface
using System; public interface IPrintMessage { void Print(); }; class Class1 { public void Print() { Console.WriteLine("Hello from Class1!"); } } class Class2 : IPrintMessage { public void Print() { Console.WriteLine("Hello from Class2!"); } } class MainClass { public static void Main() { PrintClass PrintObject = new PrintClass(); PrintObject.PrintMessages(); } } class PrintClass { public void PrintMessages() { Class1 Object1 = new Class1(); Class2 Object2 = new Class2(); PrintMessageFromObject(Object1); PrintMessageFromObject(Object2); } private void PrintMessageFromObject(object obj) { IPrintMessage PrintMessage; PrintMessage = obj as IPrintMessage; if(PrintMessage != null) PrintMessage.Print(); } }
as operator
using System; class Employee { } class ContractEmployee : Employee { } class CastExample5 { public static void Main () { Employee e = new Employee(); Console.WriteLine("e = {0}", e == null ? "null" : e.ToString()); ContractEmployee c = e as ContractEmployee; Console.WriteLine("c = {0}", c == null ? "null" : c.ToString()); } }