本文整理匯總了C#中System.Language.DefaultIfEmpty方法的典型用法代碼示例。如果您正苦於以下問題:C# Language.DefaultIfEmpty方法的具體用法?C# Language.DefaultIfEmpty怎麽用?C# Language.DefaultIfEmpty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Language
的用法示例。
在下文中一共展示了Language.DefaultIfEmpty方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ParseExpression
/// <summary>
/// Parses a given expression into a graph.
/// </summary>
/// <returns>The built graph.</returns>
/// <param name="expression">The expression to create the graph from.</param>
/// <param name="initGraph">The graph to append too. Used for recursion.</param>
public Graph ParseExpression(string expression, Graph initGraph = null)
{
var lang = new Language ();
var ops = new Operations ();
expression = expression.Trim ();
var opStack = new Stack<Word>();
foreach (char letter in expression) {
//find the language mapping
var inLanguage = lang
.DefaultIfEmpty(null)
.FirstOrDefault (w => w.Letter == letter);
//find the operation mapping
var inOps = ops
.DefaultIfEmpty(null)
.FirstOrDefault(w => w.Letter == letter);
//not in the language or operators
if(inLanguage == null && inOps == null){
throw new InvalidLanguageException("Invalid input: " + letter);
}
//it is valid language input, so add it to the list.
if(inLanguage != null){
opStack.Push(inLanguage);
continue;
}
else{
//not a letter, so must be an operation
//verify that it has the correct arg count for that type of operation.
if (opStack.Count != inOps.MinArgCount && initGraph == null) {
throw new InvalidLanguageException (
string.Format(
"Unbalanced arguement count for operation (expected {0}, but got {1}.",
inOps.MinArgCount, opStack.Count
));
}
//operation, so stop, and process graph
initGraph = ProcessOperation(initGraph, inOps, opStack);
}
}
//expression was given with no operation, so error out.
if (opStack.Count > 0) {
throw new InvalidLanguageException ("No operation was specified in the given expression.");
}
//once done with graph, renumber the states.
initGraph.RenumberStates ();
//return the completed graph.
return initGraph;
}