Exercise 3-5. Write a program that includes and calls the Storage() method defined as a code fragment in this chapter.
public class Storage
{
static int storage (string s)
{
return s.Length * 2;
}
public static void Main()
{
string s = "Double the length of this text is: "; // 35 chars long
// access static field in a static context, Main():
System.Console.WriteLine(s + storage(s));
System.Console.WriteLine(s + Storage.storage(s));
// System.Console.WriteLine(s + new Storage().storage(s)); // compile error
}
}
/*
mcs Storage.cs
mono Storage.exe
Double the length of this text is: 70
Double the length of this text is: 70
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-9. Turn the StaticFun code fragments into a working program.
public class Increment
{
public class StaticTest // inner class
{
public static int i = 47;
}
public class StaticFun // inner class
{
public static void Increment()
{ // access static field in a static context:
StaticTest.i++;
}
}
public static void Main()
{
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
// System.Console.WriteLine("st1.i = " + st1.i); // compile errors
// System.Console.WriteLine("st2.i = " + st2.i);
System.Console.WriteLine("StaticTest.i = " + StaticTest.i);
StaticFun sf = new StaticFun();
// sf.Increment(); // compile error
StaticFun.Increment();
System.Console.WriteLine("StaticTest.i = " + StaticTest.i);
// new StaticFun().Increment(); // compile error
StaticTest.i++; // access static field in a static context, Main()
System.Console.WriteLine("StaticTest.i = " + StaticTest.i);
// new StaticTest().i++; // compile error
}
}
/*
mcs Increment.cs
// 3 warnings: st1, st2, sf not used
mono Increment.exe
StaticTest.i = 47
StaticTest.i = 48
StaticTest.i = 49
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
public class StaticField
{
static int i; // uninitialized
public static void Main()
{
// access static field from a static context, Main():
System.Console.WriteLine("i = " + i);
System.Console.WriteLine("StaticField.i = " + StaticField.i);
StaticField sf1 = new StaticField();
StaticField sf2 = new StaticField();
// System.Console.WriteLine("sf1.i = " + sf1.i); // compile errors
// System.Console.WriteLine("sf2.i = " + sf2.i);
// System.Console.WriteLine("new StaticField().i = " + new StaticField().i);
i = 1;
System.Console.WriteLine("i = " + i);
System.Console.WriteLine("StaticField.i = " + StaticField.i);
StaticField.i = 2;
System.Console.WriteLine("i = " + i);
System.Console.WriteLine("StaticField.i = " + StaticField.i);
}
}
/*
mcs StaticField.cs
// 2 warnings: sf1, sf2 not used
mono StaticField.exe
i = 0
StaticField.i = 0
i = 1
StaticField.i = 1
i = 2
StaticField.i = 2
*/