我最近开始用Ruby编程,我正在研究异常处理.
我想知道在C#中是否ensure相当于Ruby finally?我应该:
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
rescue
#handle the error here
ensure
file.close unless file.nil?
end
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做?
#store the file
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
file.close
rescue
#handle the error here
ensure
file.close unless file.nil?
end
Run Code Online (Sandbox Code Playgroud)
是否ensure被调用无论即使一个异常没有什么引发,?
ruby error-handling exception-handling ruby-on-rails exception
我知道这不会直接反转颜色,它会"反对"它.我想知道是否有人知道一种简单的方法(几行代码)来反转任何给定颜色的颜色?
目前我有这个(这不完全是反转的定义,因为如果我传递灰色/灰色,它将返回非常相似的东西,例如127,127,127):
const int RGBMAX = 255;
Color InvertMeAColour(Color ColourToInvert)
{
return Color.FromArgb(RGBMAX - ColourToInvert.R,
RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B);
}
Run Code Online (Sandbox Code Playgroud) 我希望能够使用C#获取其中一个本地目录的大小.我试图避免以下(伪代码),虽然在最坏的情况下我将不得不满足于此:
int GetSize(Directory)
{
int Size = 0;
foreach ( File in Directory )
{
FileInfo fInfo of File;
Size += fInfo.Size;
}
foreach ( SubDirectory in Directory )
{
Size += GetSize(SubDirectory);
}
return Size;
}
Run Code Online (Sandbox Code Playgroud)
基本上,是否有可以在某处使用Walk()以便我可以遍历目录树?这将保存通过每个子目录的递归.
好的,所以我创建了我的c#应用程序,为它创建了一个安装程序,并在我的机器上安装了它.
问题是,当用户打开应用程序exe两次时,将运行两个应用程序实例.我只想要一个应用程序的一个实例随时运行,我该怎么做呢?
谢谢你的帮助,
我正在使用一个我无法编辑的类,它有一个属性(一个布尔值),当它发生变化时被告知是很好的,我无法编辑属性获取或设置,因为我从一个类导入类. DLL(我没有代码).
如何创建在更改属性时触发的事件/函数?
附加
它仅在其自己的类中更改,直接更改为基础私有变量.
例如
private bool m_MyValue = false;
public bool MyValue
{
get { return m_MyValue; }
}
private void SomeFunction()
{
m_MyValue = true;
}
Run Code Online (Sandbox Code Playgroud) 基本上,我想在foreach循环中删除列表中的项目.我知道在使用for循环时这是可能的,但出于其他目的,我想知道使用foreach循环是否可以实现这一点.
在python中,我们可以通过执行以下操作来实现此目的:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
print i
if i == 1:
a.pop(1)
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出
>>>1
3
4
5
6
7
8
9
Run Code Online (Sandbox Code Playgroud)
但是当在c#中做类似的事情时,我得到一个InvalidOperationException,我想知道是否有办法解决这个问题,而不仅仅是使用for循环.
我抛出异常时使用的c#代码:
static void Main(string[] args)
{
List<string> MyList = new List<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9"});
foreach (string Item in MyList)
{
if (MyList.IndexOf(Item) == 0)
{
MyList.RemoveAt(1);
}
Console.WriteLine(Item);
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
一个很好的简单的开始一天!
我无法使用dispatchEvent我static class,我想知道是否有人知道我如何能够实现类似的功能,或者是否有可能从我的静态类调用dispatchEvent?
当我的静态类中的功能完成时,我基本上想要在我的flash文件中通知我的动作脚本代码.
谢谢,
我试图在Objective-C中操纵这个curl请求:
curl -u username:password "http://www.example.com/myapi/getdata"
Run Code Online (Sandbox Code Playgroud)
我已经实现了以下内容,我得到一个数据出现错误Domain=kCFErrorDomainCFNetwork Code=303有NSErrorFailingURLKey=http://www.example.com/myapi/getdata:
// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:@"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD];
// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request …Run Code Online (Sandbox Code Playgroud) 好的,所以我有点困惑,为什么我无法在任何地方找到它,或者如果它不存在那么为什么微软没有实现呢?
所以这是我的场景,我有一个NetworkStream,它有一个名为DataAvailable的可爱的小布尔,我需要的是一个事件,跳出来说"嘿,有数据可供你使用!" (因为我很懒,而且我宁愿被告知有数据可用,而不是继续问"好吧,有没有可用的数据?"一遍又一遍,直到我收到回复"实际上,这次有").
类似于SerialPort(它有一个很好的事件(DataReceived),它通知我从端口接收数据)会很不错.但我正在使用带有NetworkStream的Socket.
如果有一些显而易见的东西我错过了,请指出正确的方向,但如果没有,这是否意味着我将不得不在DataAvailable属性上使用一些数据绑定,当它设置为true时,调用我的拥有'自制'活动/功能?如果这样的话,请你给我一个小例子让球滚动?
编辑
我的完美答案是有人来和我解释如何找到/创建与SerialPort一起使用的DataReceived事件非常相似的东西,但是为通过NetworkStream流式传输的Socket实现了!
再次感谢,谢谢.
我想知道在这些情况下何时调用析构函数,如果它是在主UI线程上调用它?
假设我有以下代码,什么时候会调用析构函数,它会等到我完成所有函数调用之后?
private void Foo()
{
MyObject myObj = new MyObject();
DoSomeFunThingsWithMyObject(myObj);
myObj = new MyObject(); //is the destructor for the first instance called now?
DoLongOminousFunctionality(myObj);
}
//Or will it be called after the DoLongOminousFunctionality?
Run Code Online (Sandbox Code Playgroud)
如果线程在myObj = new MyObject()中断,或者Destructor调用等待直到Thread空闲,那么这只是我感兴趣的东西.
谢谢你的信息.
c# ×7
.net ×6
asp.net ×1
c#-2.0 ×1
colors ×1
curl ×1
custom-event ×1
destructor ×1
exception ×1
flash ×1
invert ×1
ios ×1
iphone ×1
objective-c ×1
python ×1
ruby ×1
serial-port ×1
sockets ×1
winforms ×1
xcode ×1