Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:20:28+00:00 2026-06-18T02:20:28+00:00

I am starting to learn c#, but i don’t know when to use new

  • 0

I am starting to learn c#, but i don’t know when to use new keyword, and when not to use it. generally i know why to use it, but as i am looking at some code on the internet, i can see that many times new keyword is not used – and those are places when i would use it.

example:

static void FileInfoClass()
{
    //this usage of new keyword i understand.
    FileInfo somefile = new FileInfo("c:\\test.txt");

    if (!somefile.Exists)
    {
        //this is the place where i would like to use new
        //like FileStream somefileStream = new FileStream();

        FileStream somefileStream = somefile.Create();                
        somefileStream.Close();
        somefile = new FileInfo("c:\\test.txt");
    }


    //same like before
    StreamWriter texttoAdd;            
    texttoAdd = somefile.CreateText();

    texttoAdd.WriteLine("This is a line in the file");
    texttoAdd.Flush();
    texttoAdd.Close();
}

this is the simplest example i can think of.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:20:29+00:00Added an answer on June 18, 2026 at 2:20 am

    I understand your question this way: you know that new can be used to create an object, so you don’t understand why is it not used for every case where a new object “appears” in your code.

    Therefore, you want to know what is different in these two lines:

    FileInfo somefile = new FileInfo("c:\\test.txt");
    

    and

    FileStream somefileStream = somefile.Create();  
    

    Well, the second one is not a direct construction of the object. Actually this command by itself does not create an object, it just calls a function that returns an object.

    Given that the function is called “Create()”, we can guess this function will create an object and return it (or will call another function that create an object and return it). But this is only a convention. Nothing in the language says that function called “Create()” should really create objects.

    So when you use this function, your code is not the one responsible to create the object and therefore you don’t use the keyword new. This pattern is called a Factory method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am thinking of starting to learn Joomla 1.7 but don't know where to
I am starting to learn Python, but I'm forced to use a v2.6.2 interpreter.
This may be a stupid question but im just starting to learn Rail thats
This may be a really dumb question but I'm just starting to learn the
Alright, so I'm starting to learn more about Coco2D, but I'm kinda frusterated. A
I'm just starting to learn jQuery and I'm not sure how to enable a
I am just starting to learn how to use RMI, and I have a
I'm starting to learn about async / await in C# 5.0, and I don't
I am just starting to learn hashtables, and so far, I know that you
Being new to this I really am trying to learn how to keep code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.