You declare variables like this in C# (the syntax we'll use is standard for C# documentationitems in square brackets are optional, and items in italics are placeholders for your own identifiers):

 [  attributes  ] [  modifiers  ]  type declarators  ; 

Here are the parts of this statement:

  • attributes (optional) Holds optional declarative meta-information , as we'll discuss in Chapter 14, "Using Attributes and Reflection."

  • modifiers (optional) Optional modifiers that include the new modifier and one of the four access modifiers like public and private that we'll see in Chapter 3.

  • type One of the C# types byte , char , short , int , long , float , double , decimal , bool , string , an enum or a reference type.

  • declarators A comma-separated list of declarators. A declarator takes the form identifier [= constant-expression ] .

For example, to declare an integer variable named temperature , you can use the statement int temperature; and then assign it a value of 32 like this:

 int temperature; temperature = 32; System.Console.WriteLine("The temperature is {0}.", temperature); 

You can also initialize variables when you declare them, as in this case, where we're declaring a string variable and assigning it a value in the same step:

 string greeting = "Hello from C#."; System.Console.WriteLine(greeting); 

When you assign literals to variables, C# ensures that the type matches. That means statements like these work to declare variables:

 int temperature = 32; double data = 1.53322; 

However, the default type for floating point literals is double , so statements like these seem like errors to the compiler, because you're assigning what it thinks is a double value to decimal and float variables:

 decimal dough = 300.5; float data = 1.53322; 

To set the type of these literals, you can use the m (or M ) suffix for decimal literals and the f (or F ) suffix for float literals, like this:

 decimal dough = 300.5m; float data = 1.53322f; 

You can assign values of true or false to Boolean variables this way:

 bool day = true; bool night = false; 

Now that you have a grip on working with variables, it's time to use the Console.ReadLine method to read text, store it, and display it, as you see in Listing 1.2. In ch01_02.cs, the Console.ReadLine method returns the text typed by the user , which we store in a variable named text and then display that text using Console.WriteLine . That's all there is to it.

Listing 1.2 Reading Text and Displaying it (ch01_02.cs)
 class ch01_02 {   static void Main()   {     string text;     System.Console.Write("Type some text: ");     text = System.Console.ReadLine();     System.Console.WriteLine("You typed {0}", text);   } } 

Here's what you see when you run ch01_02 and type Hello C#! :

 C:\>ch01_02 Type some text: Hello C#! You typed Hello C#! 

C# uses definite assignment , which means that you must initialize a variable before using it (there are a few exceptions to this rule, as you'll see when creating methods ). That means that this code will not compile:

 int value; System.Console.WriteLine(value); 

There's no official recommendation on how to name variables, but you'll usually see "Camel" notation used for variables (Microsoft no longer recommends its earlier "Hungarian" notation); in this notation, you can make up a variable name of several words, each of which begins with a leading capital letter except the first, like this: dayOfTheMonth , monthOfTheYear , and so on. For all other identifiers, you usually use Pascal notation, where all the words that make up an identifier begin with a leading capital letter, such as the methods CalculateCompoundInterest , WriteLine , ConnectToInternet , and so on.


In C#, you must initialize a variable before using it (although an exception is made for "out" parameters used in methods, as we'll see in the next chapter).

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: