Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 23 July 2010

Objects in C# by reference

I had a really nice discussion with my colleague last week and I made a very simple to prove the code. This actually proves that the objects in C# are copied by reference. So for example if I have an object defined with new keyword and set to other object of same type by changing first I am changing others as well.

Let take this example here:

I defined a class of Animal:

public class Animal

{

public int Id { get; set; }

public string Description { get; set; }

public Animal(int id,

string description)

{

this.Id = id;

this.Description = description;

}

}

This class I just added in Console application to observe the output and wrote the following code in Program.cs

static void Main(string[] args)

{

Animal Dog = new Animal(1,"Dog");

Animal Cat = Dog;

Animal Bird = Dog;

Console.WriteLine("Dog: " + Dog.Description);

Console.WriteLine("Cat: " + Cat.Description);

Console.WriteLine("Bird: " + Bird.Description);

// Now change Cat description to Cat

Cat.Description = "Cat";

// Print Again

Console.WriteLine("Changed Cat.Description to Cat");

Console.WriteLine("Dog: " + Dog.Description);

Console.WriteLine("Cat: " + Cat.Description);

Console.WriteLine("Bird: " + Bird.Description);

// Now change Bird description to Bird

Bird.Description = "Bird";

//Print again

Console.WriteLine("Changed Bird.Description to Bird");

Console.WriteLine("Cat: " + Cat.Description);

Console.WriteLine("Bird: " + Bird.Description);

Console.WriteLine("Dog: " + Dog.Description);

Console.ReadKey();

}

Now if we look into the code I defined the object Code of type Animal with new keyword and set Object Bird and Cat same as Dog. Now changed the Cat object and then Bird object and the result was:

So by changing any object here as far as none of the object defined as new keyword they all are actually referring to each other which proves that Object in C# copied by reference.









Tuesday, 12 January 2010

Create a Code snippet in C#

What is snippet?

It is a reusability of set of codes for instance if we type ‘fo’ you will see following menu:


And if you press Tab and then again Tab the code block for ‘for’ loop appears like this:


More about snippets click here.

This is called code reusability or code snippet now at this post we are going to talk about how to make our own code snippet like this ‘for’ loop we are going to create our own code snippet for ‘for’ and will name it as ‘forloop’ let’s start:

Open a notepad file name it as ‘foorloop.snippet’ you can also add a XML file in Visual Studio 2008 and type these Tags.

Now copy the following code:

<CodeSnippets

xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Shortcut>

Shortcut>

<Title>

Title>

<Author>

Author>

Header>

<Snippet>

<Declarations>

<Literal>

<ID>ID>

<ToolTip>ToolTip>

<Default>Default>

Literal>

Declarations>

<Code Language="CSharp">

]]>

Code>

Snippet>

CodeSnippet>

CodeSnippets>

Now from start In Header we have ‘Shortcut’ where we define string what we should type to call this snippet, ‘Title’ is for Title of this snippet and ‘Author’ who is the Author of this snippet.

Now come to the snippet section inside snippet tag we have Declarations where we define the variables we ask the user to define it. Where ‘ID’ is the variable name for that definition ‘ToolTip’ is same as Tooltip and default is what should be shown there when this snippet is call. You can have more than one variable so copy the literal as many times.

Inside the Code section we define out set of codes now look at this finished example:

<CodeSnippets

xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Shortcut>

forloop

Shortcut>

<Title>

For Loop

Title>

<Author>

Syed Hashmi

Author>

Header>

<Snippet>

<Declarations>

<Literal>

<ID>aID>

<ToolTip>Starting pointToolTip>

<Default>aDefault>

Literal>

<Literal>

<ID>bID>

<ToolTip>Length of the loopToolTip>

<Default>bDefault>

Literal>

Declarations>

<Code Language="CSharp">

for (int $a$ = 0; $a$ < $b$; $a$++)

{

}

]]>

Code>

Snippet>

CodeSnippet>

CodeSnippets>

Now here you can see we have two variables defined in ‘Declaration’ section one is ‘a’ and other is ‘b’ and we used them in code with ‘$a$’ this is the way we use variable in snippets.

After finishing this save it at any location and then import it from Snippet code manager from tool menu:


And now try typing ‘for’ and will notice ‘forloop’ added in this menu:


Press Tab and Tab and the code block of for loop is there:


This is post is only to show you the depth of snippet there are so many designers out which are easy to use to create own snippet like Visual Studio 2008 Code Snippet Library C# . Here are some Visual Studio 2008 Built-in Code Snippets.

Any suggestion or questions please comment. Thanks