i have a question which is quite simple but yet complicated
for me
my question is why do we use void, if it does not return
anything to the compiler?
if it is used for normal display it can also be done by
what is called Console.Write() or Consol.WriteLine() and if
i do not use void with my method then my compiler throws me
an error. if i return a value say integer then i write
public int fun() display of the result can also be done
here
then why is it so necessary to use void with a function and
why so compiler throw us an error if v don't use void
return type?

Answer Posted / sathish

Just to add on that,
i guess, Compiler basically looks the pre-defined syntax or
format rather than just the keyword, like say, it parses
function as ACCESS MODIFIER RETURNTYPE FUNCTION() e.g.
public void test(). Here ACCESS MODIFIER is optional,
becuase bydefault it is PRIVATE, but return type is
mandatory, that is why, eventhough you don't return any
value, it is mandatory to instruct compilter that, the
function does not return anything.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is string nullable in c#?

455


List down the commonly used types of exceptions in .net

496


You are creating a custom usercontrol, some of the newly created properties are shown in the properties window. How you can hide a new property named theme from the properties window?

505


What is the use of regex in c#?

498


Are arrays immutable c#?

521






What's the difference between System.String and System..StringBuilder in C#?

506


What is an event in c#?

502


If I return out of a try/finally in c#, does the code in the finally-clause run?

557


What is namespace in oop?

492


Is c# substring zero based?

506


Explain the different ways a method can be overloaded?

454


How many static constructors are allowed in a class?

484


What is the advantage of extension method in c#?

466


what happens if you inherit multiple interfaces and they have conflicting method names?

496


What is the difference between convert and parse in c#?

455