考虑这样的方法:
public void WorkAt(string location = @"home")
{
//...
}
Run Code Online (Sandbox Code Playgroud)
可以通过显式传递值来调用它,例如:
WorkAt(@"company");
WorkAt(@"home");
Run Code Online (Sandbox Code Playgroud)
或者只使用默认值,例如:
WorkAt();
Run Code Online (Sandbox Code Playgroud)
有没有办法知道是否使用默认值?
例如,我想这样编码:
public void WorkAt(string location = @"home")
{
if ( /* the default value is used unexplicitly */)
{
// Do something
}
else
{
// Do another thing
}
}
Run Code Online (Sandbox Code Playgroud)
请注意WorkAt("home")与WorkAt()此相关的不同之处.
深入了解C#,我遇到了一个与对象引用相等的小问题.我说我有两个字符串:
String a = "Hello world!";
String b = "Bonjour le monde";
bool equals = ReferenceEquals(a, b); // ******************* (1)
b = "Hello world!";
equals = ReferenceEquals(a, b); // ******************* (2)
Run Code Online (Sandbox Code Playgroud)
(1)是false和那是预期的.ReferenceEquals 文档说
ReferenceEquals比较实例
但是之后:
true?a和b它们不是同一个对象吗?如果是,那么它们是如何变得相同的,因为我从未明确地做过a=b在下面的代码中,我正在检查对象引用的相等性.
string x = "Some Text";
string y = "Some Other Text";
string z = "Some Text";
Console.WriteLine(object.ReferenceEquals(x, y)); // False
Console.WriteLine(object.ReferenceEquals(x, z)); // True
Console.WriteLine(object.ReferenceEquals(y, z)); // False
y = "Some Text";
Console.WriteLine(object.ReferenceEquals(x, y)); // True
Console.WriteLine(object.ReferenceEquals(x, z)); // True
Console.WriteLine(object.ReferenceEquals(y, z)); // True
Run Code Online (Sandbox Code Playgroud)
这里:
x并z指同一个对象; 我可以说x是实习并z使用了taht版本.好吧,我不确定这个; 如果我错了,请纠正我.y通过赋予它与x相同的值来改变它的值.我以为它会在这里创建一个新对象; 但我错了,它使用相同的参考.我的问题是:
.net使用字符串实习生?我昨天在为WP编写演示应用程序时发现了这种奇怪的行为.通常我从不传递普通物品,也从不传递"相同" - 但这次我做了.当绑定到int或string类型的observablecollection时,如果值相同,则控件将添加第一个项,然后是两个,然后是三个,然后是四个.而不是每次添加一个.基本上这样做:
Items.Count ++
但是,如果我将项目(例如字符串)更改为唯一的(例如GUID.ToString),则它具有预期的行为.
这与DataContext的设置方式无关,或者我是否将LayoutMode ="List"IsGroupingEnabled ="False"添加到控件中.
为什么这样做?这是预期的行为吗?
我花了几个小时寻找答案,所以请不要粘贴关于控件的随机链接:)
视图代码:
<phone:PhoneApplicationPage
x:Class="Bug.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<Button Click="Button_Click">Add</Button>
<phone:LongListSelector Width="300" Height="600" ItemsSource="{Binding Items}"/>
</StackPanel>
</phone:PhoneApplicationPage>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Collections.ObjectModel;
using System.Windows;
namespace Bug
{
public partial class MainPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Items = new ObservableCollection<string>();
//DataContext = this;
}
public ObservableCollection<string> Items { get; set; }
private void Button_Click(object …Run Code Online (Sandbox Code Playgroud) 下面的代码是如何打印的true?
string x = new string(new char[0]);
string y = new string(new char[0]);
Console.WriteLine(object.ReferenceEquals(x,y));
Run Code Online (Sandbox Code Playgroud)
我希望这会打印False,因为我希望构造两个单独的对象,然后比较它们的引用。