
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Object Initializer in C#
Initialize an object of a class with object initialize.
Using it, you can assign values to the fields at the time of creating an object.
We created Employee object and assigned values using curly bracket at the same time.
Employee empDetails = new Employee() { EID = 10, EmpName = "Tim", EmpDept = "Finance" }
Now access the values of the Employee class. For example, for name of the employee.
empDetails.EmpName
Let us see the complete code −
Example
using System; public class Demo { public static void Main() { Employee empDetails = new Employee() { EID = 10, EmpName = "Tim", EmpDept = "Finance" }; Console.WriteLine(empDetails.EID); Console.WriteLine(empDetails.EmpName); Console.WriteLine(empDetails.EmpDept); } } public class Employee { public int EID { get; set; } public string EmpName { get; set; } public string EmpDept { get; set; } }
Output
10 Tim Finance
Advertisements