A few days ago Anders Hejlsberg delivered a presentation about the Future of C# at the Professional Developers Conveference 2008. He focused on the new things that will be available in C# 4.0:

  • dynamic typed objects
  • optional and named parameters
  • improved COM interoperability
  • co- and contra-variance

The recording of the presentation is available on Channel9 and worth watching.

A walk-through of these new features, together with several samples for dynamic typed objects, and simple variance, are also available at MSDN.

If you want to try them you can download the VS 2010 September CTP. Notice that it is available only as a VM image.

Hits for this post: 10430 .

Microsoft has recently released the first Community Technology preview for Visual Studio 2010 and .NET Framework 4.0.

Here is a list of new things in VC++, from the VC++ team’s blog:

  • MSBuild Support for Visual C++
  • IntelliSense and Browsing Experience
    • Improved Responsiveness and Scale
    • Improved Accuracy and Robustness
  • C++0x Features
    • Lambda Expressions
    • Rvalue References
    • static_assert
    • auto Keyword
  • MFC Improvements
    • Task Dialog Support
    • Restart Manager Support
  • Deployment
    • New deployment model for Visual C++ Libraries (changed to not use Windows SxS configuration)

You can read more about this directly on the VC Team Blog. The CTP is available for download at the Microsoft Download Center. The whole download has 7.3 GB !! and the system requirements are:

  • Supported Operating Systems: Windows Server 2003; Windows Server 2008; Windows Vista; Windows XP
  • Minimum 75 GB available HDD space
  • The host computer must have a minimum of 2 GB RAM, with 1 GB allocated to the host operating system and 1 GB allocated to the VPC.
  • We recommend that the host computer CPU be at least a Core Duo 2 GHz processor.
  • Service Pack 1 of Microsoft Virtual PC 2007 is required to access the VPC.

You can provide feedback about this CTP at Microsoft Connect

Hits for this post: 6781 .

Microsoft’s Certified Partners for Learning Solutions are providing this offer untill the end of the year: enroll and attend any one of top Windows Sever 2008 training classes and receive a free copy of Microsoft Windows Server 2008 Standard Edition NFR (Not For Resale). Details about this offer can be found at http://trainingoffers.com/WS2008NFR.aspx. To see the list of available course and training centers select your country, and the state and/or city.

In Romania, the cities with training centers are Bucharest, Timisoara and Constanta.

Hits for this post: 8211 .

RONUA.RO a implinit 4 ani. Pentru aceasta iti multumim, in primul rand tie pentru sprijinul acordat, sprijin fara de care noi, nu am fi implinit 4 ani in aceasta luna. Cu aceasta ocazie festiva, in timpul roadshow-ului TechEd Review 2008, dorim sa facem diferenta intr-o campanie inovativa de promovare a demersului la nivel global de combatere a subnutritiei ca principala cauza a mortalitatii in randul copiilor sub 5 ani.

PROBLEMA

La fiecare sase secunde, undeva in lume, un copil cu varsta sub 5 ani moare din cauza subnutritiei (adica peste 5.000.000 de copii anual, in total). 800 de milioane de oameni se duc la culcare flamanzi. 50% din copiii de pe glob sunt grav sub greutatea normala. (Sursa: UNICEF, Starea copiilor lumii.)

INITIATIVA

AXTI se alatură efortului fundatiei “Nourish The Children” (NTC) în vederea conștientizării problemei și a popularizării metodelor de soluționare a acesteia.

Toate comunitățile afiliate AXTI, printre care și RONUA.RO promoveaza on-line si off-line aceasta campanie.

SOLUTIA

Eu, Marius Bancila, ma angajez sa donez timp de 3 luni suma aferentă hranei pentru 5 copii (3 luni x 5 copii); Fiecare dintre acesti 5 copii vor fi hraniti timp de 3 luni din donatiile de 237 RON/lună.

Afla ce trebuie sa faci tu ca sa devii parte a solutiei:
http://ronua.ro/CS/content/NourishTheChildrenAxtiRonua4ani.aspx#solutia

Hits for this post: 9912 .

A very old unsolved problem in numbers theory, known as Goldbach’s conjecture, says that any even number greater than 5 can be written as the sum of 2 prime numbers. It hasn’t been solver yet, not I will try to solve it. But I decided to write some F# code to display all the possibilities of writing an even number as the sum of two primes. This turned to be quite simple actually:

First, I wrote a function that checks if a number is prime:

let is_prime n =
   not ([2..n/2] |> Seq.filter (fun x -> (n % x) = 0) |> Seq.nonempty)

Then it was all about checking all the numbers from 2 to N/2 to see if k and N-k are both primes and if so print them:

let check n =
   [2..n/2] |>
   Seq.filter (fun x -> (is_prime x) && (is_prime (n-x))) |>
   Seq.iter (fun x -> printfn "%d & %d" x (n-x))

For instance, if we consider number 200, the check function would print:

3 & 197
7 & 193
19 & 181
37 & 163
43 & 157
61 & 139
73 & 127
97 & 103

And that’s about it; though you can imagine optimizing this by caching the prime numbers already computed. But that wasn’t something I wanted to consider for this problem.

Hits for this post: 11127 .

Let’s say you need to write an XML file with this content:

< ?xml version="1.0" encoding="UTF-8"? >
< root description="this is a naïve example" >
< /root >

How do we write that in C++?

At a first glance, you could be tempted to write it like this:

#include < fstream >

int main()
{
	std::ofstream testFile;

	testFile.open("demo.xml", std::ios::out | std::ios::binary); 

	std::string text =
		"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
		"< root description=\"this is a naïve example\" >\n< /root >";

	testFile << text;

	testFile.close();

	return 0;
}

When you open the file in IE for instance, surprize! It's not rendered correctly:

So you could be tempted to say "let's switch to wstring and wofstream".

int main()
{
	std::wofstream testFile;

	testFile.open("demo.xml", std::ios::out | std::ios::binary); 

	std::wstring text =
		L"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
		L"< root description=\"this is a naïve example\" >\n< /root >";

	testFile << text;

	testFile.close();

	return 0;
}

And when you run it and open the file again, no change. So, where is the problem? Well, the problem is that neither ofstream nor wofstream write the text in a UTF-8 format. If you want the file to really be in UTF-8 format, you have to encode the output buffer in UTF-8. And to do that we can use WideCharToMultiByte(). This Windows API maps a wide character string to a new character string (which is not necessary from a multibyte character set). The first argument indicates the code page. For UTF-8 we need to specify CP_UTF8.

The following helper functions encode a std::wstring into a UTF-8 stream, wrapped into a std::string.

#include < windows.h >

std::string to_utf8(const wchar_t* buffer, int len)
{
	int nChars = ::WideCharToMultiByte(
		CP_UTF8,
		0,
		buffer,
		len,
		NULL,
		0,
		NULL,
		NULL);
	if (nChars == 0) return "";

	string newbuffer;
	newbuffer.resize(nChars) ;
	::WideCharToMultiByte(
		CP_UTF8,
		0,
		buffer,
		len,
		const_cast< char* >(newbuffer.c_str()),
		nChars,
		NULL,
		NULL); 

	return newbuffer;
}

std::string to_utf8(const std::wstring& str)
{
	return to_utf8(str.c_str(), (int)str.size());
}

With that in hand, all you have to do is doing the following changes:

int main()
{
	std::ofstream testFile;

	testFile.open("demo.xml", std::ios::out | std::ios::binary); 

	std::wstring text =
		L"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
		L"< root description=\"this is a naïve example\" >\n< /root >";

	std::string outtext = to_utf8(text);

	testFile << outtext;

	testFile.close();

	return 0;
}

And now when you open the file, you get what you wanted in the first place.

And that is all!

Hits for this post: 15494 .