Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 6 April 2015

Can we have multiple Main methods in one class

Today in Engineer's World, we are discussing a very important topic of C# - Multiple Main Method in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 

MULTIPLE MAIN METHODS IN A CLASS

This is the most asked Interview Question. 
Can we have multiple main methods in one class?
Answer is Yes. We can use more than one Main Method in C# program by making the entry point one by one. 

To understand more clearly follow the easy steps:

Step 1: First open your Visual Studio -> Go to File Menu -> New project -> Select Console Application -> Name Your Application -> Browse the path where you have to save your project ->click OK.
Now, write the same piece of code in your program.cs file to understand it better.
Figure-1

Step 2: Now, as you have written the code. Press F6. 
You are going to face an error which says that " Specify the entry point to your project ". See Figure-2
 
Figure-2

This may happen because you have two Main Method. But compiler doesn't know that which main method take as an entry point. Either the Program1 or the Program2. 

Click imaginationhunt.blogspot to see latest Blogs

Step 3: So, for solving this issue what you have to do is, you have to set an entry point. 
Now, How to create entry points?
To create entry points Right Click on Solution Explorer -> Go to Properties. See Figure-3   

Figure-3

Here, under Startup object: You see that both the classes Program1 and Program2 are present. Select object for Program1. And Press Ctrl+S(Save)
 
Figure-4

Step 4: Now, Press F6. And check the status bar it is showing Build Succeeded. Now Debug the Program by pressing Ctrl+F5. Figure-5 shows you the output.

Figure-5

Step 5: Again go to properties and this time select Program2 object in the Startup Project. Save it(Ctrl+S). 
 
Figure-6

Press F6. Build Succeeded. Now Debug the Program by pressing Ctrl+F5. Figure-7 shows you the output for Program2 method.
 
Figure-7

Hence, by this way we can have multiple Main methods in one class.

To read Decision making C# concept - Click here.
 
Related Questions:-

#newtoprgm try first program

Q-1 Does Startup project may contain only Main method class object or they may contain any method class object that is going to use in this program?
Ans- For this we have created a new class i.e. Program3, which contain method Path(). Saving it(Ctrl+S)


Go to Properties -> Startup project. Check whether the class Program3 is created. No. You can find only two classes which contain Main Method() one is the Program1 and the other is the Program2.


Hence, we can say that Startup project contain only Main method() class object.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...
Read More »

Friday, 3 April 2015

UNDERSTANDING DECISION MAKING STATEMENT IN C#

Today in Engineer's World, we are discussing a very important topic of C# - Decision Statements in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs

Previous - First C# Program

Decision Statement

Decision making statement is very useful in cases when we find ourselves stuck in some really bad situation. In general scenarios, we find out possible causes for such scenarios and on behalf of that we make our decision.

Let's understand this by taking a general example
Suppose we decide to buy a guitar. But, the problem here is how we would pay the cost?
 
Figure-1
 
Similarly, we programmers on the basis of some test cases and conditions find out a way to solve such problem. 

If you are new to .NET and C# try to understand by coding. And ask us.

In C# there are following 4 types of decision making statements: -

1. if statement 
An if statement consists of a Boolean expression followed by one or more statements. If the value is true, then if block is executed otherwise next statement would be executed. 
 
Syntax: The Syntax for if statement is
 
Figure-2

Practical Implementation:
Try this example to understand better
 
Figure-3
 
This is not an efficient way of writing statement.

2. if-else statement 
An if-else statement means you can use one if accompanied with else if statement when the condition fails. If the value is true, then if block is executed otherwise next statement would be executed. 

Syntax: The Syntax for if-else statement is
 
Figure-4

Practical Implementation:
Try this example to understand better
 
Figure-5

3. nested if statement
Nested if means you can use one if or else if statement inside another if or else if statement. If the value is true, then if block is executed otherwise next statement would be executed. 

Syntax: The Syntax for nested if statement is
 
Figure-6

Practical Implementation:
Try this example to understand better
 
Figure-7

4. switch
A switch statement allows a variable to be tested for equality against a list of values.
Note: switch accept constant value in cases, otherwise it may result in error. 

Syntax: The Syntax for switch statement is
 
Figure-8

Practical Implementation:
Try this example to understand better
 
Figure-9


For any query, comment us below.


Related Questions: -


Q-1 Select the output for following set of code:
using System;

namespace AspnetSolutions
{
    class Switch
   {
         public static void Main()
         {
              int i = 2, k = 3;
              switch (i - k)
              {
                    case -1:
                                   ++i;
                                   ++k;
                                   break;
                    case 2:
                                   --i;
                                  ++k;
                                  break;
                    default:
                                  i += 3;
                                  k += i;
                                  break;
              }
             Console.WriteLine(i + "\n" + k);
             Console.ReadLine();
        }
     }
}

A) 2 3
B) 3 2
C) 3 4
D) 5 10
Ans. Option C) is correct.
Explanation: (2-3)= -1, so case -1 will be selected. And inside this case the value of i and k will increment to i=3 and k=4.

Q-2 Does switch case work properly if we remove the default statement from the code?
Ans. Yes, removing default will not cause any error, code will run successfully. Actually, we use default statement for our own convenience. Suppose if neither of the cases will support then it may execute the statement for default. That's why we use default cases and try to make habit to use them.

Click imagination hunt to see latest Blogs

Keep learning and sharing…
Read More »

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...