Sri lanka web server - CHAPTER 3 78 C# LANGUAGE FUNDAMENTALS c.InternalMethod();

March 26th, 2008

Apache web server for windows - CHAPTER 3 C# LANGUAGE FUNDAMENTALS 77 Table

March 25th, 2008

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 77 Table 3-4. C# Accessibility Keywords C# Access Modifier Meaning in Life public Marks amember as accessible from an object variable as well as any derived classes. private Marks a method as accessible only by the class that has defined the method. In C#, all members are private by default. protected Marks a method as usable by the defining class, as well as any derived classes. Protected methods, however, are not accessible from an object variable. internal Defines a method that is accessible by any type in the same assembly, but not outside the assembly. protected internal Defines a method whose access is limited to the current assembly or types derived from the defining class in the current assembly. As you may already know, members that are declared public are directly accessible from an object reference via the dot operator (.). Private members cannot be accessed by an object reference, but instead are called internally by the object to help the instance get its work done (i.e., private helper functions). Protected members are only truly useful when you create class hierarchies, which is the subject of Chapter 4. As far as internal or internal protected members are concerned, they are only truly useful when you are creating .NET code libraries (such as a managed *.dll, a topic examined in Chapter 11). To illustrate the implications of these keywords, assume you have created a class (SomeClass) using each of the possible member access modifiers: // Member visibility options. class SomeClass { // Accessible anywhere. public void PublicMethod(){} // Accessible only from SomeClass types. private void PrivateMethod(){} // Accessible from SomeClass and any descendent. protected void ProtectedMethod(){} // Accessible from within the same assembly. internal void InternalMethod(){} // Assembly-protected access. protected internal void ProtectedInternalMethod(){} // Unmarked members are private by default in C#. void SomeMethod(){} } Now assume you have created an instance of SomeClass and attempt to invoke each method using the dot operator: static void Main(string[] args) { // Make an object and attempt to call members. SomeClass c = new SomeClass(); c.PublicMethod();
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Photography web hosting - CHAPTER 3 76 C# LANGUAGE FUNDAMENTALS Figure

March 24th, 2008

CHAPTER 3 76 C# LANGUAGE FUNDAMENTALS Figure 3-7. String format flags in action Console.WriteLine(”N format: {0:N}”, 99999); Console.WriteLine(”X format: {0:X}”, 99999); Console.WriteLine(”x format: {0:x}”, 99999); } Be aware that the use of .NET formatting characters is not limited to console applications. These same flags can be used within the context of the static String.Format() method. This can be helpful when you need to build a string containing numerical values in memory for use in any application type (Windows Forms, ASP.NET, XML web services, and so on): static void Main(string[] args) { … // Use the static String.Format() method to build a new string. string formatStr; formatStr = String.Format(”Don’t you wish you had {0:C} in your account?”, 99989.987); Console.WriteLine(formatStr); } Figure 3-7 shows a test run. Source Code The BasicConsoleIO project is located under the Chapter 3 subdirectory. Establishing Member Visibility Before we go much further, it is important to address the topic of member visibility. Members (methods, fields, constructors, and so on) of a given class or structure must specify their visibility level. If you define a member without specifying an accessibility keyword, it automatically defaults to private. C# offers the method access modifiers shown in Table 3-4.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

CHAPTER 3 C# LANGUAGE FUNDAMENTALS (Web hosting billing) 75 The

March 23rd, 2008

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 75 The first parameter to WriteLine() represents a string literal that contains optional placeholders designated by {0}, {1}, {2}, and so forth (curly bracket numbering always begins with zero). The remaining parameters to WriteLine() are simply the values to be inserted into the respective placeholders (in this case, an int, a double, and a bool). Also be aware that WriteLine() has been overloaded to allow you to specify placeholder values as an array of objects. Thus, you can represent any number of items to be plugged into the format string as follows: // Fill placeholders using an array of objects. object[] stuff = {”Hello”, 20.9, 1, “There”, “83″, 99.99933} ; Console.WriteLine(”The Stuff: {0} , {1} , {2} , {3} , {4} , {5} “, stuff); It is also permissible for a given placeholder to repeat within a given string. For example, if you are a Beatles fan and want to build the string “9, Number 9, Number 9″ you would write // John says… Console.WriteLine(”{0}, Number {0}, Number {0}”, 9); Note If you have a mismatch between the number of uniquely numbered curly-bracket placeholders and fill arguments, you will receive a FormatException exception at runtime. .NET String Formatting Flags If you require more elaborate formatting, each placeholder can optionally contain various format characters (in either uppercase or lowercase), as seen in Table 3-3. Table 3-3. .NET String Format Characters String Format Character Meaning in Life C or c Used to format currency. By default, the flag will prefix the local cultural symbol (a dollar sign [$] for U.S. English). D or d Used to format decimal numbers. This flag may also specify the minimum number of digits used to pad the value. E or e Used for exponential notation. F or f Used for fixed-point formatting. G or g Stands for general. This character can be used to format a number to fixed or exponential format. N or n Used for basic numerical formatting (with commas). X or x Used for hexadecimal formatting. If you use an uppercase X, your hex format will also contain uppercase characters. These format characters are suffixed to a given placeholder value using the colon token (e.g., {0:C}, {1:d}, {2:X}, and so on). Assume you have updated Main() with the following logic: // Now make use of some format tags. static void Main(string[] args) { … Console.WriteLine(”C format: {0:C}”, 99989.987); Console.WriteLine(”D9 format: {0:D9}”, 99999); Console.WriteLine(”E format: {0:E}”, 99999.76543); Console.WriteLine(”F3 format: {0:F3}”, 99999.9999);
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web hosting support - CHAPTER 3 74 C# LANGUAGE FUNDAMENTALS Figure

March 22nd, 2008

CHAPTER 3 74 C# LANGUAGE FUNDAMENTALS Figure 3-5. Basic I/O using System.Console Figure 3-6. Multiple string literal placeholders // Make use of the Console class to perform basic I/O. static void Main(string[] args) { // Echo some stats. Console.Write(”Enter your name: “); string s = Console.ReadLine(); Console.WriteLine(”Hello, {0} “, s); Console.Write(”Enter your age: “); s = Console.ReadLine(); Console.WriteLine(”You are {0} years old”, s); } Formatting Console Output During these first few chapters, you have seen numerous occurrences of the tokens {0}, {1}, and the like embedded within a string literal. .NET introduces a new style of string formatting, slightly reminiscent of the C printf() function, but without the cryptic %d, %s, or %c flags. A simple example follows (see the output in Figure 3-6): static void Main(string[] args) { … int theInt = 90; double theDouble = 9.99; bool theBool = true; // The ‘n’ token in a string literal inserts a newline. Console.WriteLine(”Int is: {0}nDouble is: {1}nBool is: {2}”, theInt, theDouble, theBool); }
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 73 class (Free web design)

March 21st, 2008

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 73 class HelloApp { public static int Main(string[] args) { HelloClass c1 = new HelloClass(”Hey there…”); c1.PrintMessage(); … } } Source Code The HelloClass project is located under the Chapter 3 subdirectory. The System.Console Class Many of the example applications created over the course of these first few chapters make extensive use of the System.Console class. While a console user interface (CUI) is not as enticing as aWindows or web UI, restricting the early examples to a CUI will allow us to keep focused on the concepts under examination, rather than dealing with the complexities of building GUIs. As its name implies, the Console class encapsulates input, output, and error stream manipulations for console-based applications. With the release of .NET 2.0, the Console type has been enhanced with additional functionality. Table 3-2 lists some (but not all) new members of interest. Table 3-2. Select .NET 2.0 Specific Members of System.Console Member Meaning in Life BackgroundColor These properties set the background/foreground colors for the current ForegroundColor output. They may be assigned any member of the ConsoleColor enumeration. BufferHeight These properties control the height/width of the console s buffer area. BufferWidth Clear() This method clears the buffer and console display area. Title This property sets the title of the current console. WindowHeight These properties control the dimensions of the console in relation to WindowWidth the established buffer. WindowTop WindowLeft Basic Input and Output with the Console Class In addition to the members in Table 3-2, the Console type defines a set of methods to capture input and output, all of which are defined as static and are therefore called at the class level. As you have seen, WriteLine() pumps a text string (including a carriage return) to the output stream. The Write() method pumps text to the output stream without a carriage return. ReadLine() allows you to receive information from the input stream up until the carriage return, while Read() is used to capture a single character from the input stream. To illustrate basic I/O using the Console class, consider the following Main() method, which prompts the user for some bits of information and echoes each item to the standard output stream. Figure 3-5 shows a test run.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Web design company - CHAPTER 3 72 C# LANGUAGE FUNDAMENTALS Is

March 20th, 2008

CHAPTER 3 72 C# LANGUAGE FUNDAMENTALS Is That aMemory Leak? If you have a background in C++, you may be alarmed by the previous code samples. Specifically, notice how the Main() method of the previous HelloClass type has no logic that explicitly destroys the c1 and c2 references. This is not a horrible omission, but the way of .NET. Like Visual Basic and Java developers, C# programmers never explicitly destroy a managed object. The .NET garbage collector frees the allocated memory automatically, and therefore C# does not support a delete keyword. Chapter 5 examines the garbage collection process in more detail. Until then, just remember that the .NET runtime environment automatically destroys the managed objects you allocate. Defining an Application Object Currently, the HelloClass type has been constructed to perform two duties. First, the class defines the entry point of the application (the Main() method). Second, HelloClass maintains a point of field data and a few constructors. While this is all well and good, it may seem a bit strange (although syntactically well-formed) that the static Main() method creates an instance of the very class in which it was defined: class HelloClass { … public static int Main(string[] args) { HelloClass c1 = new HelloClass(); … } } Many of my initial examples take this approach, just to keep focused on illustrating the task at hand. However, amore natural design would be to refactor the current HelloClass type into two distinct classes: HelloClass and HelloApp. When you build C# applications, it becomes quite common to have one type functioning as the application object (the type that defines the Main() method) and numerous other types that constitute the application at large. In OO parlance, this is termed the separation of concerns. In a nutshell, this design principle states that a class should be responsible for the least amount of work. Thus, we could reengineer the current program into the following (notice that a new member named PrintMessage() has been added to the HelloClass type): class HelloClass { public string userMessage; public HelloClass() { Console.WriteLine(”Default ctor called!”); } public HelloClass(string msg) { Console.WriteLine(”Custom ctor called!”); userMessage = msg; } public void PrintMessage() { Console.WriteLine(”Message is: {0}”, userMessage); } }
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web design portfolio - CHAPTER 3 C# LANGUAGE FUNDAMENTALS 71 Figure

March 19th, 2008

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 71 Figure 3-4. Simple constructor logic // Default constructor. public HelloClass() { Console.WriteLine(”Default ctor called!”); } // This custom constructor assigns state data // to a user-supplied value. public HelloClass (string msg) { Console.WriteLine(”Custom ctor called!”); userMessage = msg; } // Program entry point. public static int Main(string[] args) { // Call default constructor. HelloClass c1 = new HelloClass(); Console.WriteLine(”Value of userMessage: {0}n”, c1.userMessage); // Call parameterized constructor. HelloClass c2; c2 = new HelloClass(”Testing, 1, 2, 3″); Console.WriteLine(”Value of userMessage: {0}”, c2.userMessage); Console.ReadLine(); return 0; } } Note Technically speaking, when a type defines identically named members (including constructors) that differ only in the number of or type of parameters, the member in question is overloaded. Chapter 4 examines member overloading in detail. On examining the program s output, you can see that the default constructor has assigned the string field to its default value (empty), while the custom constructor has assigned the member data to the user-supplied value (see Figure 3-4). Note As soon as you define a custom constructor for a class type, the free default constructor is removed. If you wish to allow your object users to create an instance of your type using the default constructor, you will need to explicitly redefine it as in the preceding example.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Business web site - CHAPTER 3 70 C# LANGUAGE FUNDAMENTALS using

March 18th, 2008

CHAPTER 3 70 C# LANGUAGE FUNDAMENTALS using System; class HelloClass { public static int Main(string[] args) { // Error! Use of unassigned local variable! Must use ‘new’. HelloClass c1; c1.SomeMethod(); … } } To illustrate the proper procedures for object creation, observe the following update: using System; class HelloClass { public static int Main(string[] args) { // You can declare and create a new object in a single line… HelloClass c1 = new HelloClass(); // …or break declaration and creation into two lines. HelloClass c2; c2 = new HelloClass(); … } } The new keyword is in charge of calculating the correct number of bytes for the specified object and acquiring sufficient memory from the managed heap. Here, you have allocated two objects of the HelloClass class type. Understand that C# object variables are really a reference to the object in memory, not the actual object itself. Thus, in this light, c1 and c2 each reference a unique HelloClass object allocated on the managed heap. The Role of Constructors The previous HelloClass objects have been constructed using the default constructor, which by definition never takes arguments. Every C# class is automatically provided with a free default constructor, which you may redefine if need be. The default constructor ensures that all member data is set to an appropriate default value (this behavior is true for all constructors). Contrast this to C++, where uninitialized state data points to garbage (sometimes the little things mean a lot). Typically, classes provide additional constructors beyond the default. In doing so, you provide the object user with a simple way to initialize the state of an object at the time of creation. Like in Java and C++, in C# constructors are named identically to the class they are constructing, and they never provide a return value (not even void). Here is the HelloClass type once again, with a custom constructor, a redefined default constructor, and a point of public string data: // HelloClass, with constructors. using System; class HelloClass { // A point of state data. public string userMessage;
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

CHAPTER 3 C# LANGUAGE FUNDAMENTALS (Web hosting billing) 69 //

March 17th, 2008

CHAPTER 3 C# LANGUAGE FUNDAMENTALS 69 // List the drives on this machine. string[] drives = Environment.GetLogicalDrives(); for(int i = 0; i < drives.Length; i++) Console.WriteLine("Drive {0} : {1} ", i, drives[i]); // Which version of the .NET platform is running this app? Console.WriteLine("Executing version of .NET: {0} ", Environment.Version); ... } Possible output can be seen in Figure 3-3. The System.Environment type defines members other than those presented in the previous example. Table 3-1 documents some additional properties of interest; however, be sure to check out the .NET Framework 2.0 SDK documentation for full details. Table 3-1. Select Properties of System.Environment Property Meaning in Life MachineName Gets the name of the current machine NewLine Gets the newline symbol for the current environment ProcessorCount Returns the number of processors on the current machine SystemDirectory Returns the full path to the system directory UserName Returns the name of the entity that started this application Defining Classes and Creating Objects Now that you have the role of Main() under your belt, let s move on to the topic of object construction. All object-oriented (OO) languages make a clear distinction between classes and objects. A class is a definition (or, if you will, a blueprint) for a user-defined type (UDT). An object is simply a term describing a given instance of a particular class in memory. In C#, the new keyword is the de facto way to create an object. Unlike other OO languages (such as C++), it is not possible to allocate a class type on the stack; therefore, if you attempt to use a class variable that has not been new-ed, you are issued a compile-time error. Thus the following C# code is illegal: Figure 3-3. Various environment variables at work
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.