We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Wednesday, June 24, 2015

Structures

A structure is a value type of data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.
It is also User-Defined type same as a class which contain every member of a class can contain like variables, methods, constructors etc,
  • A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data.
  • When the struct is assigned to a new variable, it is copied.
  • The new variable and the original variable, therefore, contain two separate copies of the same data.
  • Changes made to one copy do not affect the other copy.
How to declare a structure?

Syntax: [<modifiers>]struct<name>
{
             //Statements:
}
public struct Employee
{
     public int EmpId;
     public string EmpName;
     public double EmpSalay;
};

Features of Structures
  • A structure or struct can contain only fields, methods, etc.
  • Instruct we can't initialize the variable whereas in class it can be possible
  • It must be initialized either through function or using the object.
  • Struct can't have a constructor.
  • struct can't support the concept of inheritance.
  • We can derive a structure from an interface.
  • We can't inherit from a sealed class
  • A structure could not inherit from a class, the reverse is also true.
  • You can't declare a struct as sealed, because by default it is sealed.
  • A structure declares as abstract can't be used as a base class, i.e. be used in a derivation.
  • No destructor for a struct.
Example

struct Employee
    {
        public int EmpId;
        public string EmpName;
        public double EmpSalary;
        public void Display()
        {
            Console.WriteLine("Employee Id{0}");
            Console.WriteLine("Employee  Name{0}");
            Console.WriteLine("Employee Salary{0}");
        }
        public class MainClass
        {
            public static void Main()
            {
                Employee Emp;
                Emp.EmpId = 1001;
                Emp.EmpName = "mithilesh";
                Emp EmpSalary = 1000;
                Emp.Display();
                Console.ReadLine();
            }
        }

    }


C# Enumeration

Enums are strongly typed constants. They are essentially unique types that allow you to assign symbolic names to integral values. In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. Along the same lines, integral types and enums are not implicitly interchangeable. All assignments between different enum types and integral types require an explicit cast.
Enums lend themselves to more maintainable code because they are symbolic, allowing you to work with integral values, but using a meaningful name to do so. For example, what type of code would you rather work with - a set of values named North, South, East, and West or the set of integers 0, 1, 2, and 3 that mapped to the same values, respectively? Enums make working with strongly typed constants via symbolic names easy.
Enums are value types, which means they contain their own value, can't inherit or be inherited from, and assignment copies the value of one enum to another. You will see in this lesson and elsewhere that enums are used and referred to with both lower case, enum, and upper case, Enum. The relationship between the two is that the C# type, enum, inherits the Base Class Library (BCL) type, Enum. Use the C# type, enum, to define new enums and use the BCL type, Enum, to implement static enum methods.

Enums type can be an integer (float, int, byte, double, etc.). But if you used beside int it has to be cast.

enum is used to create numeric constants in the .NET framework. All member of the enum is of the enum type. There must be a numeric value for each enum type.

Example

    namespace enum
{
    enum color
    {
        green,
        blue,
        yellow,
        red,
    };
    class Program
    {
        static void Main(string[] args)
        {
            color mycolor = color.red;
            switch (mycolor)
            {
                case color.green:
                    Console.WriteLine("This is green color");
                    break;

                case color.blue:
                    Console.WriteLine("This is blue color");
                    break;

                case color.yellow:
                    Console.WriteLine("This is yellow color");
                    break;

                case color.red:
                    Console.WriteLine("This is Red color");
                    break;
            }
            Console.ReadLine();
        }
    }


    }

output:
This is Red color

Features of Enumeration
  • enums are enumerated data type in c#.
  • enums are not for end-user, they are meant for developers.
  • enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members is the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • enum values are fixed. enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System. Enum and thus we can use System.Enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.

What are the difference between class and structure

Structure

  • Struct are value types.
  • They can be empty but a null cannot be assigned to Struct
  • Structs cannot contain explicit parameterless constructors.
  • It doesn’t support Inheritance and cannot use the protected or protected internal modifier.
  • A Struct always has a built-in public default constructor.
  • Cannot use the “as” operator.
  • Cannot have a destructor
  • Cannot be abstract
  • These are used while representing smaller volume of data.
  • All are a predefined data type which comes under value type category were internally implemented as struct under System namespace
  • Can contain variable declaration but those variable can be initialized either under constructor or assign value referring to the variable through an object

Class

  • Classes are reference types.
  • Classes can be nothing – the reference can point to a null.
  • Classes can have explicit parameterless constructors
  • Supports Inheritance
  • Cannot inherit from another class but can implement an interface
  • Can use all access modifiers
  • A class can use “as” operator.
  • Can have a destructor.
  • Can be abstract
  • These are used while representing huge volumes of data.
  • All pre-defined data type that come under reference type category where internally implemented as classes
  • Can contain variable declarations and those variables can be initialized at the time of declaration

No comments: