background preloader

C#

Facebook Twitter

Jsc (c# to javascript) Public fields vs automatic properties. An interesting new feature in C# 3.0 is that of automatic properties on objects – this allows us to define a get/set property and the creation of the underlying field is taken care off for us.

Public fields vs automatic properties

We can therefore create a class like this: Now ignoring the fact that it’s terrible OO to write a class like that, one thing that we’ve been wondering is what’s the difference between doing the above and just creating a public field on Foo called Bar like so: Dynamic objects and ReSharper. As you might have heard by now, C# 4.0 (or is it just 4?

Dynamic objects and ReSharper

…) comes with a new keyword: dynamic. This means that you could do something like the following: Simply put, ExpandoObject is a class that allows you to add and remove members at runtime. This allows us to call methods that are resolved at runtime. As such, the previous code will compile. Just as you can declare methods, you can also declare properties: This no doubt can come in handy when working with ViewModels and ASP.NET MVC. However, there is one minor problem with dynamic objects: you lose intellisense, which means that if in your view, instead of typing dynaCustomer.FirstName, you type dynaCustomer.FristName, you won’t get any errors until you run the app.

And that’s where ReSharper can help: This is the same code, but with ReSharper activated inside Visual Studio 2010! FileSystemWatcher and IOException Class. This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner. Another very useful class, FileSystemWatcher, acts as a watchdog for file system changes and raises an event when a change occurs. You must specify a directory to be monitored.

The class can monitor changes to subdirectories and files within the specified directory. Create a Relative path code snippet. MathML Visualizer in C# on Windows 7. Wednesday, February 11, 2009 10:37 AM bart Lately I’ve been playing quite a bit with mathematical representation formats like OMML (Office’s markup language for math) and MathML (the nice thing about standards is …, well you know).

MathML Visualizer in C# on Windows 7

Both have their pros and cons, but let’s not dive into that for now. In today’s post, I’m showing you the new Windows 7 “Math Input Panel” and how you can retrieve its MathML data from the clipboard in a UI application. I’m actually using this technique to read MathML and feed it in to my interpreter that tries to turn it into execution. I’ll show how to do a similar thing with Office 2007’s OMML in a next post, using VSTO that time. Okay, so what are we talking about. When you open up this guy, you’ll see the following: Now try to write something math-stylish.

Sweet! So here it is: a simple form with a few events, using two user32 functions and a window message that were added in Vista: Enjoy! Filed under: Windows 7. An Overview Of System.Collections.Generic. I recently put up a post on my blog about some of the new concurrent collections in .NET 4.0, and I noticed that a lot of people were being sent by Google to those posts who were only searching for System.Collections.

An Overview Of System.Collections.Generic

I figured that maybe people could use a similar overview of the collections available to them in the System.Collections.Generic namespace, since it seems to me that no one uses anything other than List and Dictionary. So, in this post, I am going to take a look at a few of those collections, and explain exactly why you would want to use them. Reflection on a method with an out parameter. C# String Interpolation. Displaying custom HTML in WebBrowser control. I am using WebBrowser control to show preview of automatically generated HTML.

Displaying custom HTML in WebBrowser control

Users can select options and preview pane reflects those changes automatically. WebBrowser control has some problems that have been here for years already. Here is my example about how to show custom HTML in WebBrowser control. Problem When we look at methods and properties of WebBrowser control our common sense tells us that something like this should work (VB.NET users: delete “;” after this line): webBrowser1.DocumentText = "some text here"; And it works - only once. Solution To get all assignments works after first one we need to navigate to some page. about:blank is good candidate because it “exists” also in local machine for sure.

Private void DisplayHtml(string html) Static Guid as argument of an attribute. String Formatting in C# I couldn’t find a quick reference to .NET string formatting using the String.Format() function, so I created this one (which has also spawned this String Formatting FAQ).

String Formatting in C#

When I started working with the .NET framework, one thing puzzled me. I couldn’t find sprintf(). sprintf() is the C function that takes an output buffer, a format string, and any number of arguments, and builds a string for you. For example: char szError[256];sprintf(szError, “Error %d occurred. \n”, nError); This would write “Error 12 occurred.” into the szError buffer (assuming nError was 12).

Str << “Error ” << nError << ” occurred.” << endl;