C# is a case-sensitive language that is highly expressive yet simple to learn and use. The following sections describe the various syntax of the language.

Keywords

In any programming language, there is always a list of identifiers that have special meanings to the compiler. These identifiers are known as keywords, and you should not use them as identifiers in your program.

Here's the list of keywords in C# 2008:

abstract event     new       struct

as       explicit  null      switch

base     extern    object    this

bool     false     operator  throw

break    finally   out       true

byte     fixed     override  try

case     float     params    typeof

catch    for       private   uint

char     foreach   protected ulong

checked  goto      public    unchecked

class    if        readonly  unsafe

const    implicit  ref       ushort

continue in        return    using

decimal  int       sbyte     virtual

default  interface sealed    volatile

delegate internal  short     void

do       is        sizeof    while

double   lock      stackalloc

else     long      static

enum     namespace string

Variables

In C#, you declare variables using the following format:

datatype identifier;

The following example declares and uses four variables:

class Program {

 static void Main(string[] args) {

  //---declare the variables---

  int num1;

  int num2 = 5;

  float num3, num4;

  //---assign values to the variables---

  num1 = 4;

  num3 = num4 = 6.2f;

  //---print out the values of the variables---

  Console.WriteLine('{0} {1} {2} {3}', num1, num2, num3, num4);

  Console.ReadLine();

  return;

 }

}

Note the following:

□ num1 is declared as an int (integer).

□ num2 is declared as an int and assigned a value at the same time.

□ num3 and num4 are declared as float (floating point number)

□ You need to declare a variable before you can use it. If not, C3 compiler will flag that as an error.

□ You can assign multiple variables in the same statement, as is shown in the assignment of num3 and num4.

This example will print out the following output:

4 5 6.2 6.2

The following declaration is also allowed:

//---declares both num5 and num6 to be float

// and assigns 3.4 to num5---

float num5 = 3.4f, num6;

But this one is not allowed:

//---cannot mix different types in a declaration statement---

int num7, float num8;

The name of the variable cannot be one of the C# keywords. If you absolutely must use one of the keywords as a variable name, you need to prefix it with the @ character, as the following example shows:

int @new = 4;

Console.WriteLine(@new);

Scope of Variables

The scope of a variable (that is, its visibility and accessibility) that you declare in C# is affected by the location in which the variable is declared. Consider the following example where a variable num is declared within the Program class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace HelloWorld {

 class Program {

  static int num = 7;

  static void Main(string[] args) {

   Console.WriteLine('num in Main() is {0}', num); //---7---

   HelloWorld.Program.Method1();

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату