How to make a function accepting variable arguments of same type in C#

Here is a simple way by which we can declare a function in C# which accepts variable arguments of a particular type. We have a keyword params in C# which we can use in the parameters in a function.

public class Util
    {
        public static List<int> AddIntegersToList(params int[] integerList)
        {
            List<int> numbers = new List<int>();
            foreach (int item in integerList)
            {
                numbers.Add(item);
            } return numbers;
        }
    }

Now this will accept an array of integers and thus the no of integers which can be passed here is not fixed and hence it fullfills our requirement of a function which can receive variable no of arguments of a single type.

Now for the above function we can use

List<int> listOfIntegers = Util.AddIntegersToList(2, 8, 9, 56, 77);

We can pass here any no of integers.


Boxing And Unboxing in C#

1. Boxing is when we convert a value type into reference type,when we are boxing we are creating reference variables which point to a new copy on the heap. 2. Unboxing is when we convert a reference type to a value type. 3. Best example of boxing and unboxing can be while we store integer variable which is a value type in a ArrayList which is a reference type. 4. And when we retrieve the value stored in ArrayList to an integer then we change a reference type to value type ie we do unboxing.

ArrayList myArrayList = new ArrayList();
Int n = 7;
myArrayList.Add(n);//Boxing of n
n = (int)myArrayList[0];//Unboxing of myArrayList[0]

Two uses of using keyword in C#

There are two uses of using keyword in C# i)Using as Directive in C# can be used for importing namespaces to your classes. ii)Another most useful advantage is it can be used in codeblocks what is does is it calls the Dispose() method automatically to release the resources and also automatically implements the try catch blocks for you.Here it is called a using statement.So the advantage is that we dont have to explicitly write a try-catch block and our code looks tidy.


Installing Photoshop CS3 on Windows Server 2003 Systems

Hi yesterday i was trying to install Photoshop CS3 on a system having Windows Server 2003 installed. But it gives me an error saying that you need a OS equivalent to Windows XP SP2 or greater or Windows Vista. So i found a workaroud which i would like to share with you. Just download a utility from Microsoft called Application Verifier its a small but very benificial utility.Link is http://www.microsoft.com/downloads/details.aspx?FamilyID=C4A25AB9-649D-4A1B-B4A7-C9D8B095DF18&displaylang=en 1.Run the Application Verifier and open the setup.exe file for photoshop cs3 and go to the compatibility tree view column. 2.Go to HighVersionLie and right click it to get properties. appverifier 3.Now suppose if you want to any program assume that you are using Windows XP SP2 then enter like below figure in the boxes that follow properties 4.Click OK And then Save 5.It will then ask you for the debugger to be attached do not attach any debugger and click save . 6.Now if you try installing it will be installed with no problems.


Dotnet and C# Begginner Series Part-1

Important Learning 1. Common Language Specification is a part of Common Type Systems which in turn is a part of Common Language Runtime. 2. We can take any language and if we can have or make a .net aware compiler for that language then we are done ie we can start writing code in that language and it will run on .net framework installed machines.We needn’t worry about the platform now, as .net will take care for that. 3. Single file assemblies and multifile assemblies.The difference is that we can have all our referenced code in a single assembly it is beneficial when we have only a small application and our client needs to download only a small part ie only that assembly and that’s done.But if we are making a big application in which there are thousands of small application so we neednot place everything into a single assembly whereas we can place things in different assemblies the benefit is that if our clients need some network related stuff so he can download that assembly and start working he needn’t download the whole application as such so it reduces the download time and make process faster. 4. What actually happens for those people who don’t know .net and are programmers in other languages is that first we write code in any .net language let’s be concerned now with only the languages which ship with .net.Now all these languages such as VB.Net ,C#,C++/CLI,J#,JScipt.net follow Common Type Specification and Common Language System.So if you write Dim myInt as Integer in vb.net and someone else write int myInt in C# it will be compiled by vbc.exe and csc.exe to same thing ie Sys.Int32 which is the example of Common Type System and thus it means they are same at the Intermidiate Language Level 5. What these compilers do is they generate Intermediate Language which is not instruction set specific ie which is not platform specific ie it does not contain any platform specific information which earlier COM assemblies ie .dll and .exe used to do. 6. One important point which comes here is that the assemblies generated by .net are different then the dll and exes generated by the COM components. 7. COM used to generate platform specific dlls or exe’s but .net generates dll’s and exe’s which contain metadata and CIL . 8. This metadata consists of manifest files which contains information about the assembly like versioning,culture information,and list of externally referenced assemblies which are required to run that assembly. 9. CIL or Common Intermidiate Language or MSIL or IL whatever you can call it is in some sense same like Java Bytecode ie they both are not platform specific. 10. ByteCode in Java contains information which Java Virtual Machine understands and then generate or run the required operating system calls and resource allocation based on operating system and the architecture.For example if I have written in my Java Code that allocate me 2MB of ram.Now the bytecode will ask the virtual machine to allocate the resource now it’s the duty of virtual machine to do the heavy lifting and make system calls or any other thing to allocate the resource. 11. So lets consider different OS only not different architectures os let say x86 architecture,and Windows platform ,now here for allocating RAM the system call could be alloc_mem(amount) so it’s the work of Virtual machine to make this call. 12. On x86 and Linux this system call might change and we could have mem_alloc(totalamount) so again it’s the work of JVM specific to x86 Linux to make that call and execute the code. 13. So a question can be why not have a single JVM which could do all this for all platforms and architecture.In theory we can have single JVM also which can first perform a check for architecture and then for the operating system and then execute in a specific way,but its not necessary because such JVM will be huge in size and too complex and slow and also as it has bigger size it would be a waste of time and network resources for the client to download that. 14. If the client doesnot need any support for Windows , Solaris,Mac,Novell and etc.. he only needs it for linux then why should he download all crap.Thus I think they have different JVM’s there might be other limitations when we come to architecture. 15. So this was the story of JVM similar thing happens here in .net also when we get CIL now when we want to run and execute this code we need a Just In Time compiler which will compile this to the platform specific information. 16. So there are two compilations going on one when the code was compiled from its base language to the IL and another compilation is when the IL code is executed. 17. This second compilation is also managed by .net framework it searches for the platform and make the required calls and allocate the required resources just like we have seen in the concept of JVM 18. For .net languages we have support of Base Class Libraries which are simply collections of pre-defined classes which we can use as starting point and then implement bigger things over them. 19. This BCL is divided into many parts grouped under various meaningful namespaces.


How to change the product key of any windows operating system if you have already installed a key.

You can do it with WinKey Finder with ease.Although you can do it manually by editing some registry key but i think that its not a convinient method. So here is the link where you can donwload this tool and play with it. http://files6.majorgeeks.com/files/f06048518ff8de2035363e00710c6a1d/covertops/WinKeyFinder173_RC2.zip


How to switch between various opened tabs in various Microsoft Products

Today i  found out a shortcut which saved my life earlier on i was not knowing this shortcut. This shortcut Ctrl + Tab is used to switch between various opened tabs in any microsoft product. I have not tested it for everything but as i am a developer it works fine in Microsoft Sql Server Management Studio and Microsoft Visual Studio. Hope this can save life of many developers.


Attaching .mdf files in Sql Server when you dont have a .ldf file

Hi today i was trying to attach an .mdf file which was not having the .ldf file with it, sql server was giving error with it but later i found a workaround. So to attach a .mdf file without its .ldf file just press remove button at the creation windows in your sql server management studio. It will remove the .ldf file location and create a new .ldf file for your database. Here is a link of how to do this. http://img.photobucket.com/albums/v472/Schmedrick/attach.gif


Object Reference not set instance of an object error while creating new silverlight application using Silverlight2 beta 2

Yesterday i was facing problems and got the error saying object reference not set instance of object problem. I think the problem is of skip packages So the common workaround is to start visual studio using the devenv /resetskippkgs And if this also doesnot work for you then here is a very good post to help you do have a look at that. http://weblogs.asp.net/bradleyb/archive/2008/06/09/upgrading-to-silverlight-beta-2-and-visual-studio-2008-sp1-beta.aspx


http://localhost is being redirected to http://www.localhost.com

Hi today i met an ackward situation when i was trying to run my localhost by running http://localhost but to my surprise it redirected me to http://www.localhost.com This was only happening when i was trying running my localhost while i was connected to the internet. www.localhost.com domain is registered to Matthew Seidl and have an ip address of 10.11.12.13 so if you will ping www.localhost.com it will ping to this address. So after doing a lot of research i found out the reason for this culprit. The most important point was that if you have bychance enabled IPV6 support in Windows XP then please uninstall it as it is doing all the redirection. If this doesnot works then do check the host entry in your C:/Windows/System32/Drivers/etc folder it must have localhost being mapped to 127.0.0.1 and none other if there is some other entry please delete.