GVKun编程网logo

c# – 整体类型促销不一致

11

针对c#–整体类型促销不一致这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展bug时间不一致时间不一致时间不一致、Delphi变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型

针对c# – 整体类型促销不一致这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展bug 时间不一致时间不一致时间不一致、Delphi 变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型常用的函数)、html中input的text类型和button类型不一致问题、Mybaits解决实体类字段与数据库字段不一致问题等相关知识,希望可以帮助到你。

本文目录一览:

c# – 整体类型促销不一致

c# – 整体类型促销不一致

using System;

public class Tester
{
    public static void Main()
    {
        const uint x=1u;
        const int y=-1;
        Console.WriteLine((x+y).GetType());
        // Let's refactor and inline y... oops!
        Console.WriteLine((x-1).GetType());
    }
}

想象一下上面的代码在以下情况中使用:

public long Foo(uint x)
{
    const int y = -1;
    var ptr = anIntPtr.ToInt64() + (x + y) * 4096;
    return ptr;
}

内联y看起来非常安全,但事实并非如此.语言本身的这种不一致是违反直觉的,而且非常危险.大多数程序员只是内联y,但实际上你最终会出现整数溢出错误.事实上,如果你编写如上所述的代码,你很容易让下一个人在同一条代码内联工作,甚至没有考虑过两次.

我认为这是C#的一个非常适得其反的语言设计问题.

第一个问题,在C#规范中定义了这种行为,为什么这样设计?

第二个问题,1.GetType()/( – 1).GetType()给出了system.int32.那么为什么它与const int y = -1的行为不同?

第三个问题,如果隐式转换为uint,那么我们如何明确地告诉编译器它是一个有符号整数(1i不是有效的语法!)?

最后一个问题,这不可能是语言设计团队所期望的行为(Eric Lippert要编钟吗?),可以吗?

解决方法

此行为由C#标准的第6.1.9节描述,隐式常量表达式转换:

• A constant-expression (§7.19) of type int can be converted to type sbyte,byte,short,ushort,uint,or ulong,provided the value of the constant-expression is within the range of the destination type.

所以你有常数x = 1u;和常量表达式(x – 1).

根据规范,x-1的结果通常是int,但由于常量表达式(即0)的值在uint的范围内,因此将其视为uint.

请注意,这里编译器将1视为无符号.

如果将表达式更改为(x -1),则将-1视为signed,并将结果更改为int. (在这种情况下,– in -1是一个“一元运算符”,它将-1的结果类型转换为int,因此编译器不能再像普通1那样将它转换为uint.

规范的这一部分暗示如果我们将常量表达式更改为x – 2,那么结果将不再是uint,而是转换为int.但是,如果进行了更改,则会收到编译错误,指出结果将溢出uint.

这是因为C#规范的另一部分,在7.19节常量表达式中指出:

The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions,except that where run-time evaluation would have thrown an exception,compile-time evaluation causes a compile-time error to occur.

在这种情况下,如果进行检查计算会出现溢出,因此编译器会发生故障.

关于这个:

const uint x = 1u;
const int y = -1;
Console.WriteLine((x + y).GetType()); // Long

这与此相同:

Console.WriteLine((1u + -1).GetType()); // Long

这是因为-1的类型为int,而1u的类型为uint.

第7.3.6.2节二进制数字促销描述了这个:

•否则,如果任一操作数的类型为uint而另一个操作数的类型为sbyte,short或int,则两个操作数都将转换为long类型.

(我省略了与此特定表达式无关的部分.)

附录:我只是想指出常数和非常数值之间的一元减号(又称“否定”)运算符的细微差别.

根据标准:

If the operand of the negation operator is of type uint,it is converted to type long,and the type of the result is long.

变量也是如此:

var p = -1;
Console.WriteLine(p.GetType()); // int

var q = -1u;
Console.WriteLine(q.GetType()); // long

var r = 1u;
Console.WriteLine(r.GetType()); // uint

虽然对于编译时常量,如果涉及uint的表达式使用它,则将值1转换为uint,为了将整个表达式保持为uint,-1实际上被视为int.

我同意OP – 这是非常微妙的东西,导致各种惊喜.

bug 时间不一致时间不一致时间不一致

bug 时间不一致时间不一致时间不一致

显示日期与实际日期不一致!!!今天是13号 上面显示的是11号!!!

Delphi 变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型常用的函数)

Delphi 变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型常用的函数)

总结

以上是小编为你收集整理的Delphi 变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型常用的函数)全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

html中input的text类型和button类型不一致问题

html中input的text类型和button类型不一致问题

css:

 

input{
    width: 300px;
    height: 40px;
    font-size: 20px;
}

 

html:

          <div>
          <span>{{title}}</span>
          <input v-bind:type="input_type"
                 v-bind:placeholder="input_placeholder"
                 v-bind:value="input_text"
                 />
          </div>

显示效果:

这里明显button类型的input的width要短上一小点!

解决:在input框添加样式 (不是单独在类型为text或者button的input上加,所有input都加上)

Box-sizing: border-Box;

效果:

 

参考博主:https://blog.csdn.net/weixin_62042254/article/details/121905450

 

Mybaits解决实体类字段与数据库字段不一致问题

Mybaits解决实体类字段与数据库字段不一致问题

public class Employee {

private Integer id;
private String lastName;
private String email;
private String gender;

//setter与getter省略

}

而数据库中的 last_name字段是这样的与实体不一致

@Test
public void test05() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee empById = mapper.getEmpById(1);
System.out.println(empById);
/*Employee empAndDept = mapper.getEmpAndDept(1);
System.out.println(empAndDept);
System.out.println(empAndDept.getDept());*/
// Employee employee = mapper.getEmpByIdStep(3);
// System.out.println(employee);
//System.out.println(employee.getDept());
//System.out.println(employee.getDept());
}finally{
openSession.close();
}
}

在xml里这样配置的

<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where id=#{id}
</select>

因为属性和字段不一致对应查询结果如图

导致了lastName没有附上值

如何解决呢,可以在mybaits-confg.xml配置驼峰命名就能解决这一个问题,但是很局限性

<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/> 
<setting name="jdbcTypeForNull" value="NULL"/>

<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

接下来讲解用resultMap解决这一个问题

<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
<!--指定主键列的封装规则
id定义主键会底层有优化;
column:指定哪一列
property:指定对应的javaBean属性
-->
<id column="id" property="id"/>
<!-- 定义普通列封装规则 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>

<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultMap="MySimpleEmp">
select * from tbl_employee where id=#{id}
</select>

执行结果如下:

 

我们今天的关于c# – 整体类型促销不一致的分享已经告一段落,感谢您的关注,如果您想了解更多关于bug 时间不一致时间不一致时间不一致、Delphi 变体类型(Variant)的介绍(流与变体类型的相互转换、变体类型常用的函数)、html中input的text类型和button类型不一致问题、Mybaits解决实体类字段与数据库字段不一致问题的相关信息,请在本站查询。

本文标签: