JAVA to C #. net C # some basic functions are commonly used, JAVA

Source: Internet
Author: User

JAVA to C #. net C # some basic functions are commonly used, JAVA

After learning java, it is actually very simple to learn c #. There are not many differences in the basic syntax. When you use c #, you will find that it is more flexible than java.

C # common basic functions
1. the output function flashed through the Console. writeLine ("hello"); Console. write (""); Do not wrap 2. with this sentence added, it won't flash through the Console. readKey (); 3. read a String str = Console from the screen. readLine (); 4. placeholder output Console. write ("1 + 1 = {0}", 2); // The same as the C placeholder 5. use \ as the escape character String s = @ "\\\\" @ to indicate that \ is not used as the Escape Character 6. type conversion String str = "1"; int I = Convert. toInt32 (str); // converts a String to an integer String s = Convert. toString (1); // converts a number to a string int. parse (str); // converts a string to a number. If the string fails to be converted, an exception is thrown. A numeric int is returned. tryParse (str1, out I); // if you fail to convert the String to an int, return the String str = "abcde"; str. subString (2); // return cdestr. subString (5th); // returns the string cde7. enum Sex {MAIL = 'male' from 2 + 1 ', FEMAIL = 'female '} // converts the char or int type to an enumeration char c = 'male'; Sex sex = (Sex) c; // convert String type to Enumerative String a = "female"; Sex sex = (Sex) Enum. parse (typeof (Sex), a); // sex is the corresponding man's enumeration // convert the enumeration to char or intchar csex = (char) sex. MAIL; // convert enumerative to String ssex = Sex. MAIL. toString (); // use switch to convert it to the enumeration type, and then use swith
Class and struct
1. using System; namespace HelloWord {class Person {private String name; private int age; public String Name {set {this. name = value;} get {return this. name ;}} public int Age {set {this. age = value;} get {return this. age ;}} class Program {public static void Main (string [] args) {Person person Person = new person (); Person. age = 20; person. name = "Zhang Liang"; Console. writeLine (person. age); Console. readKey () ;}}// get set, which can also be written as public int Age {get; set ;}// reference Person a = new Person (); person B = a;. age ++; Console. writeLine (B. age); //. the value after age ++. The reference type uses a pointer to point to the object when passing a parameter // while the int char datatime bool value type is a copy value, so the two changes are irrelevant. C # also uses the structure struct Person {string name; int age;} in c ;} // when using this function, you do not need to declare multiple types of direct Person person1; person1.name = "zhangsan"; person1.age = 1; instead, you do not need to define multiple attributes.
Capture exception information
Public static void Main (string [] args) {Person person = new Person (); person. age = 20; person. name = "Zhang Liang"; try {person. age = Convert. toInt32 (Console. readLine ();} catch (Exception e) {Console. writeLine (e. message. toString (); // print the exception information Console. writeLine (e. data. toString (); Console. writeLine (e. getType (); // print the exception type} finally {Console. writeLine ("try_catch... execution completed ");} Console. writeLine (person. age); Console. readKey () ;}besides try cacth, throw new Exception ("message ");
Inheritance

Class Person {

}

Class Chinese: Person {

}

// Different from the root java, except for writing extends: nothing else

Constant
Constant public const int pi = 3.14; static variable public static int stage = 10; in static methods, you cannot directly call non-static members. In non-static members, you can directly call static members in. net, there are static classes used to implement some pure function libraries, that is, no new
Indexer
The indexer is an example of String str = "abc"; str [0] That is similar to an array for easy data access. // The instance indexer that writes an indexer under the first character can be directly returned. In other words, the instance indexer can manipulate data as conveniently as an array .. For example, string str = "abc"; char c = str [0]; // str [0] is an example of the indexer, isn't it easy to retrieve data? For another example, datarow row = datatable. rows [0] // is it convenient to retrieve data rows? Another custom example is public class IndexerDemo {string [] strs = new string [3]; public string this [index] {get {return strs [index];} set {strs [index] = value ;}} IndexerDemo indexer = new IndexDemo (); indexer [0] = ""; // assign the value indexer [1] = "B"; indexer [2] = "c"; string str = indexer [0]; // isn't it convenient to operate the data with the value? Practice and comprehend it by yourself. This has a major effect.
Another method to return multiple parameters
Different from java, java only returns one parameter. net can return multiple parameters. You only need to add out in the method and the value of out must be assigned public void test (out String name, out int age) {name = "abc "; age = 1;} call String name; int age; test (out name, out age); ----------------------------------- The following is an example ////// Determine the logon information //////User name ///Password ///Returned logon information ///
 
  
Return to logon status
 Public static bool login (String username, String password, out String msg) {if (! Username. Equals ("admin") {msg = "Account Error"; return false;} if (! Password. equals ("88888888") {msg = "Password error"; return false;} msg = "Logon successful"; return true;} call the Console. writeLine ("Welcome to 888 challenge cup, please enter the account password"); String name = Console. readLine (); String password = Console. readLine (); String msg; login (name, password, out msg); Console. writeLine (msg); --------------------------- simulate TryParse ();////// Simulate the TryParse function to convert a string into an int //////String to be converted ///Converted int ///
 
  
Status of successful conversion
 Public static bool MyTryParse (String str, out int result) {result = 0; try {result = Convert. toInt32 (result); return true;} catch (Exception e) {return fasle ;}}
Reference passed Value
After passing a value parameter in java and changing its status, the int double datatime byte itself will not be changed and will not be passed to a method, if the ref parameter is used, it is equivalent to a pointer. We change the value in it, this value also changes when using ref. You must assign a value outside the method. Here is a specific example ////// If no intermediate variable is used, two int-type variables are exchanged /////////Public static void swap (ref int a, ref int B) {a = a-B; B = a + B; a = B-a;} call int a = 1; int B = 2; swap (ref a, ref B); Console. writeLine ("{0} and {1}", a, B); Output 2and1
Common functions in winform
Visible = false indicates hiding true indicates visible textBox. appendText (str); // used to assign values to the text box. This will not generate multiple textBox objects. text = str; // You can also assign values. When there are many characters in the Text, we recommend that you use the preceding Application. exit (); // Exit the program str. lines; // returns the array consisting of line breaks. // the drop-down list Combobox. selectedIndex; // indicates the selected Combobox. selectedItem; // indicates the selected name.

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.