我有一个dictionary这样的简单:
var fruitDictionary = new Dictionary<string, string> {Apple,Fruit}, {Orange, Fruit}, {Spinach, Greens}
我有一个字符串
var fruitString = Apple Orange Spinach Orange Apple Spinach
如何用matching-word字典替换该句子中所有出现的特定单词?
(即)上面的句子应该是Fruit Fruit Greens Fruit Fruit Fruit什么?
任何想法都非常感谢.
编辑:
我试过这样的事情:
var outputString = string.Empty;
fruitString.ToArray().ToList().Foreach(item =>
{
if (fruitDictionary.ContainsKey(item))
{
outputString = outputString + fruitDictionary[item];
}
Run Code Online (Sandbox Code Playgroud)
对此有何最佳解决方案?上面的代码不是最优的,因为它完成traversing给定数组的整个长度!
我编写此代码以递归方式列出c#中的文件和文件夹.
var filesInTheCurrentDirectory = System.IO.Directory.GetFiles(rootFolder);
if (!filesInTheCurrentDirectory.Any() && !System.IO.Directory.GetDirectories(rootFolder).ToList().Any())
{
return;
}
filesInTheCurrentDirectory.ToList().ForEach(file => System.IO.File.AppendAllText("e:\\personal\\tests.txt", System.IO.Path.GetFileName(file) + ":" + rootFolder + "\n"));
System.IO.Directory.GetDirectories(rootFolder).ToList().ForEach(recursivePrintFolders);
Run Code Online (Sandbox Code Playgroud)
虽然这很有用,但问题是:
我正在使用递归.这是这样的best吗?(我尝试编写一个非递归函数,但因为我们事先并不知道每个文件夹的深度是多少).
如何评估这个功能的性能?难道OlogN还是O(n)?(我很困惑,因为没有循环版本.根据我的说法,如果有两个for循环,我可以调用它O(n^2).)
任何想法或指导?
这是一个用SQL Server Management Studio编写的简单存储过程.
它汇编很好,但我有问题调用它:(
CREATE PROCEDURE [dbo].[DetermineVoltage]
@itemDescription varchar(225) ,
@voltageRating varchar(225) OUTPUT
AS
BEGIN
SELECT
@voltageRating = REVERSE(SUBSTRING(REVERSE(@itemDescription),
charindex(' V', REVERSE(@itemDescription)) + 2,
CHARINDEX(' ', REVERSE(@itemDescription), charindex(' V', REVERSE(@itemDescription)) + 1) - charindex(' V', REVERSE(@itemDescription)) - 2))
RETURN
END
Run Code Online (Sandbox Code Playgroud)
在SQL Server Management Studio中,当我执行此存储过程时,如下所示:
DECLARE @voltageRating VARCHAR(225)
exec dbo.DetermineVoltage['test 20V', @voltageRating]
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
程序或功能'DetermineVoltage'需要参数'@voltageRating',这是未提供的.
有许多SO答案讨论了相同的错误消息,但是从C#程序执行时.
但是,就我而言,如何在SQL Server Management Studio中执行存储过程?
我可能错过了这里的一些东西.语法不正确.
static Task<int> MathOperation(int number)
{
//return new Task(new Func(TestMethod(number)));
}
static int LongRunningMethod(int number)
{
// some long running operation
}
Run Code Online (Sandbox Code Playgroud)
如何编写return声明MathOperation?
我收到此错误:使用泛型类型'System.Func'需要1个类型参数.
当我使用resharper工具时,它只是说"使用内置类型"字符串"而不是使用"字符串".同样,它将UInt32转换为uint.
我用google搜索了这一切,我发现它们都是别名.别名意为"重复".好.但是,它们究竟是什么意思呢?
谷歌搜索但无法找到任何令人满意的答案.谢谢.
可能重复:
如何获取IEnumerable中元素的索引?
我有以下功能,接受可相关的字符串列表.
我遍历所有字符串,如果它的值等于" TestName"(不区分大小写),我返回它的位置.
int GetMyTestColumnPosition(IEnumerable<string> TitleNames)
{
foreach (var test in TitleNames)
{
if (string.Compare(test, "testname", stringComparison.CurrentCultureIgnoreCase) == 0)
{
// return TitleNames.IndexOf(test); does not work!
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我将参数更改为" IList<string>",这是有效的!但,
考虑配置(在我的情况下属性)为null的情况.
public Configuration {get;set;}
if (configuration == null)
{
throw NullReferenceException("Blah blah blah..");
}
Run Code Online (Sandbox Code Playgroud)
但是,我在某处读到,"不要在代码中抛出空引用异常.NullReferenceException是运行时异常,只应由运行时引发".
如果它是一个函数的参数,我想我会用一个ArgumentNullException.
那么,在这种情况下应该是什么例外?总的来说,在什么情况下应该抛出什么例外?用Google搜索但没有令人满意的答案.
请考虑以下陈述
void foo()
{
string text = "something";
int a = 10;
int b = a;
var array = new int[5]{2,1,3,5,4};
GC.Collect();
}
**Stack** **Heap**
text = 0x20 0x20 something
a = 10
b = 10
array = 0x50:0x55 0x50 2
0x52 1
0x53 3
0x54 5
0x55 4
Run Code Online (Sandbox Code Playgroud)
一些假设:
问题是:
堆中有什么价值?
我回答了内存位置0x20,0x50-0x55将被回收并删除.他再次问道,记忆位置有什么价值?我猜可能是垃圾值.他说错了.(WTX?)
堆栈中的项目会发生什么?text,a,b,array包含哪些值?
我回答说,因为它们包含的引用已被删除,它们也将被回收.他问现在变量的值是多少.
在GC Collection之后重绘上面的表格列.
GC运行后
**Stack** **Heap**
text (cleared) 0x20 Garbage/NULL
a (cleared)
b (cleared)
array (cleared) 0x50 Garbage/NULL
0x52 Garbage/NULL
0x53 Garbage/NULL
0x54 Garbage/NULL …Run Code Online (Sandbox Code Playgroud)在下面的程序中, (!testlist.Any())抛出一个参数null异常.Any()扩展方法默认不处理空值吗?
什么是正确的方法?是否应该在Any()之前添加空检查何时List<int>用作方法中的参数?
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
foo(null);
}
public static void foo(List<int> testlist)
{
if (!testlist.Any())
{
Console.WriteLine("testlist is empty!");
}
}
}
Run Code Online (Sandbox Code Playgroud) 这是我用来学习角js 的小提琴
简而言之,JS正在使用的文件:
angular.module('ngApp', [])
.service('myownservice', '$q', function ($timeout, $q) {
this.httpcall = function() {
var httpresp = "1818";
//making an http call over here.
return httpresp;
};
this.UpdateSomeData = function () {
var defer = $q.defer();
myownservice.httpcall().then(function(data) {
defer.resolve(data);
});
return defer.promise;
};
})
.controller('ctrl', function ($scope, myownservice) {
$scope.value = UpdateSomeData();
});
Run Code Online (Sandbox Code Playgroud)
html page:
<div ng-controller="ctrl">{{value}}</div>
Run Code Online (Sandbox Code Playgroud)
但我收到的错误就像是
Argument 'fn' is not a function, got string.
有什么想法吗?