The tutorial first explains C #, here is a rough explanation, please purchase the relevant books, or refer to the relevant documents. C # has canceled pointers in C + +, and the number of operators used in C + + (::->) is no longer present, and C # supports a ".". C # has all the features of an object-oriented programming language, such as encapsulation, inheritance, polymorphism, and so on. And more object-oriented than Java, each type can be considered an object. But C # means to allow single inheritance, that is, a class will not have more than one base class. In addition, C # has no global functions, no global variables, and no global constants. Everything must be encapsulated in a class.
Here's a small example of a console application:
Using System;
Class Mikecat
{
public static void Main ()
{
Console.WriteLine ("Mike old cat C # asp.net Introductory series Tutorial-Welcome to visit the ideal of the Old cat");
}
}
In C # The program always starts with the main () method, and the main () method must and can only be included in a class, and the main () method returns a type of void (no return value) or int (returns an integer representing the application error level).
The above using System; used to import a namespace (Namespace) to indicate the hierarchical relationship of a class. If you do not use a using-import namespace, you must precede the class name with the name space each time a class is used.
The input and output of the C # program is implemented through the console. The console is a class under the system name space. Output a string on the screen with Console.WriteLine (), accept the input device input using the Console.ReadLine () method.
Program code:
Class Mikecat
{
public static void Main ()
{
System.Console.WriteLine ("Mike old cat C # asp.net Introductory series Tutorials-Welcome to the ideal of the Old cat");
System.Console.WriteLine ("Please enter user name:");
String User=system.console.readline ();
System.Console.WriteLine ("Welcome: {0} 's coming!", user);
}
}
The first argument in the parameter table of the WriteLine () method, followed by the string, replaces {0}.
If you want to pass command-line arguments to the application when you execute the program, the main () method should be in the format:
Using System;
public class Mikecat
{
public static void Main (string[] args)
{
Console.WriteLine ("Total {0} command-line arguments", args.) Length);
for (int i=0;i<>
{
Console.WriteLine ("Arg[{0}]=[{1}]", I,args[i]);
}
}
}
Single-line annotations in C # with//, multi-line annotations with/*...*/
Using Constants in C #:
Using System
Class Mikecat
{
Public const double pi=3.14;
public static void Main ()
{
Console.WriteLine ("Pi Pi value is {0}", pi);
}
}
A struct type (struct) is a composite data type that organizes some of the related data into a new data type.
Using System;
struct MIKECAT
{
public string Mike;//user
Public UINT Age;//age
public string Email;//email
}
Class Mf
{
public static void Main ()
{
Mikecat zl;//declaring struct type variable ZL
Zl. Name= "Mike Old cat";
Zl. age=24;
Zl. Email= "mike@hebut.com";
Console.WriteLine ("Name: {0}, Age: {1}, mailbox {2}", ZL. Name,zl. Age,zl. Email);
}
}
The enumeration type (enum) in C # is a set of logically inseparable integer values:
Each element type in the enumeration type in C # is an int byte long, and the first element value is 0, followed by a 1 increment. You can also assign values directly to an element in an enumeration, followed by increments.
Enum Weekday:byte
{
Sunday=1,monday,tuesday,wednesday,thursday,friday,saturday
On the back, the array is what we often use, let me introduce: An array is a set of data of the same type. When accessing data in an array, it can be indicated by subscript. In C #, an array element can be any data type, and the array subscript starts at 0, the first element corresponding to the subscript 0, and then incremented one at a time. Arrays can be one-dimensional or multidimensional.
An array of one-dimensional integers containing 6 elements;
Int[] Mf1=new int[6]; Note the scope of the initialization array, or specify an initial value;
A one-dimensional array of integers containing 6 elements, with the initial value 1,2,3,4,5,6
Int[] Mf2=new int[6]{1,2,3,4,5,6};
A one-dimensional array of strings that can also omit the new operator if an initializer is provided
String[] mf3={the "C", "C + +", "C #"};
One-dimensional object array
object[] Mf4 = new Object[5] {26, 27, 28, 29, 30};
Two-dimensional integer array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4
int[,] mf5=new int[,]{{1,2},{3,4}};
6*6 Two-dimensional integer array
int[,] mf6=new mf[6,6];
let's look at the traversal of a one-dimensional string array
Using System;
public class Mikecat
{
static void PrintArray (string[] arr)
{
Prints the array element, arr. Length represents the number of array elements
for (int i=0;i
<>
{
Console.WriteLine ("Arr[{0}]={1}", I,arr[i]);
}
}
public static void Main ()
{
String[] arr={the "C", "C + +", "C #"};
Passing the array as a parameter
PrintArray (arr);
}
}
Program Result: Arr[0]=c arr[1]=c++ arr[2]=c#
let's look at the traversal of an integer array of 4 rows 2 columns (4*2):
Using System;
public class Mikecat
{
static void PrintArray (int[,] arr)
{
Traversing a two-dimensional array through a two-time for loop
for (int i=0;i<4;i++)//Initialize I as a loop variable, i++ implement the self increment operation of the variable.
After the For loop satisfies the condition, executes the i++ one time after executing the loop, and then goes to the next loop. Simple c syntax, here do a simple introduction to care for beginners. (Refer to Mr. Tan Haoqiang's C language program)
{
for (int j=0;j<2;j++)
{
Console.WriteLine ("Arr[{0},{1}]={2}", I,j,arr[i,j]);//print each two-dimensional array element
}
}
}
public static void Main ()
{
Main function
Passing the array as a parameter
PrintArray (New int[,]{{1,2},{3,4},{5,6},{7,8}};
}
}
Run Results: Arr[0,0]=1 arr[0,1]=2 arr[1,0]=3 arr[1,1]=4 arr[2,0]=5 arr[2,1]=6 arr[3,0]=7 arr[3,1]=8
Class is the basic building block of object-oriented programming, which we describe in detail later. Here we introduce two special classes, the object class and the String class
1.object Class
The object class is an alias for a predefined class System.Object, which is a base class for all other types. All types in C # are inherited directly or indirectly from the object class. Therefore, a variable of an object class can be given any type of value.
int i=30;
Object obj1;
Obj1=i;
Object obj2= ' a ';
2.string class
The string class is used specifically for manipulating strings, and he is an alias for predefined class System.String
String str1= "Mikecat";
You can connect two strings with a "+" number.
String str2= "Username:" + "Mikecat";
If you are accessing a single character, use the subscript.
Char c=str1[0];
Compare two strings for equality, use the comparison operator "= ="//differ from basic syntax
BOOL B= (STR1==STR2);
In C #, the most flexible and difficult to master pointers are canceled in C and C + +. So how do you provide functions in C/C + + for function pointers? C # provides a delegate (delegate), which is a reference type that inherits from the System.Delegate class. It is equivalent to a function pointer prototype. Unlike function pointers, delegates are type-safe in C #, and delegates are especially good for anonymous invocations. To use a delegate takes three steps, that is, declaration, instantiation, invocation.
Using System;
Declares a delegate named Mfdelegate, which has a string type of argument
When the C # compiler compiles, a new class is generated that inherits from System.Delegate, and the class
Known as Mfdelegate
public delegate void Mfdelegate (string name);
public class Mikecat
{
Defines a method that has the same parameter type as the mfdelegate hello ()
public static void Hello (string name)
{
Console.WriteLine ("Hello, {0}! ", name);
}
Defines a method with the same parameter type as Mfdelegate Goodbye ()
public static void GoodBye (string name)
{
Console.WriteLine ("Goodbye, {0}!", name);
}
public static void Main ()
{
Create a Mfdelegate instance MF1
Mfdelegate mf1=new mfdelegate (Hello);
Call MF1
MF1 ("Mikecat");
Mfdelegate mf2=new mfdelegate (GoodBye);
MF2 ("Mikecat");
Combine MF1 Mf2 into a new delegate mf3
Mfdelegate Mf3=mf1+mf2;
Call MF3
MF3 ("Mike old Cat");
Remove MF1 from a grouped delegate mf3
Mfdelegate Mf4=mf3-mf1;
MF4 ("Mikecat");
}
}