What is the win-based application developing with C# equivalence of startup module we had in vb6.0? a static class ? or what?
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
What is the win-based application developing with C# equivalence of startup module we had in vb6.0? a static class ? or what?
In the properties page under "Application" there is a setting called "Startup object" which can be used to set the object with a static method named Main that will be called. This is a simple UI on the C# compiler s /main switch.
Yes, a static class named Program with a static Main() function. It is hard to miss when you look at the code auto-generated by a project template. Open Program.cs
it is static void Main() {}
, which can be found in different places depending on what type of app it is e.g.:
Winforms - it is usually found in Program.cs
WPF - it is usually found in App.cs
too long for a comment
@odiseh
You almost certainly are talking about a Windows Forms project : and most of the comments here did /are going to/ assume you are using Visual Studio, as I will. If you are using another development environment, then please let us know : that may be relevant (?).
So, create a sample WinForms project in Visual Studio : look at the Program.cs file and its structure : that s the default, as other answers here have pointed out. My guess is that : if you are "coming from" VB6, you already know what the [STAThread] attribute is about, and what "single apartment threading" is.
The default startup format embodied in "Program.cs" is not, however, the only game in town : check out ApplicationContext: MSDN .NET 3.5 ApplicationContext : note that the usages I ve seen of this are pretty rare, and often exotic : if I remember correctly, Chris Sells book on Windows Forms had an interesting ApplicationContext example.
There are some circumstances in which you may wish to modify what s happening in the default Program.cs static class : for example you can show multiple Forms, and use Application.Run() which will mean that when all the Forms you ve shown are closed, your Application will be still running, but with no UI (a problem you can handle with writing code for the FormClosing events and "doing the right thing").
Even SO s very own (highly esteemed) Marc Gravell once published, on another site, an example of an interesting use of NotifyIcon with a modified Main method : some choice "early vintage" Gravell
And do please ignore the horrible "smell" coming from the bizarre example I post below of what "you can get away with" : if there were any justice in the world, that example should result in a "blue screen of death," at least.
comments :
@Martinho : you are correct when you imply there is no "static" class absolutely required for a WinForms project startup. But I must respectfully disagree with the statement there s no "class" requirement. The static method Main, imho, must be inside a Class !
A brief demonstration of what a "monstrosity" you can actually "get away with" in .NET 4.0 : this will actually compile, and run, and put up an instance of Form1 :
using System;
using System.Windows.Forms;
// your mother told you to never do this !
namespace WindowsFormsApplication7
{
public struct Class1
{
public static int Main(string[] someWastedBunchOfStrings)
{
(new Form1()).ShowDialog();
return 1;
}
}
}
Kids : please don t try the above at home : they ll put you on Ritalin or Prozac :)
static void Main() within any static class, but you can change it, as you could change entry point in VC6.
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...