Chinaunix首页 | 论坛 | 博客
  • 博客访问: 500368
  • 博文数量: 225
  • 博客积分: 2175
  • 博客等级: 大尉
  • 技术积分: 2443
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-05 22:02
个人简介

目前在一家公司担任软件总监,主要涉及智能手机,笔记本电脑的开发

文章分类

全部博文(225)

文章存档

2024年(5)

2023年(68)

2022年(13)

2021年(7)

2020年(11)

2019年(3)

2018年(10)

2017年(8)

2012年(7)

2011年(4)

2010年(32)

2009年(41)

2008年(6)

2007年(9)

分类: WINDOWS

2010-04-20 07:24:48

C# 是一门microsoft专门为.net 制定的语言。 相当于Java , 但因为当年和Sun的官司, Mircosoft不再发展java ,转而制定了自己的C# .
难度介乎VB 和 C++之间 , 做界面和VB一样简单,但控制力要比VB强,同时,它克服了很多C++不好的特性,比如,对于C++,我们经常会
面临内存泄露,变量没初始化等,我们花费很多时间,在找这样的bugs , C#就没有这样的问题。同时,C#只支持类的{BANNED}中国第一继承,这样简化了
类的关系图,而对于接口,可以多重继承 。
当然在效率方面,会比C++稍差,但在PC上,由于存在JIT (在wince/windows mobile下不存在JIT,因为JIT会让代码的空间变大,使得效率大大
提高了。JIT是边执行,边翻译,一次翻译一定的量
C# 产生的中间代码(IL),通过JIT ,转化为native code , 给CLR调用 。
其架构为:
  windows Forms , Web Service , Web form
 ========================================================
        Data and XML classes (ADO.net ,XPath ...)
    -------------------------------------------
            Framework Base Class (IO , String , Net ....)
    ------------------------------------
             CLR (公共语言运行时)
 
问题1 : C# 主要编写的应用有console , windows form , web form , web service ,他们具体是什么?
console : 控制台程序,输入输出为Console .
windows form : Windows Applications,对于GUI的程序,为这类。
web form :  就是asp , 相当于PHP等,在服务器端自动产生html,用browser访问
web service : 服务,客户端可以是windows form ,也可以是Browser ,来调用其服务。
当然,还有其他的,比如产生DLL等等。

问题2 : 在C#中DLL和以前的有什么不同?
对于以前的COM DLL ,都是在注册表里登记的,这导致注册表很混乱, 以前的DLL会出现版本的问题,比如程序A用的DLLB 版本为1.0 ,程序C用的DLLB为2.0 ,
当安装程序C的时候,可能导致A不可以用了,因为DLLB变了
在C#中,对于private assemble , 把私有的dll 放在合适的目录就可以了(发布的时候,私有的DLL放在当前目录下面就可以了)。
对于share dll,引入了数字签名和Version .在C# 中,DLLB 1.0 2.0 可以共存
对于DLL的引用,在Visual Studio的菜单中选择Add Reference
问题3: 什么是数字签名 ?
原理就是一对key , public- private pair ,
用public 加密的文件 只能用private key打开,这样就算是加密的人都打开不了
用private key加密的文件,只能用public key 打开,public key 是公开的,这样导致加密的文件对任何人都available , 但保证了该文件只能是你所创建的(因为只有你才有private
key) 这就叫数字签名 .
问题4 : 我把Form1.cs [Design]窗口close了,找不到了,不知道怎么再打开,以修改界面。
 在Solution Explorer 窗口,选择Form1.cs , 右击 ,选择 View Designer , 又可以看到 界面了 。
 
问题5 : VS 2005 ,打不开VS 2008所创建的工程,怎么办?
  用新的版本Visual Studio 去打开老的工程,工程会自动升降,比如用Visual Studio 2005 去打开Visual Studio 2003工程,工程会被自动升级
  但我目前安装的是VS 2005 ,打不开VS 2008所创建的工程,怎么办? 提示:
The selected file is a solution file, but was created by a newer version of this application and cannot be opened.
  软件都是向下兼容的,没有向上兼容的。因为有些新特性2005没有
  只有重新建立一个工程,然后把代码copy过去。
  {BANNED}最佳好的办法是下载一个VS 2008
 
问题6 :对象的回收释放不是立即进行的,和C++不一样,如果要立即释放,调用Dispose
            FileStream fileStream = null;
            if (!File.Exists(@"c:\delete\hello.txt"))
            {
                fileStream = File.Create(@"c:\delete\hello.txt");
                fileStream.Close();
            }
           File.Copy(@"c:\delete\hello.txt", @"c:\delete\test2.txt", true);
出现异常 System.IO.IOException: The process cannot access the file 'c:\delete\test.txt'
because it is being used by another process.
这样也不可以
if (!File.Exists(@"c:\delete\hello.txt"))
            {
                File.Create(@"c:\delete\hello.txt");
               
            }:
           File.Copy(@"c:\delete\hello.txt", @"c:\delete\test2.txt", true);

修改为:
FileStream fileStream = null;
            if (!File.Exists(@"c:\delete\hello.txt"))
            {
                fileStream = File.Create(@"c:\delete\hello.txt");
                fileStream.Close();
                fileSteam.Dispose() ;
            }:
           File.Copy(@"c:\delete\hello.txt", @"c:\delete\test2.txt", true);
这样,让fileStream 对象马上释放 。
或者:
            if (!File.Exists(@"c:\delete\hello.txt"))
            {
               FileStream fileStream = null;
                fileStream = File.Create(@"c:\delete\hello.txt");
                fileStream.Close();
             }://dispose will call here automatically
           File.Copy(@"c:\delete\hello.txt", @"c:\delete\test2.txt", true);
问题7 : 对于Stream , Close 和Dispose 区别是什么?
There is one major difference between calling the Close and Dispose methods on database connections. Close leaves the connection in a closed state; but, it is reusable
—all properties, etc. can be accessed and Open can be called. On the other hand, after calling Dispose on a database connection—as with any object—, the connection
object can no longer be accessed.
However, calling Dispose does not remove the connection from the connection pool.
Visual C# Best Practice
Only call the Close method on a Stream or a database connection if the object will be reused. Otherwise, use the Dispose method.
The Dispose method may be called on any instance of any type implementing the IDisposable interface. This is supported by the C# using statement which makes calling
Dispose automatically easy.

对于FileStream , Dispose 其实就是调用Close();
 

问题8 :对象的析构
   string createString()
  {
    string str1 = " haha " ;
    return str1 ;
  
  }
以C++的观点,上述代码是错误的,因为返回的是local对象,在退出函数的时候会析构
而在C#中,却是正确的。
 
 

问题9: 对有些Proterity 进行设置,会导致事件的发生,
 比如,在一个TreeView 中,其AfterCheck 事件的处理函数
 private void tvwSource_AfterCheck(object sender, TreeViewEventArgs e)
        {
          e.Node.Checked = false ; //又触发了事件...
        }
会导致无限循环 ,
问题10 : Proterity  和 Attribute ?
  翻译起来都是属性,但其实完全不一样
proterity 是指为了封装class 的数据成员,不让外界直接访问其内部变量。可以通过proterity来访问,达到数据变量的封装 。
Attribute 是指 metadata 元数据,对类等的描述 。

提示1:
对于 string , 在前面加上@ , 比如 @"c:\delete\hello"  == "c:\\delete\\hello"
提示2: C#的编译器好智能
有时候觉得C#编译器有点神奇,比如下面的代码
            TcpClient clientForTest;
            try
            {
                clientForTest = new TcpClient("localhost", 3456);
            }
            catch
            {
                Console.WriteLine("Failed to connect to the server ");
               
            }
            NetworkStream netStream = clientForTest.GetStream();
编译出错,
error CS0165: Use of unassigned local variable 'clientForTest
百思不得其解的时候,发现
TcpClient clientForTest;
            try
            {
                clientForTest = new TcpClient("localhost", 3456);
            }
            catch
            {
                Console.WriteLine("Failed to connect to the server ");
                return ;
               
            }
            NetworkStream netStream = clientForTest.GetStream();
就可以了,
原来,如果出现异常的时候,不return ,则下面继续用clientForTest 则会出错。。。
提示3:
在C#中, 有一个 System.Configuration 来支持应用程序配置文件,相当于以前的INI文件。
对于这样的一个配置文件,可以通过
  Add --> New Item .. --> Application Configuration Files
该文件主要有2个区:appSettings 和connectionStrings
然后再应用程序里直接访问, 用ConfigurationManager.AppSettings[...]
非常方便 。
提示4:
C#的clickOnce 来发布你的程序,实现在线升级 。
 
阅读(2143) | 评论(0) | 转发(1) |
0

上一篇:Fltk 的回忆总结

下一篇:printk 的显示问题

给主人留下些什么吧!~~