Tuesday, July 28, 2009

Visual C# 2008 "static", "void", and "string[] args"?

Hi,


I just started to learn visual C# 2008, I had no previous experiences in programming and I don't understand these codes: "static", "void", and "string[] args". Anyone can explain? Thanks!





P.S I searched in MSDN but it just confused me more..

Visual C# 2008 "static", "void", and "string[] args"?
i'll start with void


void means that the functions doesn't return any thing to the caller


ex.


void sayhello(string name)


{


Console.Writeline("helloo {0}",name);


}


whn i call this function the function will print the hello messege and exits there is nothing returned


bu if i define a function like this:


int sum(intx , int y)


{


z=x+y;


return z;


}


this means that the function have a return type of int(intger)


and when i call this function it returns an intger number


--------------------------------------...


string[] args


when u run a program from a command line u can use arguments with it


ex.


c:\%26gt;hello.exe jack NY


jack and NY r called arguments


so u want to have access to this arguments in ur program


these arguments r stored in the array that is defined by string[] args


so jack will be in args[0] and NY will be in args[1]


--------------------------------------...


static


this 1 is alittel difficult


C# is and object orianted language, that means every data and function in ur program must be stored in an object and this object needs some function to create it


but if that is true then how the 1st object can be created if it needs a function to create it and there r no functions allowed outside an object (the problem of the egg and the chicken, which is first)


so the ppl that made C# "microsoft" decided that they will allow a special type of functions to work without needing an object


and to mark this function we write static before the function name


that means this function don't need an object to run


static void main(string[] args)


this means that this function will run with no need for an object and won't return any thing and stores the command line arguments in string[] args


i hope u didn't get lost in the middle


note :if u want more info then ask again and i'll try to answer u
Reply:static: it means that the variable or function is globally available, meaning any piece of code can access it (depending on access modifiers, a private or protected static member of a class would only be accessible within that class). Also is important to note is that static variables are allocated even before the main function (the entry point) is called, and the order of their allocation and deallocation cannot be determined. You would typically use static variables and functions for anything that needs to be accessed anywhere. The Main function is static because the entry point can't be tied to an object.





void: it's an object type of sorts, specifically not an object. It means nothing, literally. If you see a void function, it means it doesn't return anything.





String[] args: this is a variable declaration, specifically an array (that's what the [] means, if their was a number between the [] it would indicate the number of elements in the array). It is an array (or collection) of strings whose size is not specified.
Reply:Try to read a C# tutorial or you'll be lost every program that you try to create:





http://www.csharp-station.com/Tutorial.a...


No comments:

Post a Comment