C # For Beginners

Source: Internet
Author: User

Today, I suddenly became interested in learning C #. I rummaged through books and thought C # is really a good language. I didn't even talk about C # In college classes, I hope I can understand it through extra-curricular self-study ~~

It can be said that people who have been familiar with C, C ++, and java programming are actually quite simple about getting started with C #. However, I have learned C #, but I have not learned the depth of C #. It is hard to say that, but I hope I can remember some simple features ~~

The simplest way to get started with helloworld is not to mention, but the following is also a simple method. The tool used is visual studio 2012.

The system namespace name must be introduced, but the tool visual will be automatically generated when you create a project, so the code will not be pasted

Using HelloWorld; namespace Test {class MainPrograme {static void Main (String [] args) {Program p = new Program (); p. show () ;}} namespace HelloWorld {class Program {public void show () {Console. writeLine ("You must reference the namespace HelloWorld before output. Otherwise, an error is prompted! "); Console. ReadLine ();}}}

**************

The using command is followed by the namespace name. The using namespace std in C ++ is similar to that in order to facilitate the introduction of resources.

To use classes and methods in the namespace Test in the namespace HelloWorld, you must declare using HelloWorld before. The better visual tool is, when you do not use using HelloWorld, during programming, if you want to write the Program class, it will automatically throw an exception to avoid unnecessary compilation errors.


Because the usage and type of C # and C are similar, the following code only shows some of them:

Namespace Third {enum MonthOfYeear {January, Feburary, March, 1_l, May, June, July, Aguest, September, October, Novermber, december} class Program {static void Main (string [] args) {/* byte implicit conversion, same as other languages */int x = 123; byte y = 123; int result = x + y; Console. writeLine ("result:" + result);/* use bool type */bool flag = false; Console. writeLine ("Enter the first number:");/* forced conversion of Convert */int m = Convert. toInt32 (Console. readLine (); Console. writeLine ("enter the second number:"); int n = Convert. toInt32 (Console. readLine (); if (m> n) {flag = true;} else {flag = false;} Console. writeLine ("Result:" + flag);/* use the enum type */MonthOfYeear mon; mon = MonthOfYeear. aguest;/* pay attention to the syntax used for output */Console. writeLine ("this month is {0}", mon );}}}

The program is relatively simple, so it should be noted that Convert. toInt32 (Console. readLine (); explicitly forced type conversion and Console. read data from the keyboard in ReadLine (). The output result is True or False. In addition, pay attention to the output format of the last line. in C #, most of them are output formats above.


Reference variables:

String is the reference type, but when = or! = During operation, the string object instead of the referenced value is compared.

The reference type uses the new Keyword to create an object instance and store it in the heap.

There are also many references to variables. I think they are similar to java.

Namespace Reference {// <summary> // a variable of the Reference type is called an object, reference to actual data can be stored // </summary> class Sample {public int I = 10;} class Program {static void Main (string [] args) {object o; // define the reference variable o = 1 for the object type; // assign a value to the Console. writeLine ("Initial Value of referenced variable:" + o); o = new Sample (); // use the new keyword to instantiate the class and assign it to the variable Sample s; // declare Class Object s = (Sample) o; // use the reference variable to instantiate the object Console. writeLine ("the value of the referenced variable is:" + s. I );}}}

The reference variable packing and unpacking operations can be better understood through the following code

Namespace Box {class Program {static void Main (string [] args) {/* the value of the value type variable is copied to the boxed object, changing the value of a value type variable does not affect the value of the boxed object */int I = 10; object obj = I; // You can bind the variable I to the Console. writeLine ("I value: {0}, boxed value: {1}", I, obj); I = 22; Console. writeLine ("I value: {0}, boxed value: {1}", I, obj); int j = (int) obj; Console. writeLine ("value after unpacking, packing value {0}, unpacking value {1}", obj, j ); /* the value of the packing object is the same as the value type data obtained by the unpacking. the packing and unpacking must comply with the type consistency principle */}}}

Write these for the moment. These are simple cases learned in reading books. The procedures are simple and easy to get started ~~~



This article from "tired also happy D" blog, please be sure to keep this source http://zhangzhang.blog.51cto.com/6250085/1259856

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.