在.NET 2005开发环境中,依次选择“新建”/“项目”/“控制台应用程序”,新建一个控制台应用程序。
数据类型、类型转换及表达式
1
、枚举类型
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
//
代码演示了如何使用位域和传统枚举
namespace EnumTest
{
public enum SomeRootVegetables
{
HorseRadish,
Radish,
Turnip
}
[Flags]
public enum Seasons
{
None = 0,
Summer = 1,
Auturmn = 2,
Winter = 4,
Sprint = 8,
All = Summer | Auturmn | Winter | Sprint,
}
class Program
{
static void Main(string[] args)
{
Hashtable AvailableIn = new Hashtable();
AvailableIn[SomeRootVegetables.HorseRadish] = Seasons.All;
AvailableIn[SomeRootVegetables.Radish] = Seasons.Sprint;
AvailableIn[SomeRootVegetables.Turnip] = Seasons.Sprint | Seasons.Auturmn;
//
声明一个Seasons类型的数组,使用枚举值
Seasons[] seasons = new Seasons[] { Seasons.Winter, Seasons.Sprint, Seasons.Summer, Seasons.Auturmn };
//
将结果显示出来
for (int i = 0; i < seasons.Length; i++)
{
Console.WriteLine("/r/nThe following root vegetables are harvested in " + seasons[i].ToString("G") + ":");
foreach (DictionaryEntry e in AvailableIn)
{
if (((Seasons)e.Value & seasons[i]) > 0)
Console.WriteLine("/t" + ((SomeRootVegetables)e.Key).ToString("G"));
}
}
}
}
}
2
、object类型
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示了Object类型的变量如何接受任何数据类型的值及Object类型的变量如何在.NET Framework中使用Object的方法
namespace ObjectTest
{
class Program
{
static void Main(string[] args)
{
object a;
a = 1;
Console.WriteLine(a);
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
Console.WriteLine();
a = new SampleClass();
SampleClass classRef;
classRef = (SampleClass)a;
Console.WriteLine(classRef.i);
}
}
class SampleClass
{
public int i = 10;
}
}
3
、算术操作符
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示如何使用算术操作符
namespace SuanshuExample
{
class Program
{
static void Main(string[] args)
{
int n = 10, m = 3;
float f = 5.0F, g = 10.0F;
double d = 5.0, e = 10.0;
Console.WriteLine("
算术操作符运行示例:");
Console.WriteLine("n={0},m={1}", n, m);
Console.WriteLine("f={0},g={1}", f, g);
Console.WriteLine("d={0},e={1}", d, e);
Console.WriteLine("n+m={0},n-m={1},n*m={2},n/m={3},n%m={4}", n + m, n - m, n * m, n / m, n % m);
Console.WriteLine("f+g={0},f-g={1},f*g={2},f/g={3}", f + g, f - g, f * g, f / g);
Console.WriteLine("d+e={0},d-e={1},d*e={2},d/e={3}", d + e, d - e, d * e, d / e);
Console.WriteLine("n+m-f*g/d={0}", n + m - f * g / d);
Console.WriteLine("n%m*f*d={0}", n % m * f * d);
}
}
}
4
、赋值操作符运行示例
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示了如何使用赋值操作符
namespace FuzhiExample
{
class FuzhiExample
{
static void Main(string[] args)
{
int a = 5, b = 3;
Console.WriteLine("
赋值操作符运行示例:");
Console.WriteLine("a={0},b={1}", a, b);
Console.WriteLine("b=a+b={0}", b = a + b);
Console.WriteLine("b=a*b={0}", b *= a);
Console.WriteLine("b=b%a={0}", b %= a);
Console.WriteLine("a=a>>=2={0}", a >>= 2);
Console.WriteLine("a=a<<=2={0}", a <<= 2);
Console.WriteLine("a=a^=a={0}", a ^= a);
Console.WriteLine(6^ 5);
}
}
}
5、关系操作符
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示了如何使用关系操作符
namespace GuanxiExample
{
class GuanxiExample
{
static void Main(string[] args)
{
int i = 1, j = 2;
char c1 = 'A', c2 = 'a';
Console.WriteLine("
关系操作符运行示例:");
Console.WriteLine("i={0},j={1}", i, j);
Console.WriteLine("c1={0},c2={1}", c1, c2);
Console.WriteLine("i<j is :{0},i>=j is :{1},i<=j is :{2}", i > j, i >= j, i <= j);
Console.WriteLine("c1>c2 is :{0},c1>=c2 is :{1},c1<=c2 is :{2}", c1 > c2, c1 >= c2, c1 <= c2);
}
}
}
6、逻辑操作符
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示了如何使用逻辑操作符
namespace LuojiExample
{
class LuojiExample
{
static void Main(string[] args)
{
int x=3,y=5,a=2,b=3;
Console.WriteLine("
逻辑操作符运行示例:");
Console.WriteLine("x={0},y={1},a={2},b={3}",x,y,a,b);
Console.WriteLine("a>b&&x<y={0}",a>b&&x<y);
Console.WriteLine("!(a>b)&&!(x<y)={0}",!(a>b)&&!(x<y));
Console.WriteLine("!(a>x)||!(b<y)={0}", !(a > x) || !(b < y));
}
}
}
7、位操作符
using System;
using System.Collections.Generic;
using System.Text;
//
代码演示了将两个数值分别进行“&”、“|”、“^”运算
namespace WeiExample
{
class WeiExample
{
static void Main(string[] args)
{
int y = 150, x = 110;
Console.WriteLine("
位操作符运行示例:");
Console.WriteLine("x={0},y={1}", x, y);
Console.WriteLine("x&y={0},x|y={1},x^y={2}", x & y, x | y, x ^ y);
Console.WriteLine("x>>2={0}", x >> 2);
Console.WriteLine("x<<2={0}", x << 2);
}
}
}