一 数据类型
值类型
--简单类型(整型(sbyte,byte,short,ushort,int,uint,long,ulong)
,布尔,浮点型(实数型)(float,double,demcimal),字符型)
--结构类型(struct)
--枚举类型(enum)
引用类型
--class
--接口
--delegate(代理方法,他有些重载运算符 + - )
--array
编码风格:简单变量用驼峰,高级命名用双大写,叫PascalCase风格(如:CamelCase)
谈谈c#中的类与java相比
1. 多了析构函数,这个与c++类似
2. 多了属性 用处:我觉得有点像数据库的字段的约束条件,不能接收参数,命名风格:大多是PascalCase风格
3. 少了个final 有个constant关键字,相对于对应常量。同时static关键字也有些与java不一样,
C#中static只是说是否实例化,具体可以百度
4. 多了个索引 和属性使用方式类似,用处:可以通过逻辑用数组的访问方式访问类中的成员变量
索引例子:
using System;
namespace test
{
class Photo //声明照片类
{
string _title;
public Photo(string title)
{
this._title = title;
}
public string Title
{
get
{
return _title;
}
}
}
class Album //声明相册类
{
Photo[] photos; //该数组用于存放照片
public Album(int capacity)
{
photos = new Photo[capacity];
}
public Photo this[string title]
{
get
{
foreach (Photo p in photos) //遍历数组中的所有照片
{
if (p.Title == title) //将照片中的标题与索引器参数进行比较
return p;
}
Console.WriteLine("未找到"); //使用null指示失败
return null;
}
}
public Photo this[int index]
{
get
{
if (index < 0 || index >= photos.Length) // 验证索引范围
{
Console.WriteLine("索引无效");
return null; // 使用null指示失败
}
return photos[index]; // 对于有效索引,返回请求的照片
}
set
{
if (index < 0 || index >= photos.Length)
{
Console.WriteLine("索引无效");
return;
}
photos[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Album album = new Album(5);
album[0] = new Photo("图片1");
album[1] = new Photo("图片2");
album[2] = new Photo("图片3");
for (int i = 0; i < 3; i++)
{
Console.WriteLine(album[i].Title);
}
Console.WriteLine(album["图片1"]);
Console.ReadKey();
}
}
}
二 流程控制
略(与java没什么不同)
附加:快捷键vs2019 手动打出for按2次table键
三 异常处理
略(同java类似)
注:cookies和session大家虽然都听过,也理解,但没实操,所以原理就不说了
上面是控制台应用程序可以直接看效果,接下来说说 window Gui(即:winform)
winform学习就是学习组件使用,了解组件的属性和事件,如:textbox
然后是web sevice,和上一篇类似,不过C#比vb好多了,语法用的都舒服多了,这里就不说了
附录:
事件:
定义事件函数(比如一个list)==》定义一个delegate(其实就是下一步的需要把该事件触发调用的方法进行绑定) ==》new 出来这个delegate,绑定事件函数==》例子:当list改变是会触发该事件
using System;
using System.Collections;
namespace MySpace
{
// A delegate type for hooking up change notifications.
public delegate void ChangedEventHandler(
object sender,
string description);
}
// A class that works just like ArrayList, but sends event
// notifications whenever the list changes.
public class ListWithChangedEvent : ArrayList
{
// An event to notify clients a change.
public event MySpace.ChangedEventHandler Changed;
// Invoke the Changed event
// Called by Add,Clear and set
private void OnChanged(string description)
{
if (Changed != null)
Changed(this, description);
}
// Override Add, invoke event Changed
public override int Add(object value)
{
int i = base.Add(value);
OnChanged("\"" + value.ToString()
+ "\" has been added to the list.");
return i;
}
// Override Clear, invoke event Changed
public override void Clear()
{
base.Clear();
OnChanged("List has been cleared.");
}
// Override set, invoke event Changed
public override object this[int index]
{
set
{
base[index] = value;
OnChanged("List value has been changed to \""
+ value.ToString() + "\".");
}
}
}
class App
{
// Test the ListWithChangedEvent class.
public static void Main()
{
// Create a new list.
ListWithChangedEvent list = new ListWithChangedEvent();
// Attach event handler to this list
Console.WriteLine("Event handler attached");
MySpace.ChangedEventHandler handler =
new MySpace.ChangedEventHandler(ListChanged); ;
list.Changed += handler;
// Add and remove items from the list.
Console.Write("Adding an item ...");
list.Add("item 1");
Console.Write("Changing an item ...");
list[0] = "item 3";
Console.Write("Accessing an item ...");
Console.WriteLine("list[0] == {0}", list[0]);
Console.Write("Clearing items ...");
list.Clear();
Console.WriteLine();
Console.WriteLine("Event handler deattached");
list.Changed -= handler;
Console.WriteLine("Adding an item ...");
list.Add("item 1");
Console.WriteLine("Program finished");
}
private static void ListChanged(object sender, string description)
{
Console.WriteLine("Event fired: " + description);
}
}