// Copyright (C) 2001 StructureByDesign. All Rights Reserved.
1
2 // CLASS1.CS -- Syntax-at-a-Glance for the C
3 // A quick code reference for programmers who work in many languages.
4 // Executable code, minimal comments document the essence of the language.
5
6
7 using System;
8 using System.Collections;
9 using System.IO;
10
11 namespace StructureByDesign.Syntax
12 {
13 public class Class1: Object
14 {
15 public static int Main(string[] args) // Entry point.
16 {
17 System.Console.WriteLine("Hello");
18 Class2 aclass2 = new Class2();
19 aclass2.run();
20 return 0;
21 }
22 }
23
24 interface Interface1
25 {
26 void run();
27 }
28
29 class Class2: Class1, Interface1
30 {
31 public const int CONSTANT = 1; // Access not restricted, implicitly static.
32 private int m_intPrivateField; // Access limited to containing type.
33 public Class2() : base() // Constructor.
34 {
35 initialize();
36 }
37 protected void initialize() // Object initialization.
38 { // Access limited to containing class or types derived.
39 Number = 1;
40 }
41 protected int Number // Language property feature.
42 {
43 get
44 {
45 return m_intPrivateField;
46 }
47 set
48 {
49 m_intPrivateField = value; // Implicit parameter.
50 }
51 }
52 public void run()
53 {
54 anonymousCode();
55 arrays();
56 collections();
57 comparison();
58 control();
59 filesStreamsAndExceptions();
60 numbersAndMath();
61 primitivesAndConstants();
62 runtimeTyping();
63 strings();
64 }
65 void anonymousCode()
66 {
67 Delegate adelegate = new Delegate(Run);
68 adelegate();
69 }
70 delegate void Delegate();
71 void Run()
72 {
73 Console.WriteLine("Run");
74 }
75 void arrays()
76 {
77 int[] arrayOfInts = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
78 arrayOfInts[0] = 9;
79 assert(arrayOfInts[0] == arrayOfInts[9]);
80
81 String[] arrayOfStrings = new String[10];
82 assert(arrayOfStrings[0] == null);
83 assert(arrayOfStrings.Length == 10);
84
85 arrayOfStrings = new String[] { "one", "two" };
86
87 byte[,] arrayOfBytes = { {0,0,0},
88 {0,1,2},
89 {0,2,4}};
90 assert(arrayOfBytes[2,2] == 4);
91 }
92 void collections()
93 {
94 IList ailist = new ArrayList();
95 ailist.Add("zero"); ailist.Add("one"); ailist.Add("three");
96 ailist[2] = "two";
97 assert(ailist[2].Equals("two"));
98 ailist.Remove("two");
99 ((ArrayList)ailist).Sort();
100 for(IEnumerator aie = ((ArrayList)ailist).GetEnumerator(); aie.MoveNext(); )
101 ;
102 foreach(String astring in ailist)
103 ;
104
105 IDictionary aidictionary = new Hashtable();
106 aidictionary.Add("key", "value");
107 assert(aidictionary["key"].Equals("value"));
108
109 // Set not available.
110 }
111 void comparison()
112 {
113 int aint1 = 1;
114 int aint2 = 2;
115 int aint = 1;
116 String astring1 = "one";
117 String astring2 = "two";
118 String astring = astring1;
119
120 assert(aint == aint1);
121 assert(aint1 != aint2);
122 assert(astring == astring1);
123 assert(astring1 == String.Copy("one")); // For strings == is overloaded to compare values.
124 assert(!astring1.Equals(astring2));
125 assert(astring1.Equals(String.Copy("one")));
126
127 astring = null;
128 if (astring != null && astring.Length > 0) // Conditional evaluation.
129 assert(false);
130
131 if (aint2 < 0 || 1 < aint2)
132 assert(true);
133 }
134 void control()
135 {
136 if (true)
137 assert(true);
138 else
139 assert(false);
140 /////
141 switch ('b') {
142 case 'a':
143 assert(false);
144 break;
145 case 'b':
146 assert(true);
147 break;
148 default:
149 assert(false);
150 break;
151 }
152 /////
153 for (int ai1 = 0; ai1 < 10; ai1++)
154 assert(true);
155 /////
156 int ai = 0;
157 while (ai < 10) {
158 assert(true);
159 ai++;
160 }
161
162 do
163 ai--;
164 while (ai > 0);
165
166 for (int x = 0; x < 10; x++) // Labeled break/continue not available.
167 for (int y = 0; y < 10; y++)
168 if (x == 9)
169 break;
170 else
171 continue;
172 }
173 void filesStreamsAndExceptions()
174 {
175 FileInfo afileinfo = new FileInfo("list.txt");
176 try {
177 StreamWriter asw = new StreamWriter("list.txt");
178 asw.WriteLine("line");
179 asw.WriteLine("line");
180 asw.Close();
181
182 assert(afileinfo.Exists);
183
184 StreamReader asr = new StreamReader("list.txt");
185 String astringLine;
186 while ((astringLine = asr.ReadLine()) != null)
187 assert(astringLine.Equals("line"));
188 asr.Close();
189 } catch (IOException aexception) {
190 System.Console.WriteLine(aexception.Message);
191 throw new NotSupportedException();
192 }
193 finally {
194 afileinfo.Delete();
195 }
196 }
197 void numbersAndMath()
198 {
199 assert(Int32.Parse("123") == 123);
200 assert(123.ToString().Equals("123"));
201
202 assert(Math.PI.ToString("n3").Equals("3.142"));
203
204 assert(Int32.MaxValue < Int64.MaxValue);
205
206 assert(Math.Abs(Math.Sin(0) - 0) <= Double.Epsilon);
207 assert(Math.Abs(Math.Cos(0) - 1) <= Double.Epsilon);
208 assert(Math.Abs(Math.Tan(0) - 0) <= Double.Epsilon);
209
210 assert(Math.Abs(Math.Sqrt(4) - 2) <= Double.Epsilon);
211 assert(Math.Abs(Math.Pow(3,3) - 27) <= Double.Epsilon);
212
213 assert(Math.Max(0,1) == 1);
214 assert(Math.Min(0,1) == 0);
215
216 assert(Math.Abs(Math.Ceiling(9.87) - 10.0) <= Double.Epsilon);
217 assert(Math.Abs(Math.Floor(9.87) - 9.0) <= Double.Epsilon);
218 assert(Math.Round(9.87) == 10);
219
220 Random arandom = new Random();
221 double adouble = arandom.NextDouble();
222 assert(0.0 <= adouble && adouble < 1.0);
223 int aint = arandom.Next(10);
224 assert(0 <= aint && aint < 10);
225 }
226 enum Season: byte { Spring=0, Summer, Fall, Winter };
227
228 void primitivesAndConstants()
229 {
230 bool abool = false;
231 char achar = 'A'; // 16 bits, Unicode
232
233 byte abyte = 0x0; // 8 bits, unsigned, hex constant
234 sbyte asbyte = 0; // 8 bits, signed
235
236 short ashort = 0; // 16 bits, signed
237 ushort aushort = 0; // 16 bits, unsigned
238
239 int aint = 0; // 32 bits, signed
240 uint aunit = 0; // 32 bits, unsigned
241
242 long along = 0L; // 64 bits, signed
243 ulong aulong = 0; // 64 bits, unsigned
244
245 float afloat = 0.0F; // 32 bits
246 double adouble = 0.0; // 64 bits
247
248 decimal adecimal = 0; // 128 bits, financial calculations
249
250 Season aseason = Season.Fall;
251 assert((byte)aseason == 2);
252 }
253 void runtimeTyping()
254 {
255 assert(new int[] { 1 } is int[]);
256 assert(new ArrayList() is ArrayList);
257
258 assert((new ArrayList()).GetType() == typeof(ArrayList));
259 assert(typeof(Int32) is Type); // Type of primitive type.
260
261 assert(Type.GetType("System.Collections.ArrayList") == typeof(ArrayList));
262 }
263 void strings()
264 {
265 String astring1 = "one";
266 String astring2 = "TWO";
267
268 assert((astring1 + "/" + astring2).Equals("one/TWO"));
269 assert(astring2.ToLower().Equals("two")); // Equals ignoring case not available.
270 assert(astring1.Length == 3);
271 assert(astring1.Substring(0,2).Equals("on"));
272 assert(astring1[2] == 'e');
273 assert(astring1.ToUpper().Equals("ONE"));
274 assert(astring2.ToLower().Equals("two"));
275 assert(astring1.CompareTo("p") < 0);
276 assert(astring1.IndexOf('e') == 2);
277 assert(astring1.IndexOf("ne") == 1);
278 assert(astring1.Trim().Length == astring1.Length);
279
280 assert(Char.IsDigit('1'));
281 assert(Char.IsLetter('a'));
282 assert(Char.IsWhiteSpace('\t'));
283 assert(Char.ToLower('A') == 'a');
284 assert(Char.ToUpper('a') == 'A');
285 }
286 private void assert(bool abool)
287 {
288 if (!abool)
289 throw new Exception("assert failed");
290 }
291 }
292 }