tag:blogger.com,1999:blog-58720659623346109082024-03-08T07:13:42.033-08:00Tomb of the PrimesC# code snippets that I'm usingUnknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5872065962334610908.post-72356253015369184472011-02-16T11:01:00.000-08:002011-02-16T11:07:01.723-08:00Measuring memory cunsumptionWhen using any object oriented programming language you sooner or later start using your own objects. But when creating your own class - more complex class that uses a lot of other objects, it may be a good idea to know how much memory your object cunsumes - You may want to maintain thousands of your objects in some collection for some time and it costs some memory..<br />
<br />
A good way to get to know how much memory your object eats is using GetTotalMemory() method in System.GC.<br />
<br />
<pre class="brush: js">long memoryStart = System.GC.GetTotalMemory(true);
// Generate some of your objects
ICollection<myobject> myObjects = List<myobject>();
for (int i = 0; i < 10000; i++)
{
myObjects.Add(new MyObject);
}
long memoryEnd = System.GC.GetTotalMemory(true);
MessageBox.Show((memoryEnd - memoryStart).ToString());
</pre>
<br />
That gives you how much memory consume 10 000 objects of your class. Divide it by 10 000 and it's the memory consumption of one object.Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-5872065962334610908.post-68193663695847220862011-02-14T07:48:00.000-08:002011-02-14T07:50:33.986-08:00Time measurement with StopwatchWhen I need to measure how long some action take I use Stopwatch class. It's in System.Diagnostics.<br />
Here's sample:<br />
<br />
<pre class="brush: js">Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// action I want to measure
stopwatch.Stop();
</pre><br />
Or just<br />
<br />
<pre class="brush: js">Stopwatch stopwatch = Stopwatch.StartNew();
// action I want to measure
stopwatch.Stop();
</pre><br />
It's more precise than using DateTime.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5872065962334610908.post-67070916967335345602011-02-11T00:49:00.000-08:002011-02-11T00:49:06.908-08:00Web tip - WPFwikiA few days ago I found an interrresting source of answers to some WPF questions. It's called WPFwiki and can be found here <a href="http://wpfwiki.com/">www.wpfwiki.com</a>.<br />
The good thing is that the content is being updated time to time, so there might be a lot of interresting information.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5872065962334610908.post-40429968484415535242011-01-11T13:37:00.000-08:002011-01-12T07:05:47.841-08:00Converting/parsing string to intCommon C# application uses TextBoxes to enter some values which the program works with. So there is a need to convert some string from raw input to some other type. These examples show how to convert string to int.<br />
<br />
Using static class <b>Convert</b>:<br />
<pre class="brush: js">string input = "10";
int number;
number = Convert.ToInt32(input);</pre><br />
This method (and similar for other value types) can throw <b>FormatException</b> when <i>input</i> is not in the correct format (is not a number) of <b>OverflowException</b> if the string<i> input</i> is number but it's value is less than <b>MinValue</b> or greater than <b>MaxValue</b>.<br />
<br />
The second option is to use <b>Parse</b> method:<br />
<pre class="brush: js">result = int.Parse(input);</pre><br />
But this method can also throw some exceptions. Two of them are mentioned before: <b>FormatException</b> and <b>OverflowException</b>. Another exception, <b>ArgumentNullException</b>, is thrown when the argument (string <i>input</i>) is null.<br />
<br />
The third option and in my opinion the best solution is to use <b>TryParse</b> method. This method doesn't throw exceptions so the isn't any exception handling slowing down the application. It only return true if the parse succeeded or false if it didn't.<br />
<pre class="brush: js">bool parsed;
parsed = int.TryParse(input, out result);</pre><br />
I hope someone may find this snippet useful.Unknownnoreply@blogger.com0