- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to instantiate a class in C#?
Use the new operator to instantiate a class in C#.
Let’s say our class is Line. Instantiation will create a new object as shown below −
Line line = new Line();
Using the object, you can now call the method −
line.setLength(6.0);
Let us see the example −
Example
using System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); } } }
Output
Object is being created Length of line : 6
- Related Articles
- How to instantiate member inner class in Java?
- How to instantiate a static inner class with reflection in Java?
- How to instantiate delegates in C#?
- How to declare and instantiate Delegates in C#?
- How to convert a class to another class type in C++?
- How to inherit a class in C#?
- How to write a singleton class in C++?
- How to create a static class in C++?
- How we can instantiate different python classes dynamically?
- Is it possible to instantiate Type-parameter in Java?
- How can Tensorflow be used to instantiate an estimator using Python?
- How can I instantiate a dictionary in JavaScript where all keys map to the same value?
- How to call a parent class function from derived class function in C++?
- How to call a method of a class in C#
- How to initialize const member variable in a C++ class?

Advertisements