Exercise 3-5 (Storage, static method)

Chapter_3     Exercises_3-3,4     Autoboxing Exercise_3-6







Exercise 3-5     TCS, p. 85


Exercise 3-5. Write a program that includes and calls the Storage() method defined as a code fragment in this chapter.




Storage.cs     TCS, p. 59


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
*/




Note:  s.Length in storage() is a property of class String.









Chapter_3     Exercises_3-3,4     Autoboxing BACK_TO_TOP Exercise_3-6



Comments

Popular posts from this blog

Contents