Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

What does the word ‘new’ mean exactly in C#?

- 22.03.20 | 29.03.20 - ErcanOPAK

In C# (and many other languages) you will find that you use new often as it is fundamental to using types and classes. The new keyword does pretty much what it says: it will create a new instance of a class or type, assigning it a location in memory.

Let me try and illustrate this.

Company company;

Here we have created a variable of the Company class, but note that it isn’t assigned to anything. If you try to use this now you will get an exception as it is currently null.

Let’s create a new instance of Company instead:

Company company = new Company();

This line is much more useful. First of all we are assigning the variable company to a new instance of the type Company. The new keyword instructs the application to instantiate a new instance of Company. Note that we are required to add parentheses to the end of the class name as this is calling a special function known as a constructor.

The constructor can contain logic that allows the class to be instantiated to support any desired functionality of the class.

A basic example of a constructor:

public class Company {
 
    // Public properties
    public int CompanyID;
    public string CompanyName;
    public List<Employee> Employees;
 
    // Constructor
    public Company() {
        Employees = new List<Employee>;
    }
 
    ...
 
}

Here, the constructor is being used to instantiate the list of employees to ensure that when using the property that you do not get a NullReferenceException.

You can create multiple constructors, as long as they have unique signatures (the parameters in the constructor cannot match the pattern of parameters in another constructor).

public class Company {
 
    // Public properties
    public int CompanyID;
    public string CompanyName;
    public List<Employee> Employees;
 
    // Constructor
    public Company() {
        Employees = new List<Employee>;
    }
 
    public Company(int companyId) {
        LoadCompany(companyId);
    }
 
    ...
 
}

You can now see that we’ve got two constructors. Calling new and passing an integer to the constructor will call the second constructor, which in this case will call a function that will load the company data (just an example, you can load data in other ways).

When you want to use this constructor, change your code, simply, to (passing the relevant integer):

Company company = new Company(1337);

One special thing about constructors is that they can be used to do things that you cannot do with an already instantiated class: you can write to readonly properties.

So, whenever you want to create a new instance of a type, you will need to call new. It is a very simple keyword, but is very powerful as it essentially does all of the heavy lifting of object creation for you.

Thanks to Dan Jackson for this great answer.

Reference

Related posts:

Constructors and Its Types in C#

Static State Causes Test Flakiness

ASP.NET Core “Middleware Never Executes” — The Ordering Trap

Post Views: 41

Post navigation

Determine If string contains more than 1 value in C#
Book Suggestions: The Goal & The Phoenix Project

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com