c# 基础语法总结(实例)

这位仁兄总结的很详细:
http://www.deepcast.net/wiki/ow.asp?CSharp%D3%EF%B7%A8%BB%F9%B4%A1

其博客文章地址:http://www.cnblogs.com/nightlast/archive/2005/07/21/197252.html

 

本文包含了:C#预定义类型,语句,运算符,类型转换,类,结构,接口的基本定义,不涉及类,结构,接口的操作。
对应C#高级编程(第6版)的第2,3,4,5章内容

 

下面是例子:

创建BasicGrammer类:

 

具体代码:

 

Code
/*
 * 使用using关键字表示包含其它的类库。
 * 比如:using System。表示把System命名空间(namespace)下的所有的类包含进本程序,
 * 之后我们就可以使用这些类了。
 * 命名空间解释详细参考:
http://msdn.microsoft.com/zh-cn/library/0d941h9d(VS.80).aspx
 * 
 * 如何查找System类库或者其它类库的详细资料? 
 * 参见:
http://msdn.microsoft.com/zh-cn/library/ms229335(VS.80).aspx (.Net Framework 2.0版本)
 * 
 
*/
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

//我们自己定义的命名空间
namespace TestConsole
{
    
/*
     * Custom Class(自定义类)
     * class(类)介绍:
http://msdn.microsoft.com/zh-cn/library/x9afc042(VS.80).aspx
     
*/
    
public class BasicGrammar
    {
        
/*
         * C#预定义类型,同时也是class(类)的field(字段)定义方式
         
*/

        
//整型
        private int _int = 1;
        
/*
         * 同时,"private int _int = 1;"也是class(类)的field(字段)定义方式,
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms173118(VS.80).aspx
         
*/

        
//双精度
        private double _double = 2.31;

        
//浮点型,末尾增加一个f表示浮点型
        private float _float = 3.22f;

        
//字符型,只能包含一个字符,用单引号定义。
        private char _char = 'a';

        
//字符串
        private string _str = "Hello";

        
//bool(布尔)型,真假值,bool型只有2个值:true和false;
        private bool _bool = true;
        
//private bool _boolFalse = false; 

        
/*
         * class(类)的property(属性)定义方式
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/x9fsa0sw(VS.80).aspx
         
*/
        
public int MyInt
        {
            
set { _int = value; }
            
get { return _int; }
        }

        
/*
         * class(类)的method(方法)定义方式 - 无返回值
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms173114(VS.80).aspx
         
*/
        
public void NoReturns()
        {
            Console.WriteLine(
"无返回值方法");
        }

        
/*
         * class(类)的method(方法)定义方式 - 有返回值
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms173114(VS.80).aspx
         
*/
        
public int HaveReturns()
        {
            Console.WriteLine(
"有返回值演示:");
            
            
//返回MyInt的值。
            return MyInt;
        }

        
/*
         * class(类)的method(方法)定义方式 - 有返回值
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms173114(VS.80).aspx
         
*/
        
public int HaveReturnsAndParams(string args)
        {
            Console.WriteLine(
"有返回值演示:");
            
            
//输出传入的值。
            Console.WriteLine(args);

            
//返回MyInt的值。
            return MyInt;
        }

        
/*
         * 构造函数
         * 详细解释:
http://msdn.microsoft.com/zh-cn/library/ace5hbzh(VS.80).aspx
         
*/
        
public BasicGrammar()
        {
            MyInt 
= 2;
        }


        
/*
         * 值类型(Value Type)和引用(Reference Type)类型说明
         * 参见:
http://www.cnblogs.com/zwq194/archive/2008/11/22/1339083.html
         * 
         * 同时值传递和引用传递区别:
         * 值传递和来个通俗例子:
         * 比如我有100¥,我直接给张三100¥(值传递),
         * 换种方式,我把钱放到某个屋子的柜子里,
         * 并告诉张三具体地址,张三到这个地址来取钱(引用传递)
         * 参见:
http://www.cnblogs.com/shiyulun1984/archive/2008/11/01/1324312.html
         
*/

        
/*
         * 类型转换和强制类型转换
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms173105(VS.80).aspx
         
*/
        
public void CastType()
        {
            
//把double类型转换成int型
            MyInt = (int)_double;
            Console.WriteLine(MyInt);
//输出2
        }

        
/*
         * boxing(装箱)和拆箱(unboxing)
         * 其主要作用是在值类型和引用类型间转换.
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/yz2be5wk(VS.80).aspx
         
*/
        
public void BoxAndUnbox()
        {
            
int i = 2;
            
object objectI = (object)i; //装箱

            
            
try
            {
                i 
= (int)objectI; //拆箱
                Console.WriteLine("拆箱成功");
            }
            
catch (InvalidCastException ex) //异常,放在后面的文章中总结。
            {
                System.Console.WriteLine(
"{0} 错误: 拆箱失败.", e.Message);

                
//btw:“System.Console.WriteLine” 等于 “Console.” 因为Console是System命名空间下的一个类
            }
        }

        
//类型转换高级参考:
        
//使用可控类型:http://msdn.microsoft.com/zh-cn/library/1t3y8s4s(VS.80).aspx

        
/*
         * 数组总结
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/9b9dty7d(VS.80).aspx
         
*/
        
public void SummaryArray()
        {
            
//上面链接msdn的例子很详细,我不就不重复了。
        }

        
/*
         * 字符串操作总结
         * 详细参考:
http://msdn.microsoft.com/zh-cn/library/ms228364(VS.80).aspx
         
*/
        
public void SummaryString()
        {
            
string str1 = "Hello";
            
string str2 = "Baby!";

            
//连接字符串
            string str = str1 + "," + str2;
            Console.WriteLine(str); 
//输出:Hello,Baby!

            
//把str转换成char类型的数组.
            char[] strs = str.ToCharArray();

            
//获取strs这个变量的长度
            int length = strs.Length;

            
for (int i = 0; i < length; i++)
            {
                Console.WriteLine(strs[i]); 
//将按照Hello,Baby!字符串的顺序,一个一个字母输出
            }
        }

        
/*
         * C#语句,运算符,表达式总结
         * 详细解释:
http://msdn.microsoft.com/zh-cn/library/ms173142(VS.80).aspx
         
*/
        
public void Statement()
        {
            
//表达式:其实char[] strs = str.ToCharArray();就是一个表达式。

            
//语句,if..else语句,while(dowhile)语句,for语句,foreach语句,switch语句
            
//比如:
            if (_bool)
            {
                Console.WriteLine(
"语句输出'真(true)'!");
            }
            
else
            {
                Console.WriteLine(
"语句输出'假(false)'!");
            }

            
//运算符,比如上面的:string str = str1 + "," + str2; 
            
//另外还有+,-,*,/,等等。

        }
    }

    
/*
     * struct(结构)和class(类)在定义上没有太大区别。可以这样理解,struct是精简版的class 
     * 但是struct也有自己的一些特点,请参见下面链接。
     * struct详细解释:
http://msdn.microsoft.com/zh-cn/library/saxz13w4(VS.80).aspx
     * 
     * struct 其它人做的解释,可以在google里面输入:“c# struct site:cnblogs.com ”进行查询。
     * 其中“c# struct”是我们要查的关键字,"site:cnblogs.com",限制google只能在cnblogs.com网站查询。
     * btw:cnblogs.com是c#开发的一个基地,上面很多资料哦。
     
*/
    
public struct HelloStruct
    {
        
private string _str;
        
public string Str
        {
            
get { return _str; }
            
set { _str = value; }
        }
    }

    
/*
     * Interface(接口)定义,接口定义时,不需要定义具体的实现。
     * 高级参考:
http://www.cnblogs.com/sifang2004/archive/2006/07/14/450565.html
     
*/
    
public interface HelloInterface
    {
        
void WriteHello(string helloStr);
        
string getHello();
        
int Count();
    }
}

 

posted @ 2008-12-08 15:49  无尽思绪  阅读(877)  评论(2编辑  收藏  举报