C#学习
C#的概念
C#是一种面向对象的编程语言,首先需要知道的是何为面向对象,语言有面向对象与面向过程两种。一切皆为对象。同时,C#是一种优雅的语言,意在以最简洁的代码解决问题。
C#区别于C++
在C#中的输入与输出:Consolove.ReadLine()表示输入一个数值;Consolove.WriteLine()表示输出,注意在C#中输入的默认为字符串,所以想要输入为整型或者是字符类型或者浮点型,eg:int a=int.Prase(Cosolove.ReadLine());在C#输出中,如果想要输出多个字符串或者说是值的话,eg:Consolove.WriteLine(“a={0},b={1}”,a,b);表示{}中为后边的值。 在C#中的数组,链表,因为C#属于面对对象的编程,区别于C,在C里想要输入或者是输出数组,链表,通常都是需要运用循环,但是在C#中可以直接使用List,eg:List list=new List();list.Add(2);表示已新建一个整型数据的链表,链表名为list,添加了第一个元素为2。 C#中的函数的引用,一般在C中使用函数,指针有时候是必不可少的,值传递与无返回值,很多时候都需要用到指针,不过显然在C#中式没有指针的,但是依照个人理解来看,C#中使用的ref在某种意义上就是相当于指针了。因为本人最开始了解的就是C语言了,所以里面很多的指针的内容,取地址还有取值,传参等等就不在此详细解释了。 C#的循环,因为继承了C++的各种方法与类,所以C#中对于for ,while ,if ,switch,do等基本循环都是可以适用的,但是在C#中有一个很好用的循环,就是foreach,eg:int[] arry={0,1,2,3}; foreach(int i in arry) {Consolove.WriteLine(i)};表示的就是打印出arry整个数组里的所有元素,遍历法,foreach中的理解为,in表示在里面,也就是说在arry这个歌数组里面,从i开始,逐渐执行下面的循环,这种循环方法在多位数组中显得格外简洁和快速,但是同时foreach的限制表现为无法进行修改数据。 理解重载。 泛型。我们在写程序的时候,很多时候会有一定的重复,但是一般来说我们都是要避免重复工作的,所以有了泛型。 在C#中使用累加的时候,区别于C的地方在于,C#中有一个params函数。 #话不多说,说多无益,先给大家看看我的第一周做出来的一个小玩意吧,一个简单的记事本。
记事本的开始界面
记事本的运行界面
代码实现
using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
using System. Windows. Forms;
using System. Drawing. Text;
using System. Collections;
using System. IO;
using System. Drawing. Text;
using System. Collections;
using System. IO;
namespace 记事本
{
public partial class 子窗体 : Form
{
public 子窗体( )
{
InitializeComponent ( ) ;
}
private void toolStripTextBox1_LocationChanged ( object sender, EventArgs e)
{
InstalledFontCollection myFonts = new InstalledFontCollection ( ) ;
FontFamily[ ] ff = myFonts. Families;
ArrayList list = new ArrayList ( ) ;
int count = ff. Length;
for ( int i= 0 ; i< count; i++ )
{
string FontName = ff[ i] . Name;
}
}
private void toolStripComboBoxFonts_LocationChanged ( object sender, EventArgs e)
{
InstalledFontCollection myFonts = new InstalledFontCollection ( ) ;
FontFamily[ ] ff = myFonts. Families;
ArrayList list = new ArrayList ( ) ;
int count = ff. Length;
for ( int i= 0 ; i< count; i++ )
{
string FontName = ff[ i] . Name;
toolStripComboBoxFonts. Items. Add ( FontName) ;
}
}
private void toolStripButtonBold_Click ( object sender, EventArgs e)
{
if ( textBoxNote. Font. Bold == false )
{
textBoxNote. Font = new Font ( textBoxNote. Font, FontStyle. Bold) ;
}
else
{
textBoxNote. Font = new Font ( textBoxNote. Font, FontStyle. Regular) ;
}
}
private void toolStripButtonItalic_Click ( object sender, EventArgs e)
{
if ( textBoxNote. Font. Italic== false )
{
textBoxNote. Font = new Font ( textBoxNote. Font, FontStyle. Italic) ;
}
else
{
textBoxNote. Font = new Font ( textBoxNote. Font, FontStyle. Regular) ;
}
}
private void toolStripComboBoxFonts_SelectedIndexChanged ( object sender, EventArgs e)
{
string fontName = toolStripComboBoxFonts. Text;
float fontSize = float . Parse ( toolStripComboBoxSize. Text) ;
textBoxNote. Font = new Font ( fontName, fontSize) ;
}
private void toolStripComboBoxSize_SelectedIndexChanged ( object sender, EventArgs e)
{
string fontName = toolStripComboBoxFonts. Text;
float fontSize = float . Parse ( toolStripComboBoxSize. Text) ;
textBoxNote. Font = new Font ( fontName, fontSize) ;
}
private void toolStripComboBoxSize_TextChanged ( object sender, EventArgs e)
{
string fontName = toolStripComboBoxFonts. Text;
float fontSize = float . Parse ( toolStripComboBoxSize. Text) ;
textBoxNote. Font = new Font ( fontName, fontSize) ;
}
private void toolStripButtonSave_Click ( object sender, EventArgs e)
{
if ( textBoxNote. Text. Trim ( ) != "" )
{
if ( this . Text == "" )
{
saveFileDialog1. Filter = ( "文本文档(*.txt)|*.txt" ) ;
if ( saveFileDialog1. ShowDialog ( ) == DialogResult. OK)
{
string path = saveFileDialog1. FileName;
StreamWriter sw = new StreamWriter ( path, false ) ;
sw. WriteLine ( textBoxNote. Text. Trim ( ) ) ;
sw. Flush ( ) ;
sw. Close ( ) ;
}
}
else
{
string path = this . Text;
StreamWriter sw = new StreamWriter ( path, false ) ;
sw. WriteLine ( textBoxNote. Text. Trim ( ) ) ;
this . Text = path;
sw. Flush ( ) ;
sw. Close ( ) ;
}
}
else
{
MessageBox. Show ( "空文档不能保存!" , "信息提示" , MessageBoxButtons. OK, MessageBoxIcon. Error) ;
}
}
private void toolStripButtonOpen_Click ( object sender, EventArgs e)
{
openFileDialog1. Filter = ( "文本文档(*.txt)|*.txt" ) ;
if ( openFileDialog1. ShowDialog ( ) == DialogResult. OK )
{
string path = openFileDialog1. FileName;
StreamReader st = new StreamReader ( path, Encoding. UTF8) ;
string txt = st. ReadToEnd ( ) ;
textBoxNote. Text = txt;
this . Text = path;
st. Close ( ) ;
}
}
private void toolStripButton1_Click ( object sender, EventArgs e)
{
textBoxNote. Text = "" ;
this . Text = "" ;
}
}
}
以上就是本周的主要内容还有一个记事本的代码实现。