background preloader

The C# Language

The C# Language

.NET Type Internals - From a Microsoft CLR Perspective - The Cod This article contains technical information about implementation of different categories of types (Value Types, Reference Types, Delegates, etc) in Microsoft CLR 2.0 (hereafter called as CLR). The concepts that are presented in this article are based on my analysis and study of type behavior in .NET using Son Of Strike (SOS) debugging extensions for VS.NET 2005 and the C# compiler. These concepts are also discussed in different MSDN blogs, MSDN articles and books. This article assumes that the readers have a working knowledge on a different category of types in .NET. In .NET there are two main classifications of types and every type is derived from a Root Reference Type named System.Object (directly or indirectly through another base type). Apart from simple differences on whether the instances of these types are allocated on stack or heap there are core internal design differences in the definition, behavior and instances of these types. Value Types are allocated on stack.

C# Preprocessor Directives (C#) This section contains information about the following C# preprocessor directives. #if #else #elif #endif #define #undef #warning #error #line #region #endregion #pragma #pragma warning #pragma checksum See the individual topics for more information and examples. Although the compiler does not have a separate preprocessor, the directives described in this section are processed as if there were one. A preprocessor directive must be the only instruction on a line. Deadlock Detection in Existing Code - The Code Project - .NET Introduction Deadlocks are common problems in multi-threaded programming. When it comes to multithreading development, the most common problem developers are facing is critical sections. It is not uncommon to use more than a single lock, but if one does not pay attention to the order of the locks, or to the context in which they are being called (e.g., from within a callback), deadlocks will form. (There are many reasons for deadlocks to occur other than the obvious critical section, e.g., two threads that are waiting for each other to signal an event, but we will not discuss them here). As with anything that is related to threads, timing is everything. What if we could make the rare case the normal case? The attached ZIP file contains a DLL that does exactly that. Note that there is no need for the deadlock to really occur; rather, it is only important that suspected flows (or all flows) will be performed at least once. Using the code Analyzing the stacks (slockimp based)

C# Object Clone Wars : C# 411 Cloning C# objects is one of those things that appears easy but is actually quite complicated with many “gotchas.” This article describes the most common ways to clone a C# object. Shallow vs. Deep Cloning There are two types of object cloning: shallow and deep. Hence, a reference in the original object and the same reference in a shallow-cloned object both point to the same object. ICloneable Interface The ICloneable interface contains a single Clone method, which is used to create a copy of the current object. public interface ICloneable { object Clone(); } The problem with ICloneable is that the Clone method does not explicitly specify whether it is performing a shallow or deep copy, so callers can never be sure. The ICloneable interface contains one member, Clone, which is intended to support cloning beyond that supplied by MemberWiseClone… The MemberwiseClone method creates a shallow copy… Type-Safe Clone One way to avoid the cast is to provide your own type-safe Clone method. 1. 2. 3.

Implementing INotifyPropertyChanged with DynamicProxy2 22:30 and still working on various bits and bobs, so I thought I'd take a well deserved break to tell you about one nice way to implement INotifyPropertyChanged, the one and only interface any class that will be bound in WPF should be implemented. I used to (circa .net 2) not see a bit issue in this kind of code. public class MyClass : INotifyPropertyChanged private string _myValue; public event PropertyChangedEventHandler PropertyChanged; public string MyValue get return _myValue; set _myValue = value; RaisePropertyChanged("MyValue"); protected void RaisePropertyChanged(string propertyName) if (PropertyChanged ! PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); But I've been badly spoiled by automatic properties in C# 3.0, and this all sounds like a lot of noise. public class MyDreamClass : INotifyPropertyChanged public string MyValue { get; set; } Definitly much cleaner. The process is quite simple. First, let's clear up our class. public class MyBetterClass : INotifyPropertyChanged

A Library for Writing/Building Scripts in C#. Free source code a Download source (version 1.0.1) - 2 MB Introduction ClockWork Script Builder is a .NET library (2.0) to make the building of scripts more readable and structured. An extendible architecture lets developers add script building blocks (Script Items) for languages such as the ones provided in the download for JavaScript (Js) and XML (Xs). Background Recently I started playing with ExtJs and I ended up needing to build large chunks of JavaScript from .NET code. Using the Code (CHM documentation is available from Tony's Wibbles.) In its simplest form, it is a way to join a bunch of strings together so that each is written on a new line: Script script = Sb.Script( "line 1;", "line 2;", "line 3;" ); string result = script.Render(); Console.WriteLine(result); It results in the following output: line 1; line 2; line 3; However there's a lot more to it. This example demonstrates the use of some core layout items. This results in the following output: Script Languages Points of Interest DOM & Rendering ScriptIf

Steve Heberts dotMath Library - Home The following page is focused on understanding how to use dotMath in your application and get up and running in the shortest amount of time. If you are using the compiled assembly library, be sure to create a reference to the .Math library and include the library DLL in your distribution package. Scenario 1: Evaluating an Expression In order to take an expression without variables stored in a string – sExpression – and evaluate its value we need the following set of code. sExpression = “4+3”; EqCompiler oCompiler = new EqCompiler( sExpression, true); oCompiler.Compile(); double dValue = oCompiler.Calculate(); If you do not perform the ‘.Compile()’ step, it will be executed automatically the first time you perform ‘.Calculate();’. Scenario 2: Evaluating an Expression with Variables Taking advantage of expressions with variables involves discovering the variables and setting them before performing the .Calculate() step. Scenario 3: Adding user-defined functions to the compiler’s syntax.

Automating Undo/Redo with .NET Generics - The Code Project - C# Introduction This article introduces a reusable library that can equip any action in your application with the undo/redo feature. You may use complex data structures and complex algorithms with no care about how they will be rolled back on user demand or after an error. Background If you've ever developed a graphic editor or designer for complex data, you've encountered the daunting task of implementing an undo/redo feature supported throughout the application. This project has been published on CodePlex so that everybody can contribute to it and gain from it. Using the Code There are two points of good news. UndoRedoManager.Start("My Command"); myData1.Name = "Name1"; myData2.Weight = 33; myData3.MyList.Add(myData2); UndoRedoManager.Commit(); All of the changes made in the code block above can be rolled back with one line: UndoRedoManager.Undo(); The next line allows enforcing of the changes again: UndoRedoManager.Redo(); There are three vital differences in these code snippets: Implementation

Related: