运算符与类型转换 mogondb简介
运算符与类型转换
1.运算符
(1)分类
算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符、其他运算符
>.算术运算符:
| 运算符 | 描述 |
|---|---|
| + | 把两个操作数相加 |
| - | 从第一个操作数中减去第二个操作数 |
| * | 把两个操作数相乘 |
| / | 分子除以分母 |
| % | 取模运算符,整除后的余数 |
| ++ | 自增运算符,整数值增加 1 |
| -- | 自减运算符,整数值减少 1 |
>.关系运算符:
| 运算符 | 描述 |
|---|---|
| == | 检查两个操作数的值是否相等,如果相等则条件为真。 |
| != | 检查两个操作数的值是否相等,如果不相等则条件为真。 |
| > | 检查左操作数的值是否大于右操作数的值,如果是则条件为真。 |
| < | 检查左操作数的值是否小于右操作数的值,如果是则条件为真。 |
| >= | 检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 |
| <= | 检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 |
using System;
namespace study
{
public class demo0_operator
{
static void Main(string[] args)
{
UserControl u1 = new UserControl("1", 2, 3);
UserControl u2 = new UserControl("1", 4, 5);
Console.WriteLine("hashcode of u1 is=" + u1.GetHashCode());
Console.WriteLine("hashcode of u2 is=" + u2.GetHashCode());
Console.WriteLine("u1==u2 is " + (u1 == u2));
//demo string
string s1 = "hello word";
string s2 = "hello word";
string s3 = "hello " + "word";
Console.WriteLine("hashcode of s1 is=" + s1.GetHashCode());
Console.WriteLine("hashcode of s2 is=" + s2.GetHashCode());
Console.WriteLine("hashcode of s3 is=" + s3.GetHashCode());
Console.WriteLine("s1==s2 is " + (s1 == s2));
Console.WriteLine("s1==s3 is " + (s1 == s3));
//运行结果如下:
// hashcode of u1 is=1062342447
// hashcode of u2 is=472133479
// u1==u2 is True
// hashcode of s1 is=-645689584
// hashcode of s2 is=-645689584
// hashcode of s3 is=-645689584
// s1==s2 is True
}
}
public class UserControl
{
public string id;
public int width;
public int height;
public UserControl(string id, int width, int height)
{
this.id = id;
this.width = width;
this.height = height;
}
public static bool operator ==(UserControl lhs, UserControl rhs)
{
return lhs.id == rhs.id;
}
public static bool operator !=(UserControl lhs, UserControl rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
if (obj is UserControl) return false;
return this == (UserControl)obj;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public override string ToString()
{
return "控件id:" + this.id + "宽度" + this.width + ";高度:" + this.height;
}
}
}
>.逻辑运算符:
| 运算符 | 描述 |
|---|---|
| && | 称为逻辑与运算符。如果两个操作数都非零,则条件为真。 |
| || | 称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 |
| ! | 称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 |
>.位运算符:
位运算符作用于位,并逐位执行操作。&、 | 和 ^ 的真值表如下所示
| p | q | p & q | p | q | p ^ q |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
>.赋值运算符:
| 运算符 | 描述 |
|---|---|
| = | 简单的赋值运算符,把右边操作数的值赋给左边操作数 |
| += | 加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 |
| -= | 减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 |
| *= | 乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 |
| /= | 除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 |
| %= | 求模且赋值运算符,求两个操作数的模赋值给左边操作数 |
| <<= | 左移且赋值运算符 |
| >>= | 右移且赋值运算符 |
| &= | 按位与且赋值运算符 |
| ^= | 按位异或且赋值运算符 |
| |= | 按位或且赋值运算符 |
>.其他运算符:
| 运算符 | 描述 |
|---|---|
| sizeof() | 返回数据类型的大小。 |
| typeof() | 返回 class 的类型。 |
| & | 返回变量的地址。 |
| * | 变量的指针。 |
| ? : | 条件表达式 |
| is | 判断对象是否为某一类型。 |
| as | 强制转换,即使转换失败也不会抛出异常。 |
using System;
namespace study
{
public class demo2_reflection
{
private string currentType = typeof(demo2_reflection).ToString();
private string value;
public demo2_reflection(string value)
{
this.value = value;
}
public void showName()
{
Console.WriteLine("currentType is " + currentType);
Console.WriteLine("value=" + this.value);
}
static void Main(string[] args)
{
Type t = Type.GetType("study.demo2_reflection");
Console.WriteLine("type of t is "+t.GetType().ToString());
Object[] constructParms = new object[] {"hello word"}; //构造器参数
demo2_reflection obj = (demo2_reflection)Activator.CreateInstance(t, constructParms);
obj.showName();
}
}
}
2.运算符优先级
| 类别 | 运算符 |
|---|---|
| 后缀 | () [] -> . ++ - - |
| 一元 | + - ! ~ ++ - - (type)* & sizeof |
| 乘除 | * / % |
| 加减 | + - |
| 移位 | << >> |
| 关系 | < <= > >= |
| 相等 | == != |
| 位与 AND | & |
| 位异或 XOR | ^ |
| 位或 OR | | |
| 逻辑与 AND | && |
| 逻辑或 OR | || |
| 条件 | ?: |
| 赋值 | = += -= *= /= %=>>= <<= &= ^= |= |
| 逗号 | , |
2.类型转换
C#里的类型转换
(1)隐式转换
从类型A到类型B的转换可以在所有情况下进行,执行转换的规则非常简单,可以让编译器执行转换。
隐式转换不需要做任何工作,也不需要另外编写代码。如将int型数据转换成double型数据:
int a = 10; double b = a;//隐式转换
(2)显式转换
从类型A到类型B的转换只能在某些情况下进行,转换规则比较复杂,应进行某种类型的额外处理。显式转换又叫强制类型转换,显式转换需要用户明确的指定转换类型。如将double类型数据转换成int类型数据:
double c = 10.5; int d = (int)c;//显示转换
(3)通过方法进行类型转换
| 序号 | 方法 & 描述 |
|---|---|
| 1 | ToBoolean 如果可能的话,把类型转换为布尔型。 |
| 2 | ToByte 把类型转换为字节类型。 |
| 3 | ToChar 如果可能的话,把类型转换为单个 Unicode 字符类型。 |
| 4 | ToDateTime 把类型(整数或字符串类型)转换为 日期-时间 结构。 |
| 5 | ToDecimal 把浮点型或整数类型转换为十进制类型。 |
| 6 | ToDouble 把类型转换为双精度浮点型。 |
| 7 | ToInt16 把类型转换为 16 位整数类型。 |
| 8 | ToInt32 把类型转换为 32 位整数类型。 |
| 9 | ToInt64 把类型转换为 64 位整数类型。 |
| 10 | ToSbyte 把类型转换为有符号字节类型。 |
| 11 | ToSingle 把类型转换为小浮点数类型。 |
| 12 | ToString 把类型转换为字符串类型。 |
| 13 | ToType 把类型转换为指定类型。 |
| 14 | ToUInt16 把类型转换为 16 位无符号整数类型。 |
| 15 | ToUInt32 把类型转换为 32 位无符号整数类型。 |
| 16 | ToUInt64 把类型转换为 64 位无符号整数类型。 |
using System;
namespace study
{
public static class typeConversion
{
public static string timeToString(DateTime value)
{
return value.ToString("yyyy-MM-dd HH:mm:ss fff");
}
public static string timeToStringEx(this DateTime value)
{
return value.ToString("yyyy-MM-dd HH:mm:ss fff");
}
static void Main(string[] args)
{
DateTime now=DateTime.Now;
Console.WriteLine("timeToString="+typeConversion.timeToString(now));
Console.WriteLine("timeToStringEx="+now.timeToStringEx());
}
}
}
using System;
namespace study
{
public class typeConversion
{
static IUserControl getInstance(string controlName)
{
switch (controlName)
{
case "TextBox":
return new User_TextBox();
case "Dropdown":
return new User_Dropdown();
default:
return new User_TextBox();
}
}
static void Main(string[] args)
{
IUserControl contorl1 = new User_TextBox();
IUserControl contorl2 = new User_Dropdown();
Console.WriteLine("type of contorl1" + contorl1.GetType().ToString());
Console.WriteLine("type of contorl2" + contorl2.GetType().ToString());
IUserControl contorl3 = (IUserControl)contorl2;
Console.WriteLine("type of contorl3" + contorl3.GetType().ToString());
IUserControl contorl4 = getInstance("TextBox");
IUserControl contorl5 = getInstance("Dropdown");
Console.WriteLine("type of contorl4" + contorl4.GetType().ToString());
Console.WriteLine("type of contorl5" + contorl5.GetType().ToString());
}
}
public interface IUserControl
{
void getControl();
void checkdata();
void resetControl();
}
public class User_TextBox : IUserControl
{
public void checkdata()
{
Console.WriteLine("checkdata is User_TextBox");
}
public void getControl()
{
Console.WriteLine("getControl is User_TextBox");
}
public void resetControl()
{
Console.WriteLine("resetControl is User_TextBox");
}
}
public class User_Dropdown : IUserControl
{
public void checkdata()
{
Console.WriteLine("checkdata is User_Dropdown");
}
public void getControl()
{
Console.WriteLine("getControl is User_Dropdown");
}
public void resetControl()
{
Console.WriteLine("resetControl is User_Dropdown");
}
}
}
mogondb简介
MongoDB是一款强大、灵活,且易扩展的通用型数据库,其包含以下设计特点
1.1易于使用
与关系型数据库不同的是,它没有table/rows/records,相反采用更为灵活的文档(document);另外不在预定义模式,即没有数据类型、长度的限制,文档的key跟value不再受这些约束,即添加、删除更容易
1.2易于扩展
MongoDB采用横向扩展,即横向扩展机器。顺带提一下,纵向扩展是加大单机的配置,但始终会出现单机负载不了业务的时候。那横向扩展只需要向集群添加新机器,Mogondb就会自动将现有数据向新服务器传送
1.3丰富的功能
处理能够创建、读取、更新、删除以外,还提供其他不断扩展的功能:索引、聚合、特殊的集合类型(如某个时刻过期的sesssion、也支持固定大小的集合,用于保存近期的数据,如日志)、文件存储
但mongodb并不具备存在于关系型数据中的一些功能,如:jon和复杂的事务,因为在分布式的系统中这两个功能难以高效的实现


浙公网安备 33010602011771号