dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersSameer Sayani
Ranjith Kumar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » .NET Framework

Classes and Objects in C#


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 10


C# is an Object-Oriented Programming Language. Class is Template used to define the properties and Actions performed by the Object. Object is an Instance of a class. Object are used to access the public members of the class.



 


Introduction


C# is a Object-Oriented Programming Language. Class is Template used to define the properties and Actions performed by the Object. Object is an Instance of a class.

Class


1. It is Template used to define the Properties and Actions of an Object.
2. Properties are represented by using variables.
3. Actions are implemented by using Methods(Functions).
4. In C# all the programming statements are placed inside the class Definition.
5. Keyword "class" is used in the class definition.
6. By Default in C# all the Class Members are private
Syntax

class classname {
// Variable Declarations
// Method definitions
}

Object


1. It is an Instance of a class.
2. When the Object is created the variables defined in the class will be binded to the Object.
3. Initialized Object is used to access the public variables and Methods in the class.
Syntax

classname objectName = new constructorname();

Example
Define a class called Bike with two variables model and brand. Define a Methods to Assign the value and also to display the values.

// Example: Classes and Objects
class Bike {
// variable declaration
String model;
String brand;

// Method to assign the bike details(Without parameter)
public void assignBikeDetails() {
model = "CT-100";
brand = "Bajaj";
}

// Method to assign the bike details (With parameter)
public void assgnBikeDetails1(String m, String b){
model = m;
brand = b;
}
// Method to display the Bike details
public void showBikeDetails() {
System.out.println("Model = " + model);
System.out.println("Brand = " + brand);
}
} // End of the class definition

// Implementation class
class ClassDemo {
public static void Main() {
// Object Creation
Bike objBike;
// Object Initialization
objBike = new Bike();
// Invoking the Method without parameter
objBike.assgnBikeDetails();
objBike.showBikeDetails();

// Object Creation and Initialization
Bike objBike1 = new Bike();
// Invoking the Method with parameter
objBike1.assgnBikeDetails1("Splendor", "Hero-Honda");
objBike1.showBikeDetails();
}
}

Note:
1. Variable declared inside the class is called as an Instance variable.
2. new Operator dynamically allocates the memory for the object.





Did you like this resource? Share it with your friends and show your love!


Responses to "Classes and Objects in C#"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Control on visibility in C#
    Previous Resource: How Method Hiding is helpfull to development.
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.