C#.NET Classes and Objects
C#.NET Classes and Objects
In c#, Class is a data structure and it will combine various types of data members such
as fields, properties, member functions, and events into a single unit.
Declaring a Class in C#
In c#, classes are declared by using class keyword. Following is the declaration of class in c#
programming language.
}
If you observe the above syntax, we defined a class “users” using class keyword
with public access modifier. Here, the public access specifier will allow the users to create
objects for this class and inside of the body class, we can create required
fields, properties, methods and events to use it in our applications.
C# Object
In c#, Object is an instance of a class and that can be used to access the data members and
member functions of a class.
Creating Objects in C#
Generally, we can say that objects are the concrete entities of classes. In c#, we can create
objects by using a newkeyword followed by the name of the class like as shown below.
using System;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Users user = new Users("Sush", 30);
user.GetUserDetails();
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
public class Users
{
public string Name { get; set; }
public int Age { get; set; }
public Users(string name, int age)
{
Name = name;
Age = age;
}
public void GetUserDetails()
{
Console.WriteLine("Name: {0}, Age: {1}", Name, Age);
}
}
}