C学习笔记.docx

上传人:b****4 文档编号:3741580 上传时间:2023-05-06 格式:DOCX 页数:22 大小:549.18KB
下载 相关 举报
C学习笔记.docx_第1页
第1页 / 共22页
C学习笔记.docx_第2页
第2页 / 共22页
C学习笔记.docx_第3页
第3页 / 共22页
C学习笔记.docx_第4页
第4页 / 共22页
C学习笔记.docx_第5页
第5页 / 共22页
C学习笔记.docx_第6页
第6页 / 共22页
C学习笔记.docx_第7页
第7页 / 共22页
C学习笔记.docx_第8页
第8页 / 共22页
C学习笔记.docx_第9页
第9页 / 共22页
C学习笔记.docx_第10页
第10页 / 共22页
C学习笔记.docx_第11页
第11页 / 共22页
C学习笔记.docx_第12页
第12页 / 共22页
C学习笔记.docx_第13页
第13页 / 共22页
C学习笔记.docx_第14页
第14页 / 共22页
C学习笔记.docx_第15页
第15页 / 共22页
C学习笔记.docx_第16页
第16页 / 共22页
C学习笔记.docx_第17页
第17页 / 共22页
C学习笔记.docx_第18页
第18页 / 共22页
C学习笔记.docx_第19页
第19页 / 共22页
C学习笔记.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C学习笔记.docx

《C学习笔记.docx》由会员分享,可在线阅读,更多相关《C学习笔记.docx(22页珍藏版)》请在冰点文库上搜索。

C学习笔记.docx

C学习笔记

C#学习笔记

你一定能成功开发漂亮的大型商用软件

入门:

控制台技巧

在调试C#控制台应用程序的时候,如果出现输出窗口一闪就关闭,就在程序最后一行加入:

Console.Read();或者Console.ReadLine();

以上两种的区别

Console.Read(),返回值为首字符的ASCII码

Console.ReadLine(),返回值为字符串

也就是说read方法只能读取第一个字符,而ReadLine能读多个字符也可以换行读取

数组命名并且循环输出

int[]arr=newint[]{11,2,3};

for(inti=0;i

{

Console.WriteLine(arr[i]);

}

Console.Read();

其中数组命名可以简化为int[]arr={11,2,3};

也可以后赋值

int[]arr=newint[3];

arr[0]=1;

arr[1]=1;

arr[2]=1;

动态赋值并且用静态方法输出

staticvoidst(intvalue)//定¡§义°?

一°?

个?

静2态¬?

方¤?

法¤¡§

{

int[]arr=newint[value];//在¨²方¤?

法¤¡§中D定¡§义°?

全¨?

局?

数ºy组Á¨¦变À?

量¢?

for(inti=0;i

{

arr[i]=i;//赋3值¦Ì

}

Console.WriteLine("value");

for(inti=0;i

{

Console.WriteLine(arr[i]);//循-环¡¤输º?

出?

}

}

staticvoidMain(string[]args)

{

inti=1;//在¨²主¡Â方¤?

法¤¡§中D定¡§义°?

整?

形?

变À?

量¢?

while(i>0){

Console.WriteLine("length");

i=Int32.Parse(Console.ReadLine());//从䨮窗ä¡ã口¨²获?

得Ì?

输º?

入¨?

值¦Ì

st(i);//引°y用®?

静2态¬?

方¤?

法¤¡§

}

Console.Read();

}

同样,也可以新增加一个类用来输出以及赋值

classAb{

publicvoidst(intvalue)//定¡§义°?

一°?

个?

静2态¬?

方¤?

法¤¡§

{

int[]arr=newint[value];//在¨²方¤?

法¤¡§中D定¡§义°?

全¨?

局?

数ºy组Á¨¦变À?

量¢?

for(inti=0;i

{

arr[i]=i;//赋3值¦Ì

}

Console.WriteLine("value");

for(inti=0;i

{

Console.WriteLine(arr[i]);//循-环¡¤输º?

出?

}

}

}

classProgram

{

staticvoidMain(string[]args)

{

inti=1;//在¨²主¡Â方¤?

法¤¡§中D定¡§义°?

整?

形?

变À?

量¢?

Abdd=newAb();

while(i>0){

Console.WriteLine("length");

i=Int32.Parse(Console.ReadLine());//从䨮窗ä¡ã口¨²获?

得Ì?

输º?

入¨?

值¦Ì

dd.st(i);//引°y用®?

静2态¬?

方¤?

法¤¡§

}

Console.Read();

}

}

应该使用public类,否则将无法访问

最简单的一个c#例子

usingSystem;//intonamespace

classyy{//thereisnot()

staticvoidMain(){//rememberthefirstwordofmainisbig

Console.WriteLine("hellp");

Console.Read();//remainthewindowopen

}

}

数组

动态数组,可以添加新数据(注意要引入一个名词空间sysystem.connections)

∙认识C#的ArrayList

∙使用C#的ArrayList

∙C#中List用法

C#不支持动态数组,用ArrayList可以实现动态数组的功能。

ArrayList的名称空间是System.Collections。

ArrayList元素类型

和数组不同,ArrayList的各个元素的类型可以不同。

声明对象

//声明ArrayList有三种重载方法,较常用的有两种

ArrayListal=newArrayList();

ArrayListal=newArrayList(3);

上例中,参数值3表示容量,即可以容纳多少个元素。

Capacity与Count

ArrayList具有Capacity和Count属性,分别表示容量和数量。

∙Capacity表示ArrayList可包含的元素数;Count表示实际包含的元素数。

∙Capacity可读可写;Count只读。

当Capacity不够时

我们的Capacity如果小了,无法容纳我们实际的元素个数,这时再增加元素时:

如果指定了Capacity,Capacity将按指定的Capacity的倍数增长(如果Capacity 小于2,将按2的倍数增长),比如指定了Capacity为3,那么将按3、6、9、12、15、18……线性的方式增长,直到可以容纳实际的元素个数。

如果没有指定Capacity值,Capacity将按2、4、8、16、32、64 ……指数的方式增长,直到可以容纳实际的元素个数。

Capacity不会自动缩小

当Capacity被自动增大后,即使删除了某些元素,Capacity也不会自动缩小,需要设置Capacity来缩小,注意Capacity不能小于当前Count

二维数组

数组的数组

二维数组赋值并显示

命名空间的使用

命名空间的有点

可以用A.A1.A2形成层次结构

命名空间的别名

用动态数据获取数据库数据,用控制台代码连接数据库

usingSystem;

usingSystem.Collections;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Linq;

usingSystem.Data.Odbc;

classyy{

//thereisnot()

staticvoidMain(){

ArrayListusername=newArrayList();

//connecttothedatabase

stringMyConString="DRIVER={MySQLODBC5.1Driver};"+

"SERVER=www.ebjtu.org;"+

"DATABASE=ebjtu;"+

"UID=ebjtu;"+

"PASSWORD=123456;"+

"OPTION=3";

OdbcConnectionMyConnection=newOdbcConnection(MyConString);

MyConnection.Open();

//ifconnected

Console.WriteLine("ok\n");

//以°?

下?

是º?

插?

入¨?

到Ì?

数ºy据Y库a

/*stringquery="SELECTusernameFROMpw_memberslimit10";

OdbcCommandcmd=newOdbcCommand(query,MyConnection);

//处ä|理¤¨ª异°¨¬常¡ê:

êo插?

入¨?

重?

复¡ä记?

录?

有®D异°¨¬常¡ê

try{

cmd.ExecuteNonQuery();

}

catch(Exceptionex){

//Console.WriteLine("recordduplicate.");

}finally{

cmd.Dispose();

}

*/

//以°?

下?

是º?

查¨¦询¡¥数ºy据Y库a

stringtmp1=null;

Stringquery="SELECTusernameFROMpw_memberslimit10";

OdbcCommandcmd2=newOdbcCommand(query,MyConnection);

OdbcDataReaderreader=cmd2.ExecuteReader();

while(reader.Read())

{

tmp1=reader[0].ToString();

username.Add(tmp1);//arraylist

}

foreach(stringstrinusername){

Console.WriteLine(str);

}

Console.WriteLine();

Console.ReadLine();

}

//Console.ReadLine();//remainthewindowopen

}

C#自定义浏览器导向

try

{

stringstrurl;

strurl="http:

//www.ebjtu.org/website/";

this.webBrowser1.Url=newSystem.Uri(strurl,System.UriKind.Absolute);

}

catch{}

c#打开连接

System.Diagnostics.Process.Start("

判断是否电脑是否联网

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

返回当前时间

DateTime.Now;

共享变量

三种传递参数形式

数组传值

两种数据类型

字符串是引用类型,但是在函数中不会改变它的值

判断中文

面向对象学习

编译成DLL文件

构造器无返回值,也不能有返回值

构造函数的继承

usingSystem;

classA{

publicA(){

Console.WriteLine("thisAstruct");

}

}

classB:

A{

publicB(){

Console.WriteLine("thisBstruct");

}

}

classy{

publicstaticvoidMain(){

Ba=newB();

Console.Read();

}

}

B始终会调用A的无参构造器

当A有一个有参数的构造器时候,就不会自动产生无参数的构造器

静态构造函数无参数,同时不能被重载

首先调用静态构造器然后调用非静态构造器

析构

访问控制修饰符

利用timer制作滚动字幕

privatevoidtimer1_Tick(objectsender,EventArgse)

{

label2.Left=label2.Left-1;

if(label2.Right<0){

label2.Left=200;

}

}

连接远程mysql数据库代码

usingSystem;

usingSystem.Data.Odbc;

usingSystem.Data.OleDb;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespacemysql

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

try

{

stringMyConString="DRIVER={MySQLODBC5.1Driver};"+

"SERVER=www.ebjtu.org;"+

"DATABASE=ebjtu;"+

"UID=ebjtu;"+

"PASSWORD=123456;"+

"OPTION=3";

OdbcConnectiondbconn=newOdbcConnection(MyConString);

if(System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())

{

dbconn.Open();

if(dbconn.State==ConnectionState.Open)

{

//操¨´作Á¡Â数ºy据Y

Stringquery="SELECT*FROMpw_members";

OdbcCommandcmd=newOdbcCommand(query,dbconn);

OdbcDataAdaptersda=newOdbcDataAdapter();

sda.SelectCommand=cmd;

DataSetds=newDataSet();

sda.Fill(ds,"pw_members");

dataGridView1.DataSource=ds.Tables[0];

//读¨¢取¨?

数ºy据Y

//OleDbDataAdaptersda=newOleDbDataAdapter();

//sda.SelectCommand=cmd;

//DataSetds=newDataSet();

//sda.Fill(ds,"student");

//dataGridView1.DataSource=ds.Tables[0];

//关?

闭À?

}

dbconn.Dispose();

}

}

catch{

MessageBox.Show("数ºy据Y库a没?

有®D连¢?

接¨®上¦?

ê?

");

}

}

privatevoidForm1_Load(objectsender,EventArgse)

{

label1.Text=System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().ToString();

}

}

}

C#连接access数据库代码

//定¨义?

数簓据Y库a连?

接ó

//连?

接óaccess数簓据Y库a

//1.绝?

对?

路·径?

连?

接ó

OleDbConnectiondbconn=newOleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Datasource=C:

\\student.mdb");

dbconn.Open();

//操ù作痢?

数簓据Y

OleDbCommandcmd=dbconn.CreateCommand();

cmd.CommandText="select*fromstudent";

cmd.CommandType=CommandType.Text;

OleDbDataReaderreader=cmd.ExecuteReader();

//读á取?

数簓据Y的?

方?

法ぁ?

//首骸?

先è要癮读á一?

行D,?

然?

后ó才?

能ü把?

这a一?

行D的?

数簓据Y显?

示?

出?

来ぁ?

//reader.Read();

//textBox1.Text=reader.GetString

(1);

while(reader.Read())

{

textBox1.Text=reader.GetString

(1)+textBox1.Text;

}

//关?

闭?

reader

reader.Close();

//关?

闭?

数簓据Y库a的?

连?

接ó

dbconn.Close();

属性学习

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2