C # Learn the third day

Source: Internet
Author: User

After these days of study, really a little thought before the C-learning is too bad now learning C # is not smooth, although a lot of things are familiar, but it is also necessary to see several times to remember, and now are simple things, have not seen the overload and other difficult places. Should make a good effort, yesterday busy all things are more than 11 o'clock, still be learning elder sister scold of not can not, on the early rest, really should be able to see a bit every day, so also can remember a bit, every day can learn something still can a little accomplishment feeling. It is said that it takes at least 23 days to develop a habit, I really want to try it is not really, in fact, the purpose of blogging is to urge yourself, every day to pressure yourself, do not like a freshman time as nothing, busy with those so-called student work, pay too little too little, hope to find the state of high school, The surrounding environment is not a reason, time is not a reason, the only thing that I feel very annoying is the doctoral dissertation, I learned a few years of science, university or engineering, all day also to write the script of ideological and political education, think it is not worth it, but only in their own blog spit slot, After all, there are too many places for the guide to help. Isn't that too snobbish? I also often think, direct altogether not to do, love. But think has already paid so much, to this year December also liberated, also endure, sincerely hope will not have this wonderful task, oneself do point oneself want to do, want to learn, stack stack code, learn to learn single-chip computer, engage in embedded is very good. Do not say nonsense, or talk about today's study it! Suddenly a lot of emotion ... However, my blog I am the master, haha haha.

Today I saw a bit of the escape character that block, and C almost, basically the same, also do not repeat, is more than a @ that function, the first character of the variable name must be a letter, underscore or a number. The name of the variable, there are two main types of naming, one is the CamelCase way, to give a few examples to understand, Age,firstname,timeofdeath (the name is so scary), is the first letter is not written, the other words appear in the first letter to capitalize , there is a pascalcase way, look at the name can be seen, more than the previous naming method capitalized, as if our teacher used to speak C language when said a Hungarian naming law and the second kind of like, do not know is a kind. For both of these naming methods, the official argument is that for simple variables, the first is better, for the complex, the second, but the individual prefers the second.

Just down to the usage of the @, you can effectively simplify the code, reduce the use of escape characters, so that the code more concise. Here is an example should be able to understand clearly, without @ words for the following string "C:\\mydir\\myfile.doc" can be simplified to "C:\Mydir\Myfile.doc" of course, this method is also an accident, For example, if there are double quotes in the string itself, it must be escaped to avoid ending the string. Next, a basic thing, the conversion of data types: implicit conversion and explicit conversion, the so-called implicit conversion, is a quiet conversion, do not need to do any work, do not need any code, here is not much to say, and should not be stuck in this place. It is important that explicit conversions are performed explicitly when the compiler is explicitly required to convert a numeric type. The simple display conversion format is < (destinationtype) Sourcetype>, so that the data type of the Sourceval value becomes the case in the preceding brackets. The code's still going to stack up.

BYTE Destinationvar;

Short Sourcevar = 7;

Destinationval= (byte) Sourcevar;

Console.WriteLine ("Sourcevar val:{0}", Sourcevar);

Console.WriteLine ("Destinationvar val:{0}", Destinationvar);

We can get

Sourcevar Val:7

Destinationvar Val::7

Although the code is very simple, or to knock out, the impression can be profound, after all, laying the groundwork. However, if the data is too large to overflow, for example, the short type value is 281, the result will be 25, loop, because the byte type can be expressed at most is 255. So be aware of the range sizes of different data types when converting data types. To prevent overflow, you can add a check function for overflow checking, and a prompt for program crashes if overflow. In addition to this one way, there is another slightly more complicated conversion, using convert. Do not say other, directly on the code:

static void Main (string[] args)//and Java It's like a mother.

{

Short shortresult,shortval=4;

int integerval=67;

Long Longresult;

float floatval=10.5f;

Double doubleresult,doubleval=99.999;

String stringresult,stringval= "17";

BOOL Boolval=true;

Console.WriteLine ("Variable Conversion examples\n");

Doubleresult=floatval*shortval;

Console.WriteLine ("Implicit,->double:{0}*{1}->{2}", Floatval,shortval,doubleresult);

shortresult= (short) floatval;

Console.WriteLine ("Explicit,->short:{0}->{1}", Floatval,shortresult);

Stringresult=convert.tostring (boolval) +convert.tostring (doubleval);

Console.Write ("explicit,->string:\" {0}\ "+\" {1}\ "->{2}", Boolval,doubleval,stringresult);

Longresult=integerval+conver.toint64 (Stringval);

Console.WriteLine ("Mixed,->long: {0}+{1}->{2}", Integerval,stringval,longresult);

}

Code to end this piece of blood to vomit, good boring, but the practice is still to practice, this can not escape.

The next step is to say one of the complex variable types: enumerations.

Directly on the code:

Enum<typename>//Definition Enumeration

{

<value1>

<value2>

...

<valuen>

}

<typeName><varName>; Declaring a new variable

<varName>=<typeName>.<value>; Assign value

You can also add types:

Enum<typename>:<underlyingtype>//define enumerations, and set the storage type later

{

<value1>

<value2>

...

<valuen>

}

By default, each value is automatically assigned to the corresponding base type value in the order defined. That is, the value of value1 is the value of 0,value2 is 1 and so on. You can also override this process:

Enum<typename>:<underlyingtype>//define enumerations, and set the storage type later

{

<value1>=<actualVal1>

<value2>=<actualVal2>

...

<valuen>=<actualValn>

}

You can also specify the same value for multiple enumerations by using one value as the underlying value of another enumeration value:

Enum<typename>:<underlyingtype>//define enumerations, and set the storage type later

{

<value1>=<actualVal1>

<value2>=<value1>

...

<valuen>=<actualValn>

}

Any job that does not have an assignment automatically gets an initial value, and the value used here is a sequence that starts at 1 larger than the last explicitly declared value. For example, the value of <value3> in the previous example is <value1>+1.

Assigning a value in a circular manner may result in an error, such as:

Enum<typename>:<underlyingtype>//define enumerations, and set the storage type later

{

<value1>=<value2>

<value2>=<value1>

}

Forced data type conversions for enumerations:

Directionstring=mydirection.tostring (); Equivalent to

Directionstring=convert.tostring (mydirection);

Where mydirection is a data variable of the enumeration type, directionstring is a data variable of type string.

Converts a string to an enumeration value, with a specific command Enum.parse (), using the following:

(Enumrationtype) Enum.parce (typeof (Enumrationtype), enumrationvaluestring);

The specific application is as follows

String mystring= "North";

Orientation mydirection= (Orientation) enum.parse (typeof (Orientation), myString); Where orientation is an enumeration type

Just now the system crashed, scared the baby, thanks to automatically save .....

C # Learn the third day

Related Article

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.