How are classes related to objects in .NET Application?

As per object oriented programming, properties and the methods that access these properties are binded together into an entity. This is known as encapsulation.



This entity which wraps properties and its corresponding methods is known as a Class. Here is an example for the class:
class sampleClass {
int dataMember1;
public void setData(int data) {
dataMember1 = data;
}
public int getData() {
return dataMember1;
}
}

In this example, you have a class called sampleClass containing a property called dataMember1. Two methods namely setData and getData are trying to access this property. Hence both the methods are wrapped inside sampleClass. Till this it looks fine. But how do you access a class and its members? For that you have to create an instance of the class and that instance is termed as object of the class. Given below is a sample code to show how to create object for the class sampleClass:

class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj.setData(100);
int data = obj.getData();
Console.WriteLine(“Value of obj’s dataMember1 = {0}”);
}
}
Output of this code will be:
Value of obj’s dataMember1 = 100

In this example, you create an object obj of sampleClass and access setData and getData methods using this object.

This is how classes and objects are related. Object is the instance of the class with which you can access the class members.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.