Friday, 24 June 2016

C# Tutorial - For Beginners & Professionals ~ Introduction to C Sharp


C# is developed by microsoft and comes after C, C++, Java. It inherits the properties of C, C++, Java, VB. We can say C# is smart and intelligent sister of Java because it do work smartly in comprasion to Java. The basic concept of C# language are same as C, C++ to whom you have learnt in C, C++ tutorials. The advance concept of C# language are as :

Object

Object is representative of the class and is responsible for memory allocation of its data members and member functions.An object is a real world entity having attributes (data type) and behaviors (functions).

Class

Class is a data structure that contains data members (constants files, events), member function methods, properties, constructor, destructor, indexers and nested type.Basically :
  1. It is a user defined data type.
  2. It is a reference type.
  3. Infact class is a tag or template for object.

Drawback of Class

Class does not allocate memory to its data members & member function itself.Basically memory is allocated through object of a class.Class can’t explore itself means it can not access its members itself, to access members of a class we use object of that class.

Example :

  1. // Namespace Declaration
  2. using System;
  3. // helper class
  4. class ClassA
  5. {
  6. string myString;
  7. // Constructor
  8. public ClassA(string str)
  9. {
  10. myString = str;
  11. }
  12. // Instance Method
  13. public void Show()
  14. {
  15. Console.WriteLine("{0}", myString);
  16. }
  17. // Destructor
  18. ~ClassA()
  19. {
  20. // Some resource cleanup routines
  21. }
  22. }
  23. // Program start class
  24. class ClassProgram
  25. {
  26. // Main begins program execution
  27. public static void Main()
  28. {
  29. // Instance of ClassA
  30. ClassA objA = new ClassA("Welcome to the world of C# language !!");
  31. // Call ClassA method
  32. objA.Show();
  33. }
  34. }
Read More »

C# Tutorial - For Beginners & Professionals ~ C Sharp Var data type and Anonymous Type


Var data type was introduced in C# 3.0. var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration.

Valid var statements

  1. var str = "1";
  2. var num = 0;
  3. string s = "string";
  4. var s2 = s;
  5. s2 = null;
  6. string s3 = null;
  7. var s4 = s3;

At compile time, the above var statements are compiled to IL, like this:

  1. string str = "1";
  2. int num = 0;
  3. string s2 = s;
  4. string s4 = s3;

The compile-time type value of var variable cannot be null but the runtime value can be null.

  1. // invalid var statements
  2. var v; //need to initialize
  3. var num = null; // can’t be null at compile time

Once var variable is initialized its data type became fixed to the type of the initial data.

  1. // invalid var statements
  2. var v2 = "str12";
  3. v2 = 3; // int value can’t be assign to implicitly type string variable v2

Anonymous Types

An anonymous type is a simple class generated by the compiler within IL to store a set of values. var data type and new keyword is used to create an anonymous type.
  1. var emp = new { Name = "Deepak", Address = "Noida", Salary=21000 };

At compile time, the compiler will create an anonymous type, as follows:

  1. class __Anonymous1
  2. {
  3. private string name;
  4. private string address;
  5. int salary; public string Name
  6. {
  7. get{return name; }
  8. set { name=value }
  9. }
  10. public string Address
  11. {
  12. get{ return address; }
  13. set{ address=value; }
  14. }
  15. public int Salary
  16. {
  17. get{ return salary; }
  18. set{ salary=value; }
  19. }
  20. }

The anonymous type is very useful when you want to shape the result in your desired form like this:

  1. var result =from book in Books
  2. where book.Price > 200
  3. orderby book.IssueDate descending
  4. select new
  5. {
  6. Name = book.Name,
  7. IssueNumber = "#" + book.Issue
  8. };
In above example, I change the name of the “Issue” field of Book table to “IssueNumber” and add # before value to get desired output.
Summary
In this article I try to explain var data type with example. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
Read More »

C# Tutorial - For Beginners & Professionals ~ C Sharp Anonymous Method

The concept of anonymous method was introduced in C# 2.0. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say, an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.

A Simple Anonymous Method Example

  1. delegate int MathOp(int a, int b);
  2. class Program
  3. {
  4. //delegate for representing anonymous method
  5. delegate int del(int x, int y);
  6.  
  7. static void Main(string[] args)
  8. {
  9. //anonymous method using delegate keyword
  10. del d1 = delegate(int x, int y) { return x * y; };
  11.  
  12. int z1 = d1(2, 3);
  13. Console.WriteLine(z1);
  14. }
  15. }
  16. //output:
  17. 6

Key points about anonymous method

  1. A variable, declared outside the anonymous method can be accessed inside the anonymous method.
  2. A variable, declared inside the anonymous method can’t be accessed outside the anonymous method.
  3. We use anonymous method in event handling.
  4. An anonymous method, declared without parenthesis can be assigned to a delegate with any signature.
  5. Unsafe code can’t be accessed within an anonymous method.
  6. An anonymous method can’t access the ref or out parameters of an outer scope.

Anonymous Method as an Event Handler

  1. <form id="form1" runat="server">
  2. <div align="center">
  3. <h2>Anonymous Method Example</h2>
  4. <br />
  5. <asp:Label ID="lblmsg" runat="server" ForeColor="Green" Font-Bold="true"></asp:Label>
  6. <br /><br />
  7. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />  
  8. <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
  9. </div>
  10. </form>

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. // Click Event handler using Regular method
  4. btnCancel.Click += new EventHandler(ClickEvent);
  5. // Click Event handler using Anonymous method
  6. btnSubmit.Click += delegate { lblmsg.Text="Submit Button clicked using Anonymous method"; };
  7. }
  8. protected void ClickEvent(object sender, EventArgs e)
  9. {
  10. lblmsg.Text="Cancel Button clicked using Regular method";
  11. }
 
Summary
In this article I try to expose anonymous method with simple example. I hope after reading this article you will be able to use anonymous method in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
Read More »