English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# is an object-oriented programming language. In the object-oriented programming design approach, programs are composed of various interacting objects. Objects of the same type usually have the same type, or are in the same class.
For example, take a Rectangle (rectangle) object as an example. It has length and width properties. According to the design, it may need to accept these property values, calculate the area, and display details.
Let's take a look at the implementation of a Rectangle (rectangle) class and use this opportunity to discuss the basic syntax of C#:
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 result:
Length: 4.5 Width: 3.5 Area: 15.75
The first statement in any C# program is:
using System;
using Keywords are used to include namespaces in a program. A program can contain multiple using statements.
class Keywords are used to declare a class.
Comments are used to explain code. The compiler ignores the entries in comments. In C# programs, multiline comments are marked by /* Starts, and ends with the character */ Terminates, as shown below:
/*This program demonstrates Basic syntax of C# programming language */
Single-line comments are denoted by '//The ' symbol indicates. For example:
//End of the Rectangle class
Variables are the properties or data members of a class, used to store data. In the above program,Rectangle The class has two member variables named length and width.
Member functions are a series of statements that perform specified tasks. Member functions of a class are declared within the class. The class Rectangle that we are using includes three member functions: AcceptDetails,GetArea and Display.
In the above program, the class ExecuteRectangle Contains Main() Methods and instantiation Rectangle Class class.
Identifiers are used to identify classes, variables, functions, or any other user-defined items. In C#, the naming of classes must follow the following basic rules:
Identifiers must start with a letter, underscore, or @, followed by a series of letters, numbers (0) - 9 )、underline (_), @.
The first character of an identifier cannot be a number.
Identifiers must not contain any embedded spaces or symbols, such as ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \.
Identifiers cannot be C# keywords. Unless they have a @ 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 name of a C# library.
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 an @ character as a prefix before the keyword.
In C#, some keywords have special meanings in the context of the code, such as get and set, which are called contextual keywords (contextual keywords).
The following table lists the reserved keywords (Reserved Keywords) and contextual keywords (Contextual Keywords) in C#:
Reserved Keywords | ||||||
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 | |||||
Contextual Keywords | ||||||
add | alias | ascending | descending | dynamic | from | get |
global | group | into | join | let | orderby | partial (type) |
partial (method) | remove | select | set |