我有以下示例(链接到ideone).
long lDurationMillis = 0;
lDurationMillis = Long.parseLong("30000.1");
System.out.print("Play Duration:" + lDurationMillis);
Run Code Online (Sandbox Code Playgroud)
它引发了一个异常:
Exception in thread "main" java.lang.NumberFormatException: For input string: "30000.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:419)
at java.lang.Long.parseLong(Long.java:468)
at Main.main(Main.java:9)
Run Code Online (Sandbox Code Playgroud)
但为什么它不会让我直接将该数字转换为字符串?我可以将数字转换为整数而不是转换为double.但还有其他方法吗?
我想通过传递命令行参数来覆盖标准app.config的使用.如何更改默认应用程序配置文件,以便在访问ConfigurationManager.AppSettings时访问命令行中指定的配置文件?
编辑:
事实证明,加载与EXE加上.config名称不同的配置文件的正确方法是使用OpenMappedExeConfiguration.例如
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Shell2.exe.config");
currentConfiguration = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
这部分有效.我可以在appSettings部分看到所有键,但所有值都为null.
首先,问题是:CLR规范是否保证在同一进程中的多个应用程序域中执行的代码将共享相同的地址空间?通过"共享地址空间",我的意思是指向在一个应用程序域中分配的内存的指针对于在同一进程内托管的所有应用程序域中进行读写是有效的.
请考虑这个自包含的示例来说明问题:程序Worker在单独的应用程序域中分配对象.该Worker分配10000个整数一个内存块,并用数据填充进去.然后,程序调用app域边界以获取指向已分配块的指针,并验证它是否可以读取10,000个项目中的每一个.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace crossapp {
public class Worker : MarshalByRefObject {
private readonly IntPtr myData;
public const int DataLength = 10000;
public Worker() {
Console.Error.WriteLine(
"Memory allocation happens in app domain '{0}'"
, Assembly.GetExecutingAssembly().FullName
);
myData = Marshal.AllocHGlobal(sizeof(int) * DataLength);
unsafe {
var ptr = (int*) myData.ToPointer();
for (var i = 0 ; i != DataLength ; i++) {
ptr[i] = 2*i + 1;
}
}
}
public IntPtr GetData() …Run Code Online (Sandbox Code Playgroud) 我们如何从两个ArrayList中删除常用值.让我们考虑我有两个Arraylist,如下所示
ArrayList1= [1,2,3,4]
ArrayList1= [2,3,4,6,7]
Run Code Online (Sandbox Code Playgroud)
我希望得到结果
ArrayListFinal= [1,6,7]
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我刚刚开始自学C#,在关于Switch语句的教程中,我读到:
禁止执行流程从一个案例块流向下一个案例块的行为是C#与C++不同的一个区域.在C++中,允许case语句的处理从一个到另一个.
为什么它在C#中的一个案例陈述后停止?如果您可以break在任何时候使用该语句停止,那么在C#与C++之后是否有任何理由让它在找到匹配后停止?如果你想在C#中使用多个案例,你是否需要使用另一个Switch语句?
我发现自己编写了一些方法,其中存在一个永远不会发生的代码路径.这是一个简化的例子:
double Foo(double x) {
int maxInput = 100000;
double castMaxInput = (double)maxInput;
if (x < 0 || x > castMaxInput || double.IsNaN(x)) {
return double.NaN;
}
double r = 0;
for (double boundary = 1; boundary<=castMaxInput; boundary++) {
if (x <= boundary) {
r += boundary * (x + 1 - boundary);
return r;
}
else {
r += boundary;
}
}
// we should never get here.
throw new SomeException();
}
Run Code Online (Sandbox Code Playgroud)
这里最有意义的例外是类似的
TheAuthorOfThisMethodScrewedUpException()
Run Code Online (Sandbox Code Playgroud)
因为如果我们到达for循环的末尾,那就是正在发生的事情.不幸的是,使用上面构造的方法,编译器似乎不够聪明,无法确定for循环之后的代码永远不会发生.所以你不能在那里什么也没有,或者编译器会抱怨"并非所有的代码路径都返回一个值".是的,return double.NaN除了之前我还可以在循环之后输入.但这会掩盖问题的根源.
我的问题是 …
我目前正在重写一些单元测试以使用NUnit 3而不是NUnit 2,并且需要将一些断言更改为基于约束的断言.我有以下断言:
Assert.IsNullOrEmpty(result);
Run Code Online (Sandbox Code Playgroud)
我改为:
Assert.That(result, Is.Null.Or.Empty);
Run Code Online (Sandbox Code Playgroud)
但是,断言时我对可读性并不完全满意IsNotNullOrEmpty:
Assert.That(result, Is.Not.Null.And.Not.Empty);
Run Code Online (Sandbox Code Playgroud)
我目前的建议是创建以下静态类:
public static class Text
{
public static EmptyConstraint IsNullOrEmpty => Is.Null.Or.Empty;
public static EmptyConstraint IsNotNullOrEmpty => Is.Not.Null.And.Not.Empty;
}
Run Code Online (Sandbox Code Playgroud)
用法:
Assert.That(result, Text.IsNotNullOrEmpty);
Run Code Online (Sandbox Code Playgroud)
这提供了更好的可读性,但代价是引入了自定义约束.是否存在制定相同断言的标准方法,还是应该继续使用Is.Not.Null.And.Not.Empty?
我有一个View Controller,我在故事板中添加了一个圆形的矩形按钮.该应用程序运行良好,我还使用故事板将按钮连接到segue.我正在尝试为此按钮设置自定义图像,以便打开和关闭状态.如何访问此按钮并设置其属性?(在这种情况下,开关图像)
我不太熟悉C语言并在Linux中进行编译,但我有一些问题要求并希望你能提供帮助.
我有这行代码使用已安装的命令及其参数来加入域.(见图).在我运行gcc join.c编译之后,它创建了一个文件a.out.到目前为止这么好,但是当我vim a.out查看该文件的内容时,我看到mypassword可以通过简单的文本编辑器轻松查看.(见第二张图)
编译我的C代码时,有什么办法可以避免这种情况吗?
#include <stdio.h>
#include <unistd.h>
int main ()
{
printf("Running 'net join' with the following parameters: \n");
char *domain="mydomain";
char *user="domainjoinuser";
char *pass="mypassword";
char *vastool="/opt/quest/bin/vastool";
char *ou="OU=test,DC=mtdomain,DC=local";
char unjoin[512];
sprintf(unjoin,"/opt/quest/in/vastool -u %s -w '%s' unjoin -f",user,pass);
printf("Domain: %s\n",domain);
printf("User: %s\n",user);
printf("-----------------\n");
printf("Unjoin.............\n");
system(unjoin);
printf("Join................\n");
execl("/opt/quest/bin/vastool", "vastool", "-u", user, "-w", pass, "join", "-c", "ou", "-f", domain, (char*)0);
}
Run Code Online (Sandbox Code Playgroud)

00000000 7f 45 4c 46 02 01 01 00 00 00 …
我有一个使用大量字符串的应用程序.所以我有一些内存使用问题.我知道在这种情况下最好的解决方案之一是使用数据库,但我暂时不能使用它,所以我正在寻找其他解决方案.
在C#中,字符串存储在Utf16中,这意味着与Utf8(对于我的字符串的主要部分)相比,我丢失了一半的内存使用量.所以我决定使用utf8字符串的字节数组.但令我惊讶的是,这个解决方案在我的应用程序中占用了比简单字符串多两倍的内
所以我做了一些简单的测试,但我想知道专家的意见.
测试1:固定长度字符串分配
var stringArray = new string[10000];
var byteArray = new byte[10000][];
var Sb = new StringBuilder();
var utf8 = Encoding.UTF8;
var stringGen = new Random(561651);
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Sb.Append((stringGen.Next(90)+32).ToString());
}
stringArray[i] = Sb.ToString();
byteArray[i] = utf8.GetBytes(Sb.ToString());
Sb.Clear();
}
GC.Collect();
GC.WaitForFullGCComplete(5000);
Run Code Online (Sandbox Code Playgroud)
内存使用情况
00007ffac200a510 1 80032 System.Byte[][]
00007ffac1fd02b8 56 152400 System.Object[]
000000bf7655fcf0 303 3933750 Free
00007ffac1fd5738 10004 224695091 System.Byte[]
00007ffac1fcfc40 10476 449178396 …Run Code Online (Sandbox Code Playgroud)