Hot File

How to Object Creation In C#

View: 256    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

15 point/3 review File has been tested

The object is a thing which is a real creation or implementation of a class (means physical creation of a class), which has physical existence.

Introduction

The object is a thing which is a real creation or implementation of a class (means physical creation of a class), which has physical existence.

It is an instance of a class.

Pro-grammatically an object is a block of memory which has been allocated and configured according to a class during run-time

According to above Mark sheet class, when we create its object it means we are creating or implementing an actual or real mark sheet object which will have physical reality

We can create multiple different types of objects from that Mark sheet class or multiple Mark sheets can be created from same Mark sheet template or blueprint, each Mark sheet will have its own information, attributes and behavior

General form of an Object creation:

ClassName refVariable = new ClassName();

Suppose, a Mark sheet object can be created:

MarkSheet markSheet = new MarkSheet();

Example – 1, a complete Marksheet class:

using System;
namespace csharpBasic
{
 
    // Declare a class MarkSheet.
    class MarkSheet
    {
        /* Following is the initialization of string and double type class attributes/properties, 
           here these attributes are called fields. 
         */
        public string StudentName = "abc";
        public string StudentAddress = "xyz";
        public string Asp = "Asp.net";
        public string Javascript = "JavaScript";
 
        public double AspMarks = 80;
        public double JavascriptMarks = 60;
 
    }    // End class definition scope.
 
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An object of a Marksheet class is being declared.
            MarkSheet markSheet = new MarkSheet();
 
            /* Following statements print the different kind of information of a 
               Marksheet by using the reference variable of type MarkSheet class followed by dot operator (.), 
               dot operator links a class member with a reference variable.
             */
            Console.WriteLine("Student name: {0}", markSheet.StudentName);
            Console.WriteLine("Student address: {0}", markSheet.StudentAddress);
 
            Console.WriteLine("Subject 1: {0}", markSheet.Asp);
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks);
 
            Console.WriteLine("Subject 2: {0}", markSheet.Javascript);
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks);
 
            Console.ReadKey();
        } 
    }
}

Example – 2, the above program can also be written as following:

using System;
namespace csharpBasic
{
 
    // Declare a class MarkSheet.
    class MarkSheet
    {
        /* Notice, in this example we do not initialize class fields in this class,
           we just declared them.
         */
        public string StudentName;
        public string StudentAddress;
        public string Asp;
        public string Javascript;
        public double AspMarks;
        public double JavascriptMarks;
 
 
    }    // End class definition scope.
 
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An object of a Marksheet class is being declared.
            MarkSheet markSheet = new MarkSheet();
 
            /* Notice, following class fields are initialize in main method (not in class itself) 
               with different values. 
             */
            markSheet.StudentName = "abc";
            markSheet.StudentAddress = "xyz";
 
            markSheet.Asp = "Asp.net";
            markSheet.AspMarks = 80;
 
            markSheet.Javascript = "JavaScript";
            markSheet.JavascriptMarks = 60;
 
            // Following statements print the different kind of Marksheet information.
            Console.WriteLine("Student name: {0}", markSheet.StudentName);
            Console.WriteLine("Student address: {0}", markSheet.StudentAddress);
            Console.WriteLine("Subject 1: {0}", markSheet.Asp);
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks);
            Console.WriteLine("Subject 2: {0}", markSheet.Javascript);
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks);
            Console.ReadKey();
        } 
    }
}

In above program examples we define a class in same file but you can also define a class in a separate class file in visual studio (it is recommended)

 

How to Object Creation In C#

How to Object Creation In C# Posted on 06-09-2016  The object is a thing which is a real creation or implementation of a class (means physical creation of a class), which has physical existence. 5/10 256

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 404    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 411    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 452    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 308    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 465    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/3 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 294    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 238    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 259    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd