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 When the above code is compiled and executed, it produces the following results: The first statement in any C# program is: Comments are used to interpret code. The compiler ignores annotated entries.In a C# program, multiple lines are commented to Single-line comments are made with Variables are properties or data members of a class that are used to store data. In the above program 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: In the above program, the class 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 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 Identifiers must be case sensitive. Uppercase and lowercase letters are considered different letters. Cannot be the same as the class library name of C#. 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 In C#, some keywords have a special meaning in the context of the code, such as The following table lists the reserved keywords and context keywords in C#: 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 Add Alias Ascending Descending Dynamic From Get Global Group Into Join let Orderby Partial (type) Partial Remove Select Set (method)
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();
}
}
}
Length: 4.5
Width: 3.5
Area: 15.75
using Keyword #
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 classC# Comments #
/*
start with a character
*/
to terminate, as follows:/* This program demonstrates
The basic syntax of C# programming
Language */
//
. For example:} //end class Rectangle
Member variable #
Rectangle
class has two member variables named
length
and
width
.Member function #
AcceptDetails
、
GetArea
and
Display
.Instantiate a class #
ExecuteRectangle
is a containing
Main()
methods and instancing
Rectangle
class.Identifier #
@
, the beginning can be followed by a series of letters, numbers (0-9), an underscore (
_
),
@
.
?
-
+!
#
%
^
&
*
(
)
[
]
{
}
.
;
:
"
'
/
\
.
@
prefix. For example,
@if
is a valid identifier, but if is not, because if is a keyword.C# keyword #
@
characters as prefixes.
get
and
set
, which are called context keywords (contextual keywords)