Skip to main content
 首页 » 编程设计

c#之类型转换操作中的不同行为

2024年02月06日14bjzhanghao

<分区>

谁能给我解释一下,为什么在下面的两个转换场景中,转换变量的行为不同?虽然第一个变量(双初始值)在第一个示例代码中保留了它的初始值,但“sender”对象根据它被转换成的新变量改变了它的内容属性值?

第一个示例:

double initialValue = 5; 
 
int secValue = (int)initial; 
 
secValue = 10; 
 
Console.WriteLine(initial); // initial value is still 5. 

第二个示例:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button btn = (Button)sender; 
    btn.Content = "Clicked"; // "sender" objects content property is also set to "Clicked". 
}