Forum guidelines
AuthorQuestion
New keyword ?
Posted on: 05 Feb 2013
Hello everyone 

This is my 1st day at this amazing forum .

Here comes one of my doubt . well NEW key is used to create an Object of non-static members  RIGHT . Actually problem is in multi-threading NEW keyword is used under a non-static block of a constructor to create new thread like t1 = new thread(method_name) .

So plz Clarify me what's going on here . Hope everything clarifies my doubt :)

Thanks in advance 

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: New keyword ?
Posted on: 06 Feb 2013   Accepted Answer
You're right that the Main() method is always static and that you'd usually (but not necessarily) create an object within it.

However, this doesn't mean that you can only create objects within a static block.

As mentioned in my previoous post, you can create them virtually anywhere.

To illustrate, here's an extended version of my previous program which creates objects in several different places:

using System;
using System.Threading;

class A
{
   public A()
   {
      Thread t = new Thread(SayHello);
      t.Start();
   }

   public void SayHello()
   {
      Console.WriteLine("Hello");
   }
}

class Test
{
   private A a = new A();

   static void Main()
   {
      new A();
      Test t = new Test();
      t.MyMethod();
      Console.ReadKey();
   }

   void MyMethod()
   {
      new A();
   }

The output should be:

Hello
Hello
Hello

Re: New keyword ?
Posted on: 05 Feb 2013  
That line is actually shorthand for this:

   Thread t1 = new Thread(new ThreadStart(method_name));

The Thread class's constructor needs to be passed a ThreadStart delegate which tells it what method to execute when the thread starts.

So when you do this:

   Thread t1 = new Thread(method_name);

the C# compiler automatically creates a ThreadStart delegate based on method_name and passes that to Thread's constructor.



Re: New keyword ?
Posted on: 05 Feb 2013  
Hie

Thanks for the quick reply but my doubt here is regarding NEW keyword that used to create a thread under non-static constructor.

PS:In C# rules we cant create a obj or etc using NEW keyword under non-static block of code i.e constructor .

Thank you

Re: New keyword ?
Posted on: 05 Feb 2013  
I'm not 100% sure what you mean but, if you're asking whether you can create new Threads or other objects within an instance constructor of some other class, then the answer is that you can.

For example, this compiles and runs fine:

using System;
using System.Threading;

class A
{
   public A()
   {
      Thread t = new Thread(SayHello);
      t.Start();
   }


   public void SayHello()
   {
      Console.WriteLine("Hello");
   }
}

class Test
{
   static void Main()
   {
      new A();
      Console.ReadKey();
   }


Re: New keyword ?
Posted on: 05 Feb 2013  
No no dude I may be confusing you a-lot sorry for that .

I just want explanation on NEW keyword just NEW keyword used in thread creation under instance constructor like u said .

I am thinking NEW keyword is used only under static block of code for creating objects or threads etc .. can NEW key used under non-static blocks ?? like above you mentioned under instance constructor ?

Ex:public A()
   {
      Thread t = new Thread(SayHello);
      t.Start();
   } u created a thread under non-static constructor but acc to c# principles we cant create obj or thread under a non-static block .. that' where my doubt pointing to "newkeyword"

Thank you

Re: New keyword ?
Posted on: 06 Feb 2013  
Yes, the new operator can be used to create an object virtually anywhere including within a static or instance constructor, method or property.

What led you to think that it couldn't be used in a non-static block?

Re: New keyword ?
Posted on: 06 Feb 2013  
Humm here it goes ---->

By default main method is static method right . we always used to create object of members under Main right :) so basic rule in Object creation is Object can only be created under Static block like Main,any static method or etc that is must

As obj can created only using NEW key . that fallowed my doubt :)

I hope you got it 

Thank you

Re: New keyword ?
Posted on: 06 Feb 2013  
Thank you thank you very much . my sir in institute made me to believe such silly acquisitions .
Any ways for further doubts will revert back at you :)

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.