
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is the use of ‘new’ keyword in C#?
Use the new keyword to create an instance of the array −
int [] a = new int[5];
The new operator is used to create an object or instantiate an object. Here in the example, an object is created for the class using the new −
Example
using System; namespace CalculatorApplication { class NumberManipulator { public void swap(int x, int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put temp into y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* local variable definition */ int a = 100; int b = 200; Console.WriteLine("Before swap, value of a : {0}", a); Console.WriteLine("Before swap, value of b : {0}", b); /* calling a function to swap the values */ n.swap(a, b); Console.WriteLine("After swap, value of a : {0}", a); Console.WriteLine("After swap, value of b : {0}", b); Console.ReadLine(); } } }
Output
Before swap, value of a : 100 Before swap, value of b : 200 After swap, value of a : 100 After swap, value of b : 200
- Related Articles
- What is the use of "is" keyword in C#?
- What is the use of "placement new" in C++?
- Using the new keyword in C#
- What is the 'new' keyword in JavaScript?
- What is the use of this keyword in TypeScript?
- What is the use of underscore keyword in Java 9?
- What is the use of the LIMIT keyword in MySQL query?
- Use of explicit keyword in C++\n
- New keyword in Java
- What is the const Keyword in C++?
- What is the C# equivalent of C++ friend keyword?
- What is volatile keyword in C++?
- What is the use of MySQL BINARY keyword while performing string comparison?
- Why do we use the params keyword in C#?
- Does static factory method internally use new keyword to create object in java?

Advertisements