X hits on this document

1066 views

0 shares

0 downloads

0 comments

31 / 396

C# LANGUAGE SPECIFICATION

The following example declares an abstract class, Expression, which represents an expression tree node, and three derived classes, Constant, VariableReference, and Operation, which implement expression tree nodes for constants, variable references, and arithmetic operations.

using System; using System.Collections;

public abstract class Expression { public abstract double Evaluate(Hashtable vars); }

public class Constant: Expression { double value;

public Constant(double value) { this.value = value; }

public override double Evaluate(Hashtable vars) { return value; } }

public class VariableReference: Expression { string name;

public VariableReference(string name) { this.name = name; }

public override double Evaluate(Hashtable vars) { object value = vars[name]; if (value == null) { throw new Exception("Unknown variable: " + name); } return Convert.ToDouble(value); } }

public class Operation: Expression { Expression left; char op; Expression right;

public Operation(Expression left, char op, Expression right) { this.left = left; this.op = op; this.right = right; }

18Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1066
Page views1066
Page last viewedSat Dec 10 19:39:06 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments