我刚刚意识到C#属性构造也可以与私有访问修饰符一起使用:
private string Password { get; set; }
Run Code Online (Sandbox Code Playgroud)
虽然这在技术上很有意思,但我无法想象何时使用它,因为私人领域涉及更少的仪式:
private string _password;
Run Code Online (Sandbox Code Playgroud)
我无法想象什么时候我需要能够内部获取但不能设置或设置但不能获得私有字段:
private string Password { get; }
Run Code Online (Sandbox Code Playgroud)
要么
private string Password { set; }
Run Code Online (Sandbox Code Playgroud)
但也许有一个嵌套/继承类的用例,或者可能是get/set可能包含逻辑而不是仅仅返回属性的值,尽管我倾向于保持属性严格简单并让显式方法做任何逻辑,例如GetEncodedPassword().
是否有人出于任何原因在C#中使用私有属性,或者它只是那些技术上可能但很少使用的实际代码构造之一?
很好的答案,阅读它们我剔除了私人财产的这些用途:
如何将可空的DateTime dt2转换为格式化的字符串?
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss")); //works
DateTime? dt2 = DateTime.Now;
Console.WriteLine(dt2.ToString("yyyy-MM-dd hh:mm:ss")); //gives following error:
Run Code Online (Sandbox Code Playgroud)
方法ToString没有重载需要一个参数
这个淘汰js的例子在你编辑一个字段并按下TAB时就可以工作,视图模型数据以及字段下面的文本都会被更新.
如何更改此代码以便每次按键更新viewmodel数据?

<!doctype html>
<html>
<title>knockout js</title>
<head>
<script type="text/javascript" src="js/knockout-1.1.1.debug.js"></script>
<script type="text/javascript">
window.onload= function() {
var viewModel = {
firstName : ko.observable("Jim"),
lastName : ko.observable("Smith")
};
viewModel.fullName = ko.dependentObservable(function () {
return viewModel.firstName() + " " + viewModel.lastName();
});
ko.applyBindings(viewModel);
}
</script>
</head>
<body>
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 在Eclipse中,您可以按CTRL- SHIFT- 搜索项目中的文件R.
有没有办法在Visual Studio中执行此操作?
在SQL Server 2008 Management Studio中,当我右键单击数据库表并选择" 选择前100行 "时,我可以轻松地向SQL添加"ORDER BY"语句.这很好.
但是当我选择" 编辑前200行 "时,我没有能力改变 SQL(这使得很难找到并编辑刚刚添加到10,000中的记录.
我很确定我能够在SQL Server 2000中执行此操作.
在编辑记录时,SMSS 2008中是否有任何方法可以改变记录的显示方式?
我经历过的大多数MVVM示例都使用了Model实现INotifyPropertyChanged,但在Josh Smith的CommandSink示例中 ,ViewModel实现了INotifyPropertyChanged.
我还在认知上把MVVM概念放在一起,所以我不知道是否:
您曾参与MVVM项目的其他经历是什么?
自从从TABLE布局切换到DIV布局以来,一个常见的问题仍然存在:
问题:你用动态文本填充你的DIV,并且不可避免地有一个超长的单词延伸到div列的边缘并使你的网站看起来不专业.
RETRO-WHINING:表格布局从未发生过这种情况.表格单元格总是很好地扩展到最长单词的宽度.
严重性:即使是最主要的网站,我也会看到这个问题,特别是在德国网站上,即使是"速度限制"等常用词也很长("Geschwindigkeitsbegrenzung").
有没有人有这个可行的解决方案?
有没有人知道一些可用的全局状态变量,以便我可以检查代码当前是否在设计模式下执行(例如在Blend或Visual Studio中)?
它看起来像这样:
//pseudo code:
if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我需要这个的原因是:当我的应用程序在Expression Blend中以设计模式显示时,我希望ViewModel使用"Design Customer类",其中包含模拟数据,设计人员可以在设计模式下查看.
但是,当应用程序实际执行时,我当然希望ViewModel使用返回实际数据的真实Customer类.
目前我通过让设计人员在开始工作之前进入ViewModel并将"ApplicationDevelopmentMode.Executing"更改为"ApplicationDevelopmentMode.Designing"来解决这个问题:
public CustomersViewModel()
{
_currentApplicationDevelopmentMode = ApplicationDevelopmentMode.Designing;
}
public ObservableCollection<Customer> GetAll
{
get
{
try
{
if (_currentApplicationDevelopmentMode == ApplicationDevelopmentMode.Developing)
{
return Customer.GetAll;
}
else
{
return CustomerDesign.GetAll;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud) 在WPF应用程序中,当用户单击某个按钮时,我想将Windows资源管理器打开到某个目录,我该怎么做?
我希望这样的事情:
Windows.OpenExplorer("c:\test");
Run Code Online (Sandbox Code Playgroud) 我需要将键/对对添加到字典中,但我当然需要首先检查密钥是否已经存在,否则我得到" 密钥已存在于字典中 "错误.下面的代码解决了这个问题,但很笨重.
在没有像这样的字符串帮助方法的情况下,这样做更优雅的方法是什么?
using System;
using System.Collections.Generic;
namespace TestDictStringObject
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> currentViews = new Dictionary<string, object>();
StringHelpers.SafeDictionaryAdd(currentViews, "Customers", "view1");
StringHelpers.SafeDictionaryAdd(currentViews, "Customers", "view2");
StringHelpers.SafeDictionaryAdd(currentViews, "Employees", "view1");
StringHelpers.SafeDictionaryAdd(currentViews, "Reports", "view1");
foreach (KeyValuePair<string, object> pair in currentViews)
{
Console.WriteLine("{0} {1}", pair.Key, pair.Value);
}
Console.ReadLine();
}
}
public static class StringHelpers
{
public static void SafeDictionaryAdd(Dictionary<string, object> dict, string key, object view)
{
if (!dict.ContainsKey(key))
{
dict.Add(key, view);
}
else
{
dict[key] = view; …Run Code Online (Sandbox Code Playgroud) c# ×6
wpf ×2
collections ×1
css ×1
datetime ×1
dictionary ×1
formatting ×1
html ×1
javascript ×1
knockout.js ×1
layout ×1
mvvm ×1
nullable ×1
properties ×1
search ×1
sql-server ×1
ssms ×1