C#基礎筆記

來源:互聯網
上載者:User

標籤:

第一章:


1.C#建立程式的基本結構

class 類名

 

{  static void Main(string[]args)  {   }}

 

注意:
1)namespace
2)using
3)類名命名規則:字母、數字、_組成 數字不能開頭 不能是關鍵字 首字母大寫
4)Main4種
static void Main(){}
static int Main(){}
static void Main(string []args){}
static int Main(string[]args){}

 

2.C#資料類型
基本:
int
double
char
bool
float
引用:
string
數組
類

3.C#定義變數

int a=10;int a;a=10;

 

4.常量定義
const int a=3.14;
注意: 常量一旦定義之後就不允許去修改

5.C#向控制台輸出資料:Console.WriteLine(); Write();
接收資料:Console.ReadLine();

6.C#方法定義
public/private 傳回值類型 方法名(參數列表)
{
主體;
}

7.類和對象
類的成員:欄位(private)、屬性(public)、方法(public)

 

class Student{   private int age;   public int Age   {        get{return age;}        set{if(value<0){age=18;}else{age=value;}}   }   public void Study(){}}Student a=new Student();a.Age = 9;a.Study();

 

 8. 注釋
  C#中有三種注釋, 分別是單行: // 多行 /* */  文檔 ///

9. 調試: F9設定斷點    F5啟動調試




第二章:
1.C#運算子
1)賦值=
2)邏輯&& || !
3)關係> < == >= <= != 
4)算術+ - * / % ++ --
5)條件 ? :  
6)複合(賦值) +=  -= *=  sum=sum+i;  sum+=i; 
2.C#分支結構
1)基本:if(){}else{}
2)嵌套:if(){if(){}else{}}else{if(){}else{}}
3)多重:if(){}else if(){}else if(){}....else{}
4)switch:
switch(運算式intar/string)
{        case 常量:             語句;             break;        ......        default:             語句;             break;}


3.迴圈結構:
1)while(){}

2)do{}while();

3)for(表達1;表達2;表達3){}

4)foreach(資料類型 變數名 in 數組/集合){}


4.數組:
1)int  []a; a=new int[5];2)int []a={2,3};  int []a=new int[]{2,3};  int []a=new int[2]{2,3};3)int []a=new int[5];


5.冒泡排序
外層:N-1
裡層:N-1-i
int []a={34,12,56,3,89};for(int i=0; i<a.length-1; i++)//輪{  for(int j=0;j<a.length-1-i; j++)//每輪比較的次數  {        //比較交換        if(a[j]>a[j+1])        {                int temp=a[j];                a[j]=a[j+1];                a[j+1]=temp;        }  }}

樣本:  int [] nums = {5,9,8,6,89};  使用冒泡排序,一共會比較幾次?  10次  
第三章:
1. 存取修飾詞: 
   private 能有類的內部可以使用, 存取層級是最低的。  public 
1.實值型別和參考型別作為方法的參數進行傳遞的特點
值傳遞:形變實不變
引用傳遞:形變實也變(string 除外)

第四章:
1.string類方法 
string s="hello hello"; string s1="how are you";1)SubString() s.SubString(2,3) s.SubString(1);2)IndexOf()   s.IndexOf(‘e‘) s.IndexOf("ll");3)LastIndexOf()4)Join() string str=string.Join("**",arr);how**are**you5)Equals()6)ToLower()7)ToUpper()8)Trim()9)Split()   string[]arr=s1.Split(‘ ‘); 10)Compare() int r=string.Compare(s,s1);(1/-1/0)11)Format() string s=string.Fromat("格式字串",參數列表);

 

2.資料類型轉換


1.string--->值 xxx.Parse(); 注意 Parse裡面的值一定是一個字串類型,
並且是能夠正確轉換為對應的數值的類型。 比如: int a = int.Parse("1.5"); 運行時出錯
值---->string toString();


2.值----值
顯示轉換:int a=(int)5.6;
隱式轉換:double d=10;

 

3.任---任
XXX c=Convert.ToXXX(變數);


***************************************************************************
************************** 資料庫部分 *************************
***************************************************************************


基本的增刪改查文法
-------------------------
1.插入文法結構
1)單行插入
insert into 表名(列名)
values (值)

2)使用union多行插入
insert into 表名(列名)
select 值 union
select 值 .....
select 值

3)新表不存在
select 舊錶列名
into 新表名
from 舊錶名

4)新表存在
insert into 新表名
select 舊錶列名
from 舊錶名


2.刪除
delete from 表名 where 條件
truncate table 表名

3.修改
update 表名 set 列名=新值 where 條件

4.查詢
select 列名
from 表名
where 條件
group by 分組列名
having 篩選條件
order by 排序列名 desc

 

資料庫連接部分
---------------------

資料庫連接步驟:

一、建立DBHelper類

1.建立連接字串

static string conStr=@"server=.;database=myschool;uid=sa;pwd=sa;";
private static SqlConnection con;public static SqlConnection Con{        get        {                if(con==null)                {                        con=new SqlConnection(conStr);                con.Open();                }                else if(con.State==System.Data.ConnectionSate.Closed)                {                con.Open();                }        return con;        }}

二、方法

1.ADO.NET的兩個重要組件
.NET Framework資料提供者
DataSet資料集

2..NET Framework資料提供者的4個核心對象
Connection:串連資料庫
Command:執行命令(SQL語句)
DataReader:讀取資料來源中的資料
DataAdapter

 

1.構建SQL語句
string sql=string.Fromat("",);

2.建立Command對象
SqlCommand cmd=new SqlCmmand(sql,DBHelper.Con);

3.調用方法
1)查詢單個值:xxx c=Convert.Toxxx(cmd.ExecuteScalar());
2)insert/update/delete:int r=cmd.ExecuteNonQuery();
3)查詢大量資料:
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
{
sdr["列名"];
sdr[0]
}
sdr.Close();

4.關閉資料庫連接
DBHelper.Con.Close();


5. 異常處理
try{} catch(){} fianly{}

 

來自http://bbs.ichunqiu.com/thread-7131-1-1.html 的分享

 

 

C#基礎筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.