C# Object and Class - Javatpoint

文章推薦指數: 80 %
投票人數:10人

C# Class Example 4: Store and Display Employee Information · using System; · public class Employee · { · public int id; · public String name; · public float salary; ... ⇧SCROLLTOTOP Home .Net C# ADO.NET ASP.NET SQLServer F# AngularJS Node.js Express.js SQL HTML JavaScript Ajax Quiz Projects InterviewQ Comment Forum Training .NetFramework .NETFramework CLR FCL C#Tutorial C#Tutorial JavavsC# C#History C#Features C#Example C#Variables C#DataTypes C#Operators C#Keywords C#ControlStatement C#if-else C#switch C#ForLoop C#WhileLoop C#Do-WhileLoop C#Break C#Continue C#Goto C#Comments C#Function C#Function C#CallByValue C#CallByReference C#OutParameter C#Arrays C#Arrays C#ArraytoFunction C#MultidimensionalArray C#JaggedArrays C#Params C#Arrayclass C#CommandLineArgs C#ObjectClass C#ObjectandClass C#Constructor C#Destructor C#this C#static C#staticclass C#staticconstructor C#Structs C#Enum C#Properties C#Properties C#Inheritance C#Inheritance C#Aggregation C#Polymorphism C#MemberOverloading C#MethodOverriding C#Base C#Polymorphism C#Sealed C#Abstraction C#Abstract C#Interface C#Namespace C#Namespaces C#AccessModifiers C#Encapsulation C#Strings C#Strings C#ExceptionHandling C#ExceptionHandling C#try/catch C#finally C#CustomException C#checkedunchecked C#SystemException C#FileIO C#FileStream C#StreamWriter C#StreamReader C#TextWriter C#TextReader C#BinaryWriter C#BinaryReader C#StringWriter C#StringReader C#FileInfo C#DirectoryInfo C#Serialization C#Deserialization C#System.IO C#Collections C#Collections C#List C#HashSet C#SortedSet C#Stack C#Queue C#LinkedList C#Dictionary C#SortedDictionary C#SortedList C#Generics C#Generics C#Delegates C#Delegates C#Reflection C#Reflection AnonymousFunction AnonymousFunction C#Multithreading C#Multithreading C#ThreadLifeCycle C#Threadclass C#MainThread C#ThreadExample C#ThreadSleep C#ThreadAbort C#ThreadJoin C#ThreadName C#ThreadPriority C#Synchronization C#Synchronization C#WebService WebServicesinC# C#Misc EventsinC# RegularExpressioninC# DateTimeinC# TypeCastinginC# ListBoxControlinC# C#ReadLine()Method DesignPatternsC# C#OperatorOverloading C#NewFeatures C#NewFeatures(40+) C#Programs C#Programs FibonacciSeries PrimeNumber PalindromeNumber Factorial ArmstrongNumber Sumofdigits ReverseNumber SwapNumber DecimaltoBinary NumberinCharacters AlphabetTriangle NumberTriangle FibonacciTriangle C#InterviewQuestions C#InterviewQuestions ADO.NETTutorial ADO.NETTutorial(10+) ASP.NETTutorial ASP.NETTutorial(50+) next→ ←prev C#ObjectandClass SinceC#isanobject-orientedlanguage,programisdesignedusingobjectsandclassesinC#. C#Object InC#,Objectisarealworldentity,forexample,chair,car,pen,mobile,laptopetc. Inotherwords,objectisanentitythathasstateandbehavior.Here,statemeansdataandbehaviormeansfunctionality. Objectisaruntimeentity,itiscreatedatruntime. Objectisaninstanceofaclass.Allthemembersoftheclasscanbeaccessedthroughobject. Let'sseeanexampletocreateobjectusingnewkeyword. Students1=newStudent();//creatinganobjectofStudent Inthisexample,Studentisthetypeands1isthereferencevariablethatreferstotheinstanceofStudentclass.Thenewkeywordallocatesmemoryatruntime. C#Class InC#,classisagroupofsimilarobjects.Itisatemplatefromwhichobjectsarecreated.Itcanhavefields,methods,constructorsetc. Let'sseeanexampleofC#classthathastwofieldsonly. publicclassStudent { intid;//fieldordatamember Stringname;//fieldordatamember } C#ObjectandClassExample Let'sseeanexampleofclassthathastwofields:idandname.Itcreatesinstanceoftheclass,initializestheobjectandprintstheobjectvalue. usingSystem; publicclassStudent { intid;//datamember(alsoinstancevariable) Stringname;//datamember(alsoinstancevariable) publicstaticvoidMain(string[]args) { Students1=newStudent();//creatinganobjectofStudent s1.id=101; s1.name="SonooJaiswal"; Console.WriteLine(s1.id); Console.WriteLine(s1.name); } } Output: 101 SonooJaiswal C#ClassExample2:HavingMain()inanotherclass Let'sseeanotherexampleofclasswherewearehavingMain()methodinanotherclass.Insuchcase,classmustbepublic. usingSystem; publicclassStudent { publicintid; publicStringname; } classTestStudent{ publicstaticvoidMain(string[]args) { Students1=newStudent(); s1.id=101; s1.name="SonooJaiswal"; Console.WriteLine(s1.id); Console.WriteLine(s1.name); } } Output: 101 SonooJaiswal C#ClassExample3:InitializeandDisplaydatathroughmethod Let'sseeanotherexampleofC#classwhereweareinitializinganddisplayingobjectthroughmethod. usingSystem; publicclassStudent { publicintid; publicStringname; publicvoidinsert(inti,Stringn) { id=i; name=n; } publicvoiddisplay() { Console.WriteLine(id+""+name); } } classTestStudent{ publicstaticvoidMain(string[]args) { Students1=newStudent(); Students2=newStudent(); s1.insert(101,"Ajeet"); s2.insert(102,"Tom"); s1.display(); s2.display(); } } Output: 101Ajeet 102Tom C#ClassExample4:StoreandDisplayEmployeeInformation usingSystem; publicclassEmployee { publicintid; publicStringname; publicfloatsalary; publicvoidinsert(inti,Stringn,floats) { id=i; name=n; salary=s; } publicvoiddisplay() { Console.WriteLine(id+""+name+""+salary); } } classTestEmployee{ publicstaticvoidMain(string[]args) { Employeee1=newEmployee(); Employeee2=newEmployee(); e1.insert(101,"Sonoo",890000f); e2.insert(102,"Mahesh",490000f); e1.display(); e2.display(); } } Output: 101Sonoo890000 102Mahesh490000 NextTopicC#Constructor ←prev next→ ForVideosJoinOurYoutubeChannel:JoinNow Feedback SendyourFeedbackto[email protected] HelpOthers,PleaseShare LearnLatestTutorials Splunk SPSS Swagger Transact-SQL Tumblr ReactJS Regex ReinforcementLearning RProgramming RxJS ReactNative PythonDesignPatterns PythonPillow PythonTurtle Keras Preparation Aptitude Reasoning VerbalAbility InterviewQuestions CompanyQuestions TrendingTechnologies ArtificialIntelligence AWS Selenium CloudComputing Hadoop ReactJS DataScience Angular7 Blockchain Git MachineLearning DevOps B.Tech/MCA DBMS DataStructures DAA OperatingSystem ComputerNetwork CompilerDesign ComputerOrganization DiscreteMathematics EthicalHacking ComputerGraphics SoftwareEngineering WebTechnology CyberSecurity Automata CProgramming C++ Java .Net Python Programs ControlSystem DataMining DataWarehouse JavatpointServicesJavaTpointofferstoomanyhighqualityservices.Mailuson[email protected],togetmoreinformationaboutgivenservices.WebsiteDesigningWebsiteDevelopmentJavaDevelopmentPHPDevelopmentWordPressGraphicDesigningLogoDigitalMarketingOnPageandOffPageSEOPPCContentDevelopmentCorporateTrainingClassroomandOnlineTrainingDataEntryTrainingForCollegeCampusJavaTpointofferscollegecampustrainingonCoreJava,AdvanceJava,.Net,Android,Hadoop,PHP,WebTechnologyandPython.Pleasemailyourrequirementat[email protected]Duration:1weekto2weekLike/Subscribeusforlatestupdatesornewsletter



請為這篇文章評分?