1.4. C# basic syntax

发布时间 : 2025-10-25 13:34:36 UTC      

Page Views: 9 views

C# is an object-oriented programming language. In the object-oriented programming method, the program consists of a variety of objects that interact with each other. Objects of the same kind usually have the same type, that is to say, in the same class medium.

For example, take a Rectangle object as an example. It has length and width Property. Depending on the design, it may need to accept these property values, calculate the area, and display details.

Let’s look at the implementation of a Rectangle class and discuss basic syntax of C# :

1.4.1. Example #

using System; namespace RectangleApplication { class Rectangle { // Member variables double length; double width; public void Acceptdetails() { length = 4.5; width = 3.5; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } } 

When the above code is compiled and executed, it produces the following results:

Length: 4.5 Width: 3.5 Area: 15.75 

using Keyword #

The first statement in any C# program is:

using System; 

using keyword is used to include namespaces in the program. A program can contain multiple using statement.

class keyword #

class keyword is used to declare a class

C# Comments #

Comments are used to interpret code. The compiler ignores annotated entries.In a C# program, multiple lines are commented to /* start with a character */ to terminate, as follows:

/* This program demonstrates The basic syntax of C# programming Language */ 

Single-line comments are made with // . For example:

} //end class Rectangle 

Member variable #

Variables are properties or data members of a class that are used to store data. In the above program Rectangle class has two member variables named length and width .

Member function #

A function is a series of statements that perform a specified task. The member functions of a class are declared within the class. Our example classRectangle contains three member functions: AcceptDetails GetArea and Display .

Instantiate a class #

In the above program, the class ExecuteRectangle is a containing Main() methods and instancing Rectangle class.

Identifier #

Identifiers are used to identify classes, variables, functions, or any other user-defined item. In C#, class naming must follow the following basic rules:

  • Identifiers must be alphabetic, underscore, or @ , the beginning can be followed by a series of letters, numbers (0-9), an underscore ( _ ), @ .

  • The first character in an identifier cannot be a number.

  • Identifiers must not contain any embedded spaces or symbols, such as ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \ .

  • The identifier cannot be a C# keyword. Unless they have one @ prefix. For example, @if is a valid identifier, but if is not, because if is a keyword.

  • Identifiers must be case sensitive. Uppercase and lowercase letters are considered different letters.

  • Cannot be the same as the class library name of C#.

C# keyword #

Keywords are reserved words predefined by the C# compiler. These keywords cannot be used as identifiers, but if you want to use these keywords as identifiers, you can add @ characters as prefixes.

In C#, some keywords have a special meaning in the context of the code, such as get and set , which are called context keywords (contextual keywords)

The following table lists the reserved keywords and context keywords in C#:

Reserved keyword #

Abstract

As

Base

Bool

Break

Byte

Case

Catch

Char

Checked

Class

Const

Continue

Decimal

Default

Delegate

Do

Double

Else

Enum

Event

Explicit

Extern

FALSE

Finally

Fixed

Float

For

Foreach

Goto

If

Implicit

In

In (generic modifier)

Int

Interface

Internal

Is

Lock

Long

Namespace

New

Null

Object

Operator

Out

Out (generic modifier)

Override

Params

Private

Protected

Public

readonly

Ref

Return

Sbyte

Sealed

Short

Sizeof

Stackalloc

Static

string

Struct

switch

This

Throw

TRUE

Try

typeof

Uint

Ulong

Unchecked

Unsafe

Ushort

Using

virtual

Void

Volatile

While

Context keyword #

Add

Alias

Ascending

Descending

Dynamic

From

Get

Global

Group

Into

Join

let

Orderby

Partial (type)

Partial

Remove

Select

Set

(method)

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。