into C # (my C # Learning tour) of the Four (3)

Source: Internet
Author: User
Tags array array definition arrays definition variables
What do you think?
The answer is this.

S1 is A string
S2 is A string
S1 is now anothing string
S2 is now string

How do you feel when you see the result? I was very surprised. Because the way the string was implemented according to the original C,

In this face as a pointer, S1 and S2 should point to the same address.

There is also another problem with string processing, as in the following example:
To build the following string: "C:\PROGRAM file\wom", I think you know what I mean!
This means that there is \ but \ is used as an escape character.
(well, very simple) This is the way: "C:\\Program File\\wom", completely correct. and C # provides another

Ways to solve such problems. is actually introducing a @:
The above can write string a=@ "C:\PROGRAM File\wom" so that C # will take all characters after @ as

, and he also contains line breaks.

OK, here's the basic data type for C # that's all! (Oh, there seems to be something missing, yes, I'm going to say

Out! )

Above is the most basic data type, let's talk about the complex data types of people created by these basic data types.

For composite data types, it is described in two categories.

The first thing to say is the value type. In C #, two types of composite values, structs and enumerations, are provided.
Are you familiar with these two types of people? But don't be careless, in C # These two types already have the not small

Of change.

Just look at an example, or a comparison of the examples to illustrate the problem.

A structure is defined as follows:
public struct subscriber
{
public long Lngsubscriberid;
public string strFirstName;
public string Strmiddlename;
public string strLastName;
public decimal decbalance;
}
is not the original structure with a very big difference! In C #, structs are actually a special class.
And look at the use of the structure:

Subscriber Subscriber1;
Subscriber Subscriber2;

Subscriber1=new subscriber ();
Subscriber1.strfilstname= "John";
Subscriber1.strmiddlename= "Q";
Subscriber1.strlastname= "Public";
subscriber1.decbalance=100;

Subscriber2=subscriber1;

Two structures are defined here: Subscriber1,subscriber2.
The Subscriber1 is then initialized with the new operator. About initialization Here's one more word. For structures that can be

To start without the new operator. Because it was initialized at the same time as the definition, the default of all fields

The recognition value is 0. But there is another drawback to this approach, which is that there is no way to replicate two structural variables.
The following code implements the assignment to the new structure. And the last line, is what I call the duplication between the structures.
So what good is the structure? I think the main point is in the function of the transfer, you can call multiple variables to

is passed in a struct variable.
As I said before, structs are a special class, and the main difference between them is that the structure does not support inheritance.

Well, having said so much, here's a look at the enumeration.
Or an example to look at the enumeration.

public enum TimeOfDay
{
Morning=0,
Afternoon=1,
evening=2
}
Class Enumexample
{
public static int Main ()
{
Writegreeting (timeofday.morning);
return 0;
}
static void Writegreeting (TimeOfDay TimeOfDay)
{
Switch (TimeOfDay)
{
Case timeofday.morning:
Console.WriteLine ("Good morning!");
Break
Case Timeofday.afternoon:
Console.WriteLine ("Good afternoon!");
Break
Case timeofday.evening:
Console.WriteLine ("Good evening!");
Break
Default
Console.WriteLine ("Helo!");
Break
}
}
}

For this enumeration I think we have a glance! For enumerations I personally think that is a bit like a constant that is used in name

Words to make the data?

For a composite value type, here's a look at the composite reference type (array, class, interface, delegate).

My top of course is the most familiar, that is the array:
The use of C # Arrays has also been affected by the limited use of pointers in C #.
First, let's look at the examples:

Int[] A;
Int[] A = new int[32]
a[0]=35;
a[31]=322;
Int[] B;
B=new int[32];
String[] c={"A", "B", "C"};
String[] D=new string[]{"A", "B", "C"};

For the above example, several issues to be noted are illustrated.
(1) The initial subscript or 0 of the array in C #.
(2) Initialization of an array uses the new operator.
(3) Arrays can also be defined dynamically in C #.
(4) For the initialization of the array support and Java-like {} mode.
(5) In the initialization of the array this method is never allowed
int len=3;
String D=new string[len]{"A", "B", "C"};
Corresponding solution: This can be written: const int len=3;
That is, you cannot set the length of an array to a variable.
(6) Also forgotten is the array definition to use [].
(7) Another point is not allowed to use the following way:
String[] E=new string[3]{"A", "B", "C", "D"};

Let's look at an example below:

String[] d=new string[]{"Linda", "Lily", "Adidas"};
int ff=d.length; Take data length
int ee=d.getlength (0); Remove the length of the specified data dimension
Array.Sort (d); sorting arrays
Array.reverse (d); Reverse the sequence of an array

Because arrays are represented as a special type in C #, they have some of their own methods. These methods provide a great convenience for us to use arrays.

These are all used for one-dimensional arrays, and C # also supports multidimensional arrays. Let's take a look at the multidimensional array.
Or an example to understand the definition and operation of multidimensional arrays.

int[,] a=new int[,]{{1,2,3},{1,2,3},{4,5,6}};
Int[,,] b;
Int[,,] b=new int[,,]{10,20,30};

String[][] Arylists=new string[3][];
Arylists[0]=new string[]{"A", "B", "C"};
Arylists[1]=new string[]{"D", "E", "F"};
Arylists[2]=new string[]{"G", "H", "I"};
int i;
For (i=0;i<arylists.getlength (0); i++)
{
Int J;
for (j=0;j<arylists[i). GetLength (0); j + +)
{
Console.Write (Arylists[i][j] + ",");
}
Console.Write ("\ n");
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.