Skip to main content

Posts

New in C#6

1. $ sign The purpose for it is to simplify string based indexing and that is all. its not some thing like current dynamic features of C# since it internally uses regular indexing functionality. to understand lets consider following example: var col = new Dictionary() { // using inside the intializer $first = "Hassan" }; //Assign value to member //the old way: col["first"] = "Hassan"; // the new way col.$first = "Hassan"; 2. Exception filters: Exception filters are already supported in VB compiler but now they are coming into C#. exception filters lets you specify a condition for a catch block. the catch block gets executed only if the condition is satisfied, this one is my favorite feature, so let's look at an example: Collapse | Copy Code try { throw new Exception("Me"); } catch (Exception ex) if (ex.Message == "You") { // this one will not execute. } catch (Exception ex) if (ex.Message == "Me") { // this one ...

Deligates

Delegates A delegate is a type that represents references to methods with a particular parameter list and return type.When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance. When and Why to use Delegates A delegate can be seen as a placeholder for a/some method(s). By defining a delegate, you are saying to the user of your class "Please feel free to put any method that match this signature here and it will be called each time my delegate is called". Typical use is of course events. All the OnEventX delegate to the methods the user defines. Delegates are useful to offer to the user of your objects some ability to customize their behaviour. Most of the time, you can use other ways to achieve the same purpose and I do not believe you can ever be forced to create delegates. It is just the easiest way in some situations t...

Web Services

Web Services A Web Service is programmable application logic accessible via standard Web protocols. One of these Web protocols is the Simple Object Access Protocol (SOAP). SOAP is a W3C submitted note (as of May 2000) that uses standards based technologies (XML for data description and HTTP for transport) to encode and transmit application data. Consumers of a Web Service do not need to know anything about the platform, object model, or programming language used to implement the service; they only need to understand how to send and receive SOAP messages (HTTP and XML). Soap Message A SOAP message consists of several elements, most notably an envelope. The envelope encapsulates the data transmitted within the SOAP message. Below is a simple SOAP message complete with HTTP headers: POST /demo/MSDN/PerfCounter.asmx HTTP/1.1 Connection: Keep-Alive Content-Length: 150 Content-Type: text/xml Host: localhost User-Agent: MS Web Services Client Protoc...

Value Type and Reference Types

Value Type A data type is a value type if it holds the data within its own memory allocation. Value types include the following: All numeric data types Boolean, Char, and Date All structures, even if their members are reference types Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong Every structure is a value type, even if it contains reference type members. For this reason, value types such as Char and Integer are implemented by .NET Framework structures. You can declare a value type by using the reserved keyword, for example, Decimal. You can also use the New keyword to initialize a value type. This is especially useful if the type has a constructor that takes parameters. An example of this is the Decimal(Int32, Int32, Int32, Boolean, Byte) constructor, which builds a new Decimal value from the supplied parts. Reference Types A reference type contains a pointer...

MethodInvoker Delegate

MethodInvoker Delegate public delegate void MethodInvoker() Remarks MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's Invoke method, or when you need a simple delegate but do not want to define one yourself. Examples The following code example demonstrates how to use a MethodInvoker to call a method that updates the title bar of the application form. C# public partial class Form1 : Form { public Form1() { // Create a timer that will call the ShowTime method every second. var timer = new System.Threading.Timer(ShowTime, null, 0, 1000); } private void ShowTime(object x) { // Don't do anything if the form's handle hasn't been created // or the form has been disposed. if (!this.IsHandleCreated && !this.IsDisposed) return; ...

Function pass by value vs. pass by reference

Function pass by value vs. pass by reference I will call what you are passing in a to a function the actual parameters, and where you receive them, the parameters in the function, the formal parameters. They are also called actual and formal arguments. When passing parameters, what it is called and what happens can be confusing. It is less essential that you call it the "correct" thing than you know exactly what is happening. It is critical to have a good mental model, a valid memory picture of the process. Recall that when you call a function, a chunk of memory called an activation record is allocated. Critical to the discussion here is that this memory holds the formal parameter values and function local variables. By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the paramet...

Serializing Objects in C#

Serializing Objects in C# In simple words serialization is a process of storing the object instance to a disk file. Serialization stores state of the object i.e. member variable values to disk. Deserialization is reverse of serialization i.e. it's a process of reading objects from a file where they have been stored. In this code sample we will see how to serialize and deserialize objects using C#.