using (StreamReader r = new StreamReader(edSourceFileToReplaceStrings.Text))
while ((lineData = r.ReadLine()) != null)
dataFile.Add(lineData);
foreach (string line in dataFile)
file.WriteLine(line);
Developing with CSharp many time you feel a strange feeling... then feeling of lost and how to solve it. Fortunately MSDN has done a good work to help the developers. .net offers thousand ways to do several things! This is the worst thing… because, you do not know where to start from and what works in real world! I hope to help you with your CSharpache.
Estimated costs of instructions Add: 1 ns Subtract: 1 ns Multiply: 2.7 ns Divide: 35.9 ns
problem
|
|
c# - How to
throw exceptions, how to bubble up exceptions, how to digg exceptions to find
deep error messages and Exceptions.
|
|
difficulty level
|
|
7/10 :|
|
|
compatibility
|
|
c#
|
|
solution
|
|
When you catch an Exception
and you want to bubble up (forward) the same exception, you should bubble it
up by your own, define it in the 2nd argument of your new Throw Exception.
When you bubble up the Exception, you can find it as .InnerException.
try{ int i=2/0; //division by zero is not allowed anyway } catch (Exception e) { throw new Exception( "Something get wrong, "+ "error message: "+e.Message, e // here we bubble up the exception ); } The follow code demonstrates how car your dig the InnerException you find all error messages: string output = "Error messages: "; Exception exp = exceptionToBubble; string expmessage = ""; while (exp != null) { expmessage = "inner error: " + exp.Message + " " + expmessage; exp = exp.InnerException; } expmessage += "final error: " + exceptionToBubble.Message; output += expmessage; Also, don't forget the .Trace property of the Exception object where show related info. |
problem
|
|
c# dispose objects of Dictionary
you cannot dispose the object of a Dictionary with "foreach" process, you can do with "while" getting the Enumerator of the Dictionary. |
|
difficulty level
|
|
5/10 :|
|
|
compatibility
|
|
c#
|
|
solution
|
|
In the follow example, I scan serially a
Dictionary generic of <string, IInDataObject> type and I dispose its
hosted objects.
Each object in the dictionary, each Value is IInDataObject interface. This interface implements IDisposable so can call Dispose(); //get the Enumerator in order to scan with while (and not with foreach) System.Collections.IEnumerator enumerator = dataObjectList.GetEnumerator(); //perform the Enumerator in while loop while (enumerator.MoveNext()) { //get the pair of Dictionary KeyValuePair<string, IInDataObject> pair = ((KeyValuePair<string, IInDataObject>)(enumerator.Current)); //dispose it pair.Value.Dispose(); } |
Looping over Dictionary with foreach [C#] static int A(Dictionary<string, int> d) { int a = 0; foreach (var pair in d) { a += pair.Value; } return a; }Next, this method demonstrates the GetEnumerator and MoveNext methods and the Current property directly. This code is compiled to the same intermediate language except the try/finally block is absent.
Looping over Dictionary with GetEnumerator [C#] static int B(Dictionary<string, int> d) { int b = 0; var enumerator = d.GetEnumerator(); while (enumerator.MoveNext()) { var pair = enumerator.Current; b += pair.Value; } return b; }
problem
|
|
mssql - compare
two datetime
Mssql, compare two datetime fields, if their values are close, if are in 24h difference and do on --- > dennis |
|
difficulty level
|
|
1/10 :)))
|
|
compatibility
|
|
mssql
|
|
solution
|
|
In mssql, the
datetime fields are actually numeric with decimals. The integer part in the
“days”. So if you want to compare two dates compare the integer part as
bellow
-- compare if the item is sold 24h before the item is modified Select * from items Where ModificationDate<SaleDate-1 -- find person(s) that born on 12/11/2010 (not exactly on the same, with at least 12 hours difference) Select * from persons Where birthdate-0.5>=’2010-12-11’ and birthdate+0.5<=’2010-12-11’ |