How to do this C++'s code with C# ?

  • Thread starter Thread starter John Saunders
  • Start date Start date
J

John Saunders

pronto said:
Hello all.

Follow C++ "console application" printing out "Hello world". How to create
C# console application
with same approach (instance of some class will be created without reference
from main())

regards
pronto

class C1
{
public :
C1::C1()
{
puts("Hello World");
}
}


C1 a1; // that's why you see "Hello world"

int main(int args, char argv[])
{
}

This sounds like a homework assignment.

Take a look at the System.Console class.
 
C1 a1; // that's why you see "Hello world"
int main(int args, char argv[])
{
}

Here you are crating a global class instance on the Stack and not the Heap
as you would with C1 a1 = new C1();

First. You can't allocate reference-objects on the stack in C#, and you
can't declare objects or variables global. So....You can´t do what you are
seeking in C#. But why you wanna do this in a objectoriented language is
another quistion (I dont think you would want too)

Regards
Anders k. Jacobsen, Denmark
 
...
Follow C++ "console application" printing out
"Hello world". How to create C# console application
with same approach (instance of some class will be
created without reference from main())
class C1
{
public :
C1::C1()
{
puts("Hello World");
}
}


C1 a1; // that's why you see "Hello world"

int main(int args, char argv[])
{
}


Not *exactly* the same, but I think as close as you can get:

using System;

public class C1
{
public C1()
{
Console.WriteLine("Hello World");
}
}

class Dummy
{
public static C1 a1 = new C1();

static void Main(string[] args)
{
// Look ma, no hands...
}
}

// Bjorn A
 
class Dummy
{
public static C1 a1 = new C1();

static void Main(string[] args)
{
// Look ma, no hands...
}
}

But ma, im still in class scope and is not visible to the rest of the
system. As you are pointing out quite clearly. You HAVE to be ni a class
scope to create an instance of a class. No way around and no hacks. You have
to write the code in another fashion.

(But Bjorns suggestion is as close as you get wish is no good for you.)

Anders Jacobsen
 
Hello all.

Follow C++ "console application" printing out "Hello world". How to create
C# console application
with same approach (instance of some class will be created without reference
from main())

regards
pronto

class C1
{
public :
C1::C1()
{
puts("Hello World");
}
}


C1 a1; // that's why you see "Hello world"

int main(int args, char argv[])
{
}
 
Flare said:
C1 a1; // that's why you see "Hello world"

int main(int args, char argv[])
{
}

Here you are crating a global class instance on the Stack and not the Heap
as you would with C1 a1 = new C1();

First. You can't allocate reference-objects on the stack in C#, and you
can't declare objects or variables global. So....You can´t do what you are
seeking in C#. But why you wanna do this in a objectoriented language is
another quistion (I dont think you would want too)

Regards
Anders k. Jacobsen, Denmark

This is part of investigation to migrate from C++ to C#. That's the way how
we do some global (per OS process instance) initialization, socket library
for example.


Thanx for advice.
 
Back
Top