C#一些用法

來源:互聯網
上載者:User

 C#一些用法

 

視窗類別:

 

關閉視窗:this.Close();隱藏視窗:this.Hide();顯示視窗:this.Show();彈出資訊的小視窗:MessageBox.Show("資訊");textBox的一些用法:'textBox2.Text = "sda"; // 把字串顯示在文本中在C#中把textBox置為密碼框,可以直接在屬性的行為的passwordChar置為*String spass = textBox2.Text; // 得到文本中字串Application.Exit(); // 關閉所有介面 C# ListView用法詳解http://blog.csdn.net/czw2010/article/details/7910324以下為簡單的應用:ListView_commuData.View = View.Details;//顯示表頭ListView.gridlines = true;//顯示網格線listView1.View = View.Details; // 顯示表頭listView1.GridLines = true; // 顯示網格線int per = 100; // 列寬listView1.Columns.Add("課程編號", per, HorizontalAlignment.Center);listView1.Columns.Add("課程名稱", per, HorizontalAlignment.Center);listView1.Columns.Add("任課教師", per, HorizontalAlignment.Center);listView1.Columns.Add("學分", per, HorizontalAlignment.Center);SqlConnection con = newSqlConnection("server= (local);uid=sa;pwd=密碼;database=資料庫名");con.Open();SqlCommand cmd = con.CreateCommand();cmd.CommandText = "SQL語句";SqlDataReader rs = cmd.ExecuteReader(); // 查詢的SQL語句while (rs.Read()) {String[] s = new String[4];for(int i=0;i<4;i++)s[i] = String.Format("{0}", rs[i]); // 取出第幾列元素ListViewItem lvi = new ListViewItem();lvi.Text = s[0];for(int i=1;i<4;i++)lvi.SubItems.Add(s[1]);listView1.Items.Add(lvi);}con.Close();cmd.ExecuteNonQuery(); // 插入,更新,刪除操作,返回的是影響的行數 資料庫進行串連:命名空間:using System.Data.SqlClient;// 等待資料庫連接SqlConnection con = new    SqlConnection("server= (local);uid=sa;pwd=密碼;database=資料庫");con.Open();SqlCommand cmd = con.CreateCommand();cmd.CommandText = "select * from teacher ";SqlDataReader rs = cmd.ExecuteReader();while(rs.Read()){String x = String.Format("{0}", rs[0]); // 取出資料庫中的第幾個下標的資訊String y = String.Format("{0}",rs[2]);}con.Close();rs.Close();cmd.ExecuteNonQuery(); // 插入,更新,刪除操作,返回的是影響的行數

 

  

 

  

 

 

 

 

 

 

 類似於C++ STL

 

 

 

操作C++(STL)C#(.net)說明包含#include <vector>using System.Collections.Generic;C++中也可以using namespace std;聲明std::vector<int> array;List<int> array = new List<int>();迭代器聲明std::vector<int>::iterator iter=array.begin();List<int>.Enumerator iter = array.GetEnumerator();C++array.push_back(4); array.insert(iter,51);C#array.Add(4); array.Insert(2, 51);C++int val=array[0]; val=array.at(1);  //進行下標檢測C#int val = array[0];array.Contains(5); //是否包含5 array.IndexOf(1);  //元素1的下標 C++array.pop_back();  //刪除最後的元素 array.erase(iter);  //刪除iter位置的元素 array.clear();C#array.Remove(1);  //刪除"1”這個元素 array.RemoveAt(0);  //刪除0號元素 array.Clear();C++array.empty(); array.capacity();  //容量(根據當前非配的記憶體) array.size();  //元素數C#array.Count;  //元素數 array.Capacity;  //容量 遍曆C++for (std::vector<int>::size_type i=0;i<array.size();++i){} for (iter=array.begin();iter!=array.end();iter++){} for_each(array.begin(),array.end(),func); //func()是函數C#for (int i = 0; i < array.Count; i++){} while (iter.MoveNext()){} foreach (int d in array){}排序std::sort(array.begin(),array.end());array.Sort();C++中要#include <algorithm>,上面的for_each也是如果是自訂的Key類型,C++中需要重載比較子;C#中可自訂相等比較子C++:map#include <map>map<string,int> map; // 當然不能夠用map,這裡只是說明方便而用的map_type::iterator iter = ma.begin();map[_T("first")]=5; map.insert(map_type::value_type(_T("second"),2)); map.insert(map_type::value_type(_T("second"),3));    //不覆蓋,不異常map.erase(iter);  //iter有效且不等於map.end() map.erase(_T("first")); map.clear();int val=map[_T("second")];  //沒有則構造新的 iter=map.find(_T("first")); if (iter!=map.end())     val=iter->second;for (iter=map.begin();iter!=map.end();++iter) //遍曆時刪除     map.erase(iter++);map.size(); map.empty();  //是否為空白C#:Dictionary命名空間:using System.Collections.Generic;定義:Dictionary<string, int> map = new Dictionary<string, int>();插入:map.Add("first", 5);   map["second"] = 2;  //這種操作已經進行了插入   //map.Add("second", 3);    //重複異常刪除:map.Remove("first"); 清空:map.Clear();調用:int data = map["second"];    //不存在則異常 map.ContainsKey("third"); map.ContainsValue(5); map.TryGetValue("hello", out data);注意C++中下標檢索時如果不存在這個元素也不產生錯誤foreach (KeyValuePair<string, int> pair in map)    //不能進行刪除操作C#:Hashtable命名空間:using System.Collections;定義:Hashtable ma = new Hashtable();插入:ma["dsa"] = 12; // 可以覆蓋之前的  ma.Add("dsadsa",123);刪除:ma["dsa"] = null;清空:ma.Clear();遍曆:foreach (DictionaryEntry i in ma)大小:ma.count不存在則為null

 

  

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.