Adv C#
Programming

1. Introduction to .NET and C# 2. Points to keep in mind for pyramid 3. C# Pyramid Program Example for type 1 4. C# Pyramid Program Example for type 2 5. C# Pyramid Programs on Type 3 6. C# Pyramid Programs of type 4 7. C# Pyramid Programs of type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in C# 11. Destructor 12. Method Overloading 13. Constructor Overloading 14. Inheritance in C# 15. Interface in C# 16. Method Overriding 17. readonly and sealed keyword in C# 18. Exception Handling 19. Delegates 20. Multicast Delegates 21. Events 22. Structure 23. Threads 24. Working with Threads 25. Thread Life Cycle 26. Introducing Multithreading 27. Advantage and limitation of multithreading 28. Thread priority 29. Properties 30. Collections 31. ArrayList Class 32. Hastable Class 33. SortedList 34. Stack 35. Queue Class 36. Generics 37. Unsafe code 38. C# Networking 39. Prgs for Science and Engineer 40. Prg to use alias 41. Prg to count minors and majors using array 42. Prg for sum up of two array in third array 43. Prg to show boxing and unboxing 44. Prg for type casting from byte to int 45. Prg to find area of circle 46. Prg to find cube root of entered number 47. Prg for digit manipulation 48. Prg for printing a reverse triangle 49. Prg to show case and goto fall through 50. Prg to show use of foreach loop 51. Prg for adding 2 numbers 52. Prg to show jagged array 53. Prg to show different methods of Array 54. Prg to print number Pyramid 55. Prg to show entered string is palindrome or not 56. Prg to show entered number is prime or not 57. Prg to show different methods of StringBuilder 58. Prg to show different methods of String 59. Prg for appending a string using StringBuilder 60. Prg to show appending a string with numeric type data using StringBuilder 61. Prg to show use of ArrayList 62. Prg to pass an array as a parameter to a function for finding out a prime number 63. Prg to show the constant member 64. Prg for showing destructor 65. Prg for sum up and reversing the entered number 66. Prg for enum type conversation 67. Prg to show sum of first fifty even and odd number 68. Prg to find the factorial of entered number 69. Prg for iterating each element of array using foreach loop 70. Prg to show array position and value 71. Prg to show how to insert and remove string using StringBuilder 72. Prg to show how to join the string 73. Prg to show the value of sin 90 74. Prg to enter two number and find max 75. Prg to show padding 76. Prg to show entered number is prime or not 77. Prg to show private constructor 78. Prg to show how to use getter and setter property 79. Prg using out parameter 80. Prg to count the number of spaces and words in a String 81. Prg to show how to use Split method 82. Prg to create binary triangle 83. Prg to create dule star triangle 84. Prg to create triangle 85. Prg to show use of EndsWith and StartsWith method of string 86. Prg for static constructor 87. Prg to show how to use insert method 88. Prg to sort a string 89. Prg to store student name and roll no using structure 90. Prg to show how to use delegate 91. Prg to calculate depreciation 92. Prg for calculating Economic order cost and time between order 93. Prg to find radius of circle 94. Prg to sum up odd numbers from 0 to 20 95. Prg to calculate simple interest 96. Prg to categorise student on base of there marks range where marks are stored in array 97. Prg to create triangle on console screen 98. Prg to print sin theta, cos theta and tan theta from 0.1 to 1.0 angle in radians 99. Prg to demonstrate variables 100. Prg to illustrate the differences between int and double 101. Prg to show exception handling using try.. catch block 102. Prg to demonstrate how to use interface 103. Prg to print number triangle 104. Prg to demonstrate structure 105. Prg to test exception 106. Prg to count number of spaces and words in a entered string 107. Prg to demonstrate ref parameter 108. Prg to print a matrix table from 1 to 10 109. Prg to display 2 D array elements position in matrix form 110. Prg to count number of adult and children 111. Prg to print pascal triangle 112. Prg to demonstrate stringbuilder and its method 113. Prg to reverse a string 114. Prg for string sorting 115. Interview Question & Answer part 1 116. Interview Question & Answer part 2 117. Interview Question & Answer part 3 118. Interview Question & Answer part 4 119. Interview Question & Answer part 5 120. Interview Question & Answer part 6 121. Interview Question & Answer part 7 122. Interview Question & Answer part 8 123. Interview Question & Answer part 9 124. Interview Question & Answer part 10 125. Interview Question & Answer part 11 126. Interview Question & Answer part 12 127. Interview Question & Answer part 13 128. Interview Question & Answer part 14 129. Interview Question & Answer part 15
Pr.Pg Next Pg

Constructors in C# tutorials

  • A constructor is a special method that is used to initialize a newly created object and is  called just after the memory is allocated for the object.

  • It can be used to initialize the objects ,to required ,or default values at the time of object creation.

  • It is not mandatory for the coder to write a constructor for the class. By default C# provide one constructor for every class.

  • Constructor has the same name as that of class, they are called or invoked when an object of class is created and can't be called explicitly.

  • Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially.

  • It is optional to write constructor in a class but due to their utility they are used.

  • If no user defined constructor is provided for a class, compiler initializes member variables to its default values as below:

    1. numeric data types are set to 0

    2. char data types are set to null character(')

    3. reference variables are set to null

  • In order to create a constructor you must follow some rules as below:
    1. Constructors can use any access modifier, including public, protected, internal, private,extern, static.

    2. A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.

    3. Constructor has same name as class.

    4. Constructor should not return any value not even void.

Syntax: 

<modifier> class <class name>

{

<constructor_name_same_as_class>

{

//Body of constructor;

}

}


 

Example: CSharp program to demonstrate constructor

Sam Madam 

//CSharp program to demonstrate constructor

using System;

public class ConstructorDemo

{

int Num1;

int Num2;

ConstructorDemo()

{

Num1 = 10;

Num2 = 20;

Console.WriteLine("I am inside Constructor");

Console.WriteLine("Value1 = "+Num1);

Console.WriteLine("Value2 = "+Num2);

Console.WriteLine("End of Constructor");

}

public void display()

{

Console.WriteLine("I am inside display method");

Console.WriteLine("Value1 = "+Num1);

Console.WriteLine("Value2 = "+Num2);

Console.WriteLine("End of display method");

}

public static void Main(String[] args)

{

ConstructorDemo d1 = new ConstructorDemo();

d1.display();

}

}

 

Output

I am inside Constructor

Value1 = 10

Value2 = 20

End of Constructor

I am inside display method

Value1 = 10

Value2 = 20

End of display method


Parameterized constructors

  • Since a constructor is also a special method it can have arguments.

  • An argument is called as a parameter.

  • Parameters appearing in the call statement are known as the formal parameters and in definition are called as actual parameters.

  • Since constructor is called at the point of definition of object, the values to constructor are sent through definition.

  • The values are specified after object name in( ).

 

 

Example: Csharp program to illustrate parameterized constructor

Sam Madam

// Csharp program to illustrate parameterized constructor

using System;

public class Test

{

Test(int a, int b)

{

Console.WriteLine("Value of a :"+a);

Console.WriteLine("Value of b :"+b);

}

public static void Main(String[] args)

{

Test t1 = new Test(10, 20);

}

}

Output

Value of a :10

Value of b :20

Pr.Pg border                                              Next Pg