0% found this document useful (0 votes)
3 views

C#.NET Classes and Objects

In C#, a class is a data structure that combines various data members into a single unit, declared using the 'class' keyword. An object is an instance of a class, created using the 'new' keyword, allowing access to the class's data members and functions. The document provides an example of a 'Users' class with properties and methods, demonstrating how to create and use an object in a C# program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C#.NET Classes and Objects

In C#, a class is a data structure that combines various data members into a single unit, declared using the 'class' keyword. An object is an instance of a class, created using the 'new' keyword, allowing access to the class's data members and functions. The document provides an example of a 'Users' class with properties and methods, demonstrating how to create and use an object in a C# program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C# Class

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.

public class users {

// Properties, Methods, Events, etc.

}
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.

Users user = new Users();


Following is the example of creating objects in c# programming language.

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);
}
}
}

You might also like