C# Tutorials for Beginners and Experienced Persons

Here I would like to explain about C#. language features and variables , every thing that make an unknown C# developer can write the program.


C# Language Features : 

C#, also know as C-Sharp, is a programming language introduced by Microsoft. C# contains features similar to Java and C++. It is specially designed to work with
 Microsoft's .Net platform.

C# Compiler :

A Compiler is a special program that processes the statements written in a particular programming language and converts them into machine language. Like everything else in the computer, the compiler also follows the Input-process-Output(I-O-P) cycle. It takes the programming language. These instructions can then be executed by the computer. This process of conversion is called compilation. For each programming language , there is a different compiler available. For example, for compiling a program written in the language , you require a Compiler, For a java program, you require a java compiler. For C# programs, you will use the csc compiler.

You will now learn how to create classes in the C# language.

Classes in C# :

Consider the following code example, which defines a Class:


Using System;

public class Hello
{

                   public static void Main(string[] args)
                   {
                               Console.WriteLine("Hello World");
                    }
}


The preceding class declaration provides a method main() that will display the message " Hello World" pn your screen. The parts of preceding code need to be examined.

The Main() Function: 

The first line of code that a c# compiler looks for in the source file Compiled is the Main() function. This function is the entry point of the Application.
          The Main() function is ideally used to create objects and invoke member functions.

The Class Keyword :

The Class keyword is used to declare a class. Keywords are reserved words that have a special meaning. Here, the class keyword defines the class Hello. The braces, known as delimeters, are used to indicate the start and end of a class body.

Example :

class Hello
{
            ........
}

C# Variables :

Variables represent storage locations. Every variable has a type that determines the values that can be stored in the variable. C# is a type-safety language and C# compiler guarantees that values stored in variables are always of the appropriate type.

C# Data Types :

C# is a strongly typed language therefore every variable and object have a declared type. Some of the data types and their ranges in C# are explained below as


Data Type Size in Bytes Range
int 4 -2,147,483,648 to 2,147,483,647
double 8 5.0X10 to the power of -324 to 1.7X10 to the power of 308
bool 1 True/False
char 2 0 to 65535

Hiding Label using Java Script with Example

Here I would like give a clear explanation on hiding a Label in a Page using Java script client side Programming.
<html>  
<script>
$(document).ready(function()
{  

document.getElementById('<%=lblInvalidMonth.ClientID%>').style.display = 'none'; });

 </script>  
<body>  

<asp:Label ID="lblInvalidMonth" runat="server" Style="color: Red;" Text="Label" Visible="False"> </asp:Label>

 </body>  
</html>

Here I am making a label to invisible at the time of document loading itself.

The Asp Label will not be appear while we are running the program.

Top most Dot Net Questions and Answers using C#

In this post I will explain about Top most Dot Net Questions and Answers.

How to Put the title for Tab ?

  <%@ Page Title="Tab Title" Language="C#" MasterPageFile="~/MasterPages/Master.Master" AutoEventWireup="true" CodeBehind="NewManageRequests.aspx.cs" Inherits="Eficaz.UI.Web.NewManageRequests" %>

we have to put the attribute title for displaying title of a tab while running the application.


There is no  title attribute in current  Page declaration then what title will be displayed on tab?

Master page title will be displayed. but first priority goes to current page title, if it is empty then master page title will be displayed

 

 

How will you convert a string to character array?

char[] chararray=string.ToCharArray();


What is C# compiler?
A Compiler is a special program that processes the statements written in a particular programming language and converts them into machine language. Like everything else in the computer, the compiler also follows the Input-process-Output(I-O-P) cycle. It takes the programming language. These instructions can then be executed by the computer. This process of conversion is called compilation. For each programming language , there is a different compiler available. For example, for compiling a program written in the language , you require a Compiler, For a java program, you require a java compiler. For C# programs, you will use the csc compiler.


What property is required for running the drop down list changed event?
AutoPostBack=true



 

How to Remove Drop here a Column... from ASPxGridView with Example

In this blog I would like to give a clear explanation on How to remove "Drop here a Column..." from
ASPxGridView.




I want to remove Drop here a Column... from my ASPxGridView,
The only thing we need to do is EmptyHeaders=" " (space) in  SettingsText tag.

<SettingsText EmptyDataRow="No Definitions available" EmptyHeaders=" "/>

Example :

<dx:ASPxGridView ID="detailGrid" AutoGenerateColumns="true" runat="server" KeyFieldName="SCHEDULEID"  SettingsText-GroupPanel=" "  OnBeforePerformDataSelect="detailGrid_DataSelect"     Width="100%">

<SettingsText EmptyDataRow="No Definitions available" EmptyHeaders=" " />


</dx:ASPxGridView>


output :








Note : EmptyHeaders=" " should contain a space.

Structure of Web.Config and App.Config in C# with Example

Let us know the Structure of Web.Config and App.Config in C# with Examples

Following pic depicts the structure of Web.Config



<configuration> tag is the root tag, under this we can have many tags,

<system.web> is the main and common tag, let us know about tags under the system.web and attributes under system.web tag.

<Compilation> is one tag under the <System.Web> tag.



Jagged Array with Examples

Here I would like to explain

Jagged Array :

The elements of a jagged array can be of different sizes and dimensions . A jagged array can be also called  as "array of arrays."

It doesn't allocate memory while declaring an array.

How to Declare a Jagged Array : 
we can declare the jagged array as

int[][] jaggedArray = new int[5][];

Here int[5][] specifies number of rows are 5 fixed.

Following specifies number of columns  for reach row. here

Declaring Column sizes for each Row in Jagged array:

jaggedArray[0] = new int[3]; first row contains 3 integer values
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
jaggedArray[3] = new int[8];
jaggedArray[4] = new int[10];

you can not create a row of different type other than the declaring type. here you can create a
size of integer type only, you can not create int to string
jaggedArray[1] = new string[5];//Error


We can initialize Jagged arrays in Three ways


First Way:

jaggedArray[0] = new int[] { 3, 5, 7, };
jaggedArray[1] = new int[] { 1, 0, 2, 4};
jaggedArray[2] = new int[] { 1, 6 };
jaggedArray[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 };
jaggedArray[4] = new int[] { 1, 0, 2, 4, 6, 34, 54, 67, 87, 78 };


Second Way:
int[][] jaggedArray = new int[][]
{
                    new int[] { 3, 5, 7, },
                    new int[] { 1, 0, 2, 4 },
                    new int[] { 1, 6 },
                    new int[] { 1, 0, 2, 4, 6, 45, 67, 78 }

};

Third Way:
int[][] jaggedArray =
{
                    new int[] { 3, 5, 7, },
                    new int[] { 1, 0, 2, 4 },
                    new int[] {1, 2, 3, 4, 5, 6, 7, 8},
                    new int[] {4, 5, 6, 7, 8}
};


Example Program :








How to use IndexOf Substring and Reverse in c# with examples

Here I would like to explain IndexOf and Substring and Reverse and How to use those functions in c#.

 

IndexOf  with Example: 

 

This is used for finding the index of a character and testing the location of a specified string.

 

string Name="Sathish";

IndexOf  is case sensitive method. example here name is sathish I want know the index for the character h
 

Console.Writeline(Name.IndexOf('H'));

It will print -1. Name.IndexOf('H')  will return -1-----> becuase it is a case sensitive method.

Console.Writeline(Name.IndexOf('h'))---> It will return 3. this method returns the index of first occurence of a character.

 

SubString with Example


The name it self tells that it is sub string of a string.

We can pass one argument and we can also pass two arguments.

One argument  : in this we pass only starting index, then this method return remaining from the starting index.

Two argument  : in this case we pass two areguments one is indicating the starting position the string and second parameter indecates the end of the substring.

 

  Reverse of a String and how to convert string to Char array

we are converting a string to char array as 
char[] arr=string.ToCharArray();
you can not print Console.Writeline(Array.Reverse(arr)), it will thows an error. we need to update the character array first then we have to print

How to split numbers with commas separated using c#

Here is an Example that gives a clear explanation about how to split big integer numbers with commas separated using C# and Data Table



Here is my table, I would like to split update my table's column Records data to commas separated data

Ex:9998 becomes 9,998

My Target :


For getting this what we have to do is :



Here reportData is my table data. there is a method called ToString with one argument is use full for getting our target table , we can convert only integer type data we can not convert string data, if there is a chance to convert our data into integer type then we are able to split the data with commas separated data.

here I am able to convert item["Records"] and item["RecordsLoaded"] into integer type so integer value.(dot) ToString will take an argument for splitting the numbered data but whereas with string value.ToString doesn't take argument so we can not split string data using ToString.

If the number is too long then you can also convert the number to ToDouble... but we need to extends the #s.

item["Records"]=Convert.ToDouble(Records).ToString("###,###,###,###,###");

MVC with Asp.Net

Here I would like to explain Asp.Net and MVC

What is Asp.Net : ASP.NET is a framework for building web pages and web sites with HTML,server scripting, CSS and JavaScript.

Asp.Net supports :

1. Web Pages
2.Web Forms
3.MVC(Model View Controller)

MVC : Model View Controller is a  highly testable and lightweight framework, it is integrated with all existing ASP.NET features, like
1.Authentication
2.Master Pages
3.Security etc....

Why MVC and what are the advantages of MVC : 

1.  Clear architecture :
 
  . MVC provides a clrear understanding of architecture (User Interface , Business Logic and Data).

2. Lightweight : 

     What is lightweight?

     ASP.NET MVC framework doesn’t support for View State.


3. Testability

     MVC  provides better testability of the Web Application.

How to Create A sample Project with Asp.Net MVC

1. Open Visual Studio 2012 or latest version.( type devenv on run then press enter).

     then you will find the following screen.




2.Go to File--->New--->Project



     then you will find the following screen.




 3.Click on Ok then you can see the following screen.




4.then click on Ok, then you can find.



5.You can see many folders in right side under solution explorer.




You can find the solution explorer in View. Here all the folder , config files , properties and references are created by default.

1 Properties :

It contains a class called assemblyInfo.cs This is just about metadata.It’s not a mandatory class. You can also delete, that doesn’t effect our solution execution.

2.References: 

Its just for adding extra  references to the solution.
for example. I need FileStream in my solution then I need to add System.IO to the references as






Right click on References and select Add reference they type System.IO in search assembly. then select the check box of System.IO then click on Ok. then reference System.IO is added to you application.

You can AssemblyInfo (Properties) class In the above pic.

3.App_Data : 

App_Data is essentially a storage point for file-based data stores Some simple sites are used to store content type XML files.

4.Controller :

Controller contains the classes for handling user inputs and responses.
You can add the controllers by right clicking the controllers folder and selecting Add --->controller--->
then you will find the following screen. you need to type controller name and clicking add.






Here main things are
1.Controller
2.Model
3.Views


Following is the Layout, is nothing but Master page is asp.net.


 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>


Where @ViewBag.Title under the <title> tag  is used for displaying the title.

@Styles.Render is used for rendering bundle of Css file from BundleConfig.cs files.

@Scripts.Render is used for rendering bundle of scripts.


following is the BundleConfig class


 
 
 
 
 
 
 
 
 














public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }
    }


@RenderBody: This will be replaced by Text you written in views. it is like ContentPlaceholder in asp.net.