Latest web development tutorials

The basic syntax for C #

C # is an object-oriented programming language. In object-oriented programming approach, the program consists of the composition of the various objects interact with each other. The same kinds of objects typically have the same type, or that is in the same class.

For example, Rectangle (rectangular) objects for example. It has a length and width attributes. By design, it may be necessary to accept the values ​​of these properties, calculate the area and show details.

Let's look at the realization of a Rectangle (rectangular) classes, and to discuss the basic C # syntax:

using System;
namespace RectangleApplication
{
    class Rectangle
    {
        // Member variable 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

usingkeywords

At any C # program in the first statement is:

using System;

using keywords for inclusion in the program namespace.A program can contain multiple using statements.

classkeyword

class keyword is used to declare a class.

Comments in C #

Comments are used to interpret the code. The compiler will ignore the comment entry. In C # program, a multi-line comments begin with / *, and the characters * / termination, as follows:

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

Single-line comments with '@' symbol. E.g:

} // End class Rectangle    

Member variables

Variable is a property or data member of a class, for storing data. In the aboveprocedure,Rectangle class has two member variables namedlengthandwidth.

Member function

Function is a series of statements to perform a given task. Class member functions are declared in the class. We illustrate the Rectangle class contains three memberfunctions:AcceptDetails, GetArea andDisplay.

Instantiate a class

In the above program, the classExecuteRectangleis aMain ()method of the class and instance of theRectangleclass contains.

Identifiers

Identifier is used to identify a class, variable, function, or any other user-defined items. In C #, class name must follow these basic rules:

  • The identifier must begin with a letter, it can be followed by a series of letters, numbers (0 - 9) or an underscore (_). Identifiers first character can not be a number.
  • The identifier must not contain any embedded spaces or symbols, such as - + @ #% ^ & * () [] {};:?! " '/ \ However, you can use an underscore (_)...
  • Identifiers can not be C # keywords.

C # Keywords

Keywords are the C # compiler predefined reserved words. These keywords can not be used as identifiers, but if you want to use these keywords as identifiers, in front of the keyword @ character as a prefix.

In C #, and some identifiers have special significance in the context of the code, such as get and set, these are called contextual keyword (contextual keywords).

The following table lists reserved keywords in C # (Reserved Keywords) and contextual keyword (Contextual Keywords):

保留关键字
abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic
modifier)
int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout
(generic
modifier)
overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile
上下文关键字
addaliasascendingdescendingdynamicfromget
globalgroupintojoinletorderbypartial
(type)
partial
(method)
removeselectset