Introduction to Variables: Accessory Data Types |
|
A string is an empty space, a character, a word, or a group of words that you want the compiler to consider "as is", that is, not to pay too much attention to what the string is made of, unless you explicitly ask it to. This means that, in the strict sense, you can put in a string anything you want. Primarily, the value of a string starts with a double quote and ends with a double-quote. An example of a string is "Welcome to the World of C# Programming!". You can include such a string in Console.Write() to display it on the console. Here is an example: |
using System; class BookClub { static void Main() { Console.WriteLine("Welcome to the World of C# Programming!"); } } This would produce: Welcome to the World of C# Programming! Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the string keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is: string Msg; After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. Here are examples of string variables declared and initialized: string Empty = ""; string Gender = "F"; string FName = "Nelson Mandela"; string Msg = "Welcome to the World of C# Programming! "; After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using the parentheses of Console.Write() or Console.WriteLine(). Here is an example: using System; class BookClub { static void Main() { string Msg = "Welcome to the World of C# Programming! "; Console.WriteLine(Msg); } }
A date is a unit that measures the number of years, months, or days elapsed in a specific period. A time is a unit that counts the number of seconds that have elapsed since midnight of the day considered. Although dates and times are large subjects that would require a detailed study, at this time, we will consider them in simple terms. To declare a variable that would hold date or time values, use the DateTime data type. using System; class NumericRepresentation { static void Main() { DateTime DateHired; } } The .NET Framework sets its starting periodic date to January 1, 0001 at midnight (12:00:00 or 0:00 AM). If not assigned a specific value (in future lessons, we will learn that this is equivalent to declaring a variable using the default constructor), the variable is initialized to 1/1/0001 at midnight. The object data type (it is a class) is used to declare a variable whose type is not primarily defined and can be any of the other data types we have introduced. Here is an example: using System; class Exercise { static void Main() { object Whatever; } } After creating an object variable, you can use its value as you see fit. For example, you can enter the variable in the parentheses of Console.Write() or Console.WriteLine() to display it in the console window.
|
|
||
Previous | Copyright © 2006-2007 FunctionX, Inc. | Next |
|